4

I would need to write an algorithm to find the closest point to a set of lines. These lines are infinite and are not parallel between each other. Closest point means that point where the sum of the squared distances between the point and each of these lines is minimum.

Let's suppose a simple 2D case, where I have a line passing through $a_0 (1, 3)$ and $b_0 (2, 2)$ and another one through $a_1 (1, 1)$ and $b_1 (2, 2)$

I tried a couple of algorithms reading some other questions like this one but I couldn't get a valid result from the general formula $$0 = \sum_{i=0}^m\vec c - \vec a_i - \vec d_i \frac{(\vec c - \vec a_i)\cdot \vec d_i}{\|\vec d_i\|^2}$$

I tried to set a system made by $$ \left\{ \begin{array}{l} 0 = c_x-a_{x0}-d_{x0}\dfrac{(c_x-a_{x0})\cdot d_{x0}}{\|d_{x0}\|^2} + c_x-a_{x1}-d_{x1}\dfrac{(c_x-a_{x1})\cdot d_{x1}}{\|d_{x1}\|^2} \\ 0 = c_y-a_{y0}-d_{y0}\dfrac{(c_y-a_{y0})\cdot d_{y0}}{\|d_{y0}\|^2} + c_y-a_{y1}-d_{y1}\dfrac{(c_y-a_{y1})\cdot d_{y1}}{\|d_{y1}\|^2} \end{array} \right. $$

but I end up with

$$ \require{cancel} \left\{ \begin{array}{l} 0= \cancel{c_x} - 1 - \cancel{c_x} + 1\\ 0=\cancel{2c_y} -4 -\cancel{c_y} +3 - \cancel{c_y} -1 \end{array} \right. $$

So I guess I didn't apply properly the equations..

Then I also followed this other answer

I took two points on the line $$a_0 (1, 3)$$ and $$b_0 (2, 2)$$ subtracted to get a vector $$\vec v = b_0 - a_0 = (1, -1)$$ rotated by 90° $$(x, y) \to (-y, x)$$ that is $$(1, -1) \to (1, 1)$$ and divided by its length $$\sqrt{1^2+1^2} = \sqrt2$$ that is $$\left(\frac {1}{\sqrt2}, \frac {1}{\sqrt2}\right)$$ but I don't know how to get the formula

$$a_ix + b_iy+c_i$$

And then I also found this one, but it sounds too complicated..

How can I solve?

elect
  • 173
  • Since the two lines intersect at $(2,2)$, isn't that the point you're looking for? – hmakholm left over Monica Jun 18 '15 at 11:47
  • @HenningMakholm exactly, but this is just a simple scenario to test the algorithm I am looking for – elect Jun 18 '15 at 11:48
  • x @elect: As long as you have just two (non-parallel) lines, their intersection point will always be the "closest", right? In order to get an interesting situation you need three lines that don't meet in a point. – hmakholm left over Monica Jun 18 '15 at 11:51
  • @HenningMakholm Yeah, this is the simplest scenario, the real scenario involves hundred, if not thousand, of lines (and I know they are not parallel). – elect Jun 18 '15 at 11:53
  • For a simplest case though, you really will need to try with at least 3 lines. Since as above, the 2 line case is degenerate. – Alan Jun 18 '15 at 12:05
  • I dont get why 2 not-parallel line case should be degenerate – elect Jun 18 '15 at 12:09
  • @elect: Because the solution is always just their intersection point! – hmakholm left over Monica Jun 18 '15 at 12:15
  • Ok, then I though degenerate meant another thing.. Anyway, as long as the solution is valid it is ok for me. The problem is that I can't even obtain the result in the 2 line case.. – elect Jun 18 '15 at 12:18
  • Are we talking about infinite lines, or line segments (bounded by two points). Measuring distance from a point to an infinite line is easy; distance to a bounded line segment is a bit harder. – bubba Jun 18 '15 at 14:10
  • Now I think about it ... It's not clear what you mean by "the closest point". Suppose I have three lines forming a triangle. What's the closest point to those three lines??? We can't provide formulae if we don't know what we're trying to compute. – bubba Jun 18 '15 at 14:15
  • @bubba infinite lines. Let me update the question – elect Jun 18 '15 at 14:16

1 Answers1

1

It took me a while but I realized how to do.

So, let's define the generic closest point as $$p (x_0, y_0)$$ and then let's call $d_i$ the generic $i_{th}$ distance from the $line_i$ to $p$

We have to find the point $p$ in order that sum of all the $n$ distancies $d_i$ is smallest as possible.

In order to do this, we need to sum all the squared distances $$\sum_{i=0}^n d_i^2$$

In the previous example with two lines $$ \begin{aligned} &L_0: \qquad x+y-4=0 \\ &L_1: \qquad x-y=0 \end{aligned}$$

We calculate the corresponding distances as following: $$\begin{aligned} d_0&=\dfrac{|x_0+y_0-4|}{\sqrt{1^2+1^2}}\\ d_1&=\dfrac{|x_0-y_0|}{\sqrt{1^2+(-1)^2}} \end{aligned}$$ so the sum of squared distances is: $$\sum_{i=0}^2 d_i =\left(\dfrac{|x_0+y_0-4|}{\sqrt{1^2+1^2}}\right)^2 + \left(\dfrac{|x_0-y_0|}{\sqrt{1^2+(-1)^2}}\right)^2=\\ =2x_0^2+y_0^2-8x_0-4y_0+12$$

to get the minimum we have to derivate that partially first on $x_0$ and then on $y_0$:

$$\dfrac{d}{dx_0} 2x_0^2+y_0^2-8x_0-4y_0+12 = 4x_0-8\\ \dfrac{d}{dy_0} 2x_0^2+y_0^2-8x_0-4y_0+12 = 2y_0-4$$

then put them equal to 0 (since we look for the minimum) and put everything into a system of equations:

$$ \left\{ \begin{array}{l} 4x_0-8=0\\ 2y_0-4=0 \end{array} \right. $$

from where we obtain $$p (2, 2)$$

elect
  • 173