I have a bunch of measured points in an image (as shown in the picture below), and I want to find the best square-sized lattice to fit those measured points, such that the sum of distance of each measured point from its closest lattice point reaches a minimum. Note that the measured points are not necessariy "complete" with regard to the ideal lattices points, meaning that some of the ideal lattice points may not have corresponding measured points (actually I also want to use the ideal lattice to "recover" the missing measured points).
It seems the square-sized lattice can be determined by the following parameters:
- pitch (horizontal pitch == vertical pitch, since it's a square-sized lattice)
- angle
- an origin point (seems it's not unique, we can just pick any ideal lattice point as the origin)
But I stuck at formalizing the distance between a measure point and its closest lattice point...
A picture with measure points:
So far I find two related posts:
Lattice fitting to points This one is almost the same as my problem, and the "solution" given by the poster himself should be viable for me too. But I want to find a "better" one.
The other one Matrix least squares with rows instead of columns seems have a nicer solution, but the problem is a little bit different.

As I did not find a general solution to my problem, I tried to find a solution for my specific problem at hand. Compared to general questions I asked, what's specific in my problem are that:
The idea of my current solution is roughly:
I chosen the R package Ckmeans.1d.dp for clustering the X coordinates, and use Newton-iteration for find the optimal $\theta$.
So far it seems it works for me.
For one of my data set, the $\theta$ vs. total withinss plot shows that it's a "good" function:
The code snippet in R:
The output of the code: