algorithm - C++, point and line position in 2D robust to rounding errors and position

A line is defined by two end points P1[x1, y1], P2[x2, y2]. Let Q [xq, yq] be a tested point. Both coordinates are double.


dx1 = x2 - x1
dy1 = y2 - y1
dx2 = xq - x1
dy2 = yq - y1


double n1_sq = sqrt(dx1 * dx1 + dy1 * dy1);
double n2_sq = sqrt(dx2 * dx2 + dy2 * dy2);

My assumption: test with normalized vectors is less sensitive to rounding errors

double test = (dx1 / n1_sq  )  * (dy2 / n2_sq) - ( dx2 / n2_sq ) * ( dy1 / n1_sq );


double test = dx1 * dy2 - dx2 * dy1;

The problem arises in the following cases:

A) A tested point is Q is on the line

Q = [0.5(x1 + x2), 0.5(y1 + y2)]

In many cases, the result is not zero, but

test >> 0

B) Inappropriate configuration of the line/ tested point

Case 1) long segment:

Let us move the tested point to the start point, dist (Q, |p1, p2|) = 7e-4

P1 = [0, 0]
P2 = [1000000000.00001, 1000000000.00001]
Q = [0.0,0.001] 

Normalized test: 0.7 Unnormalized test: 1.0e+6

Case 2) long segment:

Let us move the tested point to the end point dist (Q, |p1, p2|) = 7e-4

P1 = [0, 0]
P2 = [1000000000.00001, 1000000000.00001]
Q = [1000000000.0, 1000000000.001] 

Normalized test: 5.0e-13 Unnormalized testt: 1.1 e+6

Case 3) short segment:

Let us move the tested point to the start point, dist (Q, |p1, p2|) = 7e-4

P1 = [0, 0]
P2 = [0.00001, 0.00001]
Q = [0.0,0.001] 

Normalized test: 0.7 Unnormalized test: 1.0e-8


A) The normalized test for long segments is less reliable. The Case 2 could be considered as machinery zero with the following decision: Q is on |p1, p2|...

B) For short segments, the situation is reversed, an unnormalized test gives a machinery zero.

But result of both tests are not constant and the resulted value does not bring any information about real distance of the point Q from the line |p1, p2|. Using a threshold in both tests does not bring a correct result... And the value of the threshold can not be determined before..

What should I do?

My solution is to replace both test with the new test: test the distance of point Q and line P1, p2 and using some threshold eps. Point Q with

dist (Q,|P1,P2|) < eps,  (for example 1e-10) 

will be placed on the line P1, P2... The result of the test does not depend on configuration of points (ie if we move the test point Q along the segment P1, P2)

Is anyone using a better test or have a different solution of this problem?

2 Answers

  1. Lori- Reply


    I don't understand your test. It doesn't seem to involve the coordinates of Q at all. Also, for two values u and v, the norm is computed with minimum rounding as M * sqrt(1 + m / M), where M = max(|u|, |v|) and m = min(|u|, |v|). As for a dist function, that is the best approach, although you might want to make the threshold a function of the line segment length. It really depends on your application.

  2. Lorin- Reply


    You're using the wedge product (which in 2d is a determinant) to find your distance. I suppose the problem is that you may be subtracting similar quantities so that your result is overcome with truncation error. Try using the dot product instead. d = d1.d2/|d1|.

Leave a Reply

Your email address will not be published. Required fields are marked *

You can use these HTML tags and attributes <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>