2

Finding distance to a point from a line defined by two points is well answered, Shortest distance between a point and a line segment . Within that answer http://paulbourke.net/geometry/pointlineplane/ illustrates the line P1, P2 with point P3 tangent to the line showing how to determine distance to that point. This is a GPS application so the heading of the line is already determined and I would like to not use point slope formula or intercept with its limitations.

I would like to determine the point back on the line where the tangent intersects that line from P3 defined by P1 P2. Thank you in advance.

Edit:

I have a solution but its a bit cumbersome, but maybe not, it certainly works:

1) Calculate distance to point by 2 point line to P3 but don't take abs value 
2) If distance < 0 then side = 1 else side = -1 
3) dist = abs(distance) 
4) rad2 = heading + PI/2 * side //tangent is always 90 degrees to line
5) sin rad2*dist + P3.x = point Q.x 
6) cos rad2*dist + P3.y = point Q.y

Seems to work no matter the side the point is on

sketch

Community
  • 1
  • 1
Briantee
  • 65
  • 5
  • 2
    Can you add a sketch maybe. I am having trouble visualizing the request. – John Alexiou Aug 01 '16 at 17:10
  • The image shows a line. Point P is also known and is P3 as above. I want to determine Q [1]: http://i.stack.imgur.com/bTFIg.gif – Briantee Aug 01 '16 at 19:15
  • Do I get you right - in your case P1, P2, and P3 are known, and you also know the tangent vector? – Alex Bakulin Aug 02 '16 at 05:10
  • Yes, the tangent is 90 degrees to the line P1, P2. However the point can be on either side of the line. Also by calculating the distance of a point to a line defined by 2 points (Paul Bourke link), i also know the length of that tangent from the line to the point P3. Easily calculating the coordinates of "Q" is what i am after. – Briantee Aug 02 '16 at 05:14
  • 2
    Paul's article gives a formula for calculating the coordinates of the intersection point (x, y). Isn't it good enough for you? – Alex Bakulin Aug 02 '16 at 05:30
  • You want the point `Q` on a known line closest to another point `P`? – John Alexiou Aug 02 '16 at 11:00
  • The article refers to falling within the point segment only, does it not? – Briantee Aug 02 '16 at 15:05
  • No, the point can be anywhere along the line. – John Alexiou Aug 02 '16 at 18:46
  • I'm not sure how to code ||P2-P1|| ^2 to derive u. Is it ((P2.x - P1.x) - (P2.y-P1.y)) ^2 ? – Briantee Aug 03 '16 at 05:07

1 Answers1

0

If a line is known by the equation A*x+B*y+C=0 and a point P is outside the line with coordinates (P_x,P_y) then the point on the line closest to P is

x = (A^2*P_y-A*B*P_x-B*C)/(A^2+B^2)
y = (B^2*P_x-A*B*P_y-A*C)/(A^2+B^2)

Also the minimum distance of point P to the line is

d = ABS(A*P_x+B*P_y+C)/SQRT(A^2+B^2)

Edit 1

The equation of an infinite line passing through two points (x_1,y_1) and (x_2,y_2) is

A*x+B*x+C=0
(y_1-y_2)*x + (x_2-x_1)*y + (x_1*y_2-x_2*y_1) = 0

Edit 2

If the line is given from point (Q_x,Q_y) and direction (e_x,e_y) then the equation coefficients are

A = -e_y
B =  e_x
C = Q_x*e_y - Q_y*e_x
John Alexiou
  • 28,472
  • 11
  • 77
  • 133
  • But the line is not known by the equation. Thanks for your answer, it is very good when the equation is known. I would like to only use P1 and P2 defining an infinite line where P3 can be anywhere along that line. – Briantee Aug 02 '16 at 15:08
  • So how is the line defined? 1) Two points, 2) Point and Direction, 3) equation, 4) other construct. – John Alexiou Aug 02 '16 at 18:47
  • 1. Two points derived from a point and a heading. I have a solution but its a bit cumbersome: 1) Calculate distance to point by 2 point line to P3. 2) If distance < 0 then side = 1 else side = -1 3) dist = abs(distance) 4) rad2 = heading+PI/2*side 5) sin*rad2*dist + P3.x = point x 6)cos*rad2*dist + P3.y = point y – Briantee Aug 03 '16 at 05:19
  • So edited the answer in order to find the line equation coefficients `A`, `B` and `C` from a point and a direction. – John Alexiou Aug 03 '16 at 12:23
  • is e_x and e_y a normalized vector? – Briantee Aug 03 '16 at 13:20
  • It doesn't have to be. It works with scalar multiples also. This is because `A*x+B*y+C=0` is the same line as `2A*x+2B*y+2C=0` and the coefficients are derived from `e_x` and `e_y` by multiplications and additions (linear operations). – John Alexiou Aug 03 '16 at 14:33