This question is motivated by the Euclidean Traveling Salesman Problem, i.e. finding the shortest Hamiltonian path of a complete graph of N randomly placed vertices. To eliminate boundary effects I consider the problem on the unit square with periodic boundary conditions. The idea is to find a "direction" and connect points in an ordered manner along this direction to find a short path (Wheeler, J.A., On recognizing law without law, Am. J. Phys, 51(5), pp. 398, 1983).

The question is: given N points with real coordinates on the unit square, is there a neat way to find the least-square fit line $y=rx+b \mod 1$, where $r=p/q$ is rational? The line also needs to be as short as possible. The length of the line in the square is $\sqrt{p^{2}+q^{2}}$, so minimizing the following quantity should yield a short line that passes near the points:

$\sum d_{i}^{2}+\alpha \left( p^{2}+q^{2}\right) $.

Here $d_{i}$ is the "distance" (vertical or perpendicular) of point i from the line and $\alpha$ is a positive weight.

By "neat" I mean something like the regular least-square line fit solution (http://mathworld.wolfram.com/LeastSquaresFitting.html).