Wikipedia:Reference desk/Archives/Mathematics/2024 September 6
Mathematics desk | ||
---|---|---|
< September 5 | << Aug | September | Oct >> | Current desk > |
Welcome to the Wikipedia Mathematics Reference Desk Archives |
---|
The page you are currently viewing is a transcluded archive page. While you can leave answers for any questions shown below, please ask new questions on one of the current reference desk pages. |
September 6
[edit]Distance to a line segment
[edit]I am unsure if this is better in Maths or in Computing, but I've chosen Maths.
In a standard planar (x,y) world, a line segment is defined by two endpoints P1=(x1,y1) and P2=(x2,y2).
A third point (anywhere, not necessarily off the line segment extension) is A=(xa,ya).
It is easy to calculate the distance from A to P1 and from A to P2. Sometimes one will be the answer for which part of the line segment is closest to A.
But if A is "perpendicularly within P1~P2", then the closest part of P1~P2 will be somewhere on that line segment.
Is there a standard algorithm for determining the coordinates of the closest part of the line segment to A?
--SGBailey (talk) 22:59, 6 September 2024 (UTC)
- I don't know if it's "standard", but it's not too hard to work out. Let D be the square distance between P1 and P2, so D = (x2-x1)2 + (y2-y1)2. Let E be twice the (signed) area of the triangle P1P2A, so E = x1y2 - x1ya - x2y1 + x2ya + xay1 - xay2. Note that if D=0 then the result is undefined, if E=0 then the result is just A, if x1=x2 then y=ya, and if y1=y2 then x=xa; these facts can be used as checks. Then, using Cramer's rule and a few tricks I'm pretty sure I wouldn't be able to fully explain, I get x = xa + (y2-y1)E/D, y = ya - (x2-x1)E/D. Geometrically, you know that the vector PA is perpendicular to P1P2, which means P can be written A + mP1P2⊥, where P1P2⊥ is normal to P1P2; we can take this as (-(y2-y1), x2-x1). You can then solve for m by finding the area of the triangle P1P2A in two ways. Note that if you just want the distance to the line it's a lot easier, just divide twice the area of the triangle, |E|, by the length of the segment P1P2. I'm assuming here that you want the closest point to the line P1P2, since there's no guarantee that the result P will be between P1 and P2, and if not it technically won't be on the segment P1P2. --RDBury (talk) 05:51, 7 September 2024 (UTC)
- Here is an analytic approach. The line through the distinct points and can be parametrically represented by
- (The usual equation for the line can be obtained by eliminating from this equation.)
- The vector connecting a generic point on the line to a point not on the line is then given by
- The length of this vector is minimized when its square is, which is given by the quantity
- Now solve for and substitute the result in the parametric equation and you have the coordinates of the nearest point. Note: if is on the line, this procedure may lead to division by zero.
- (If done by hand, it helps to first rewrite the parametric equation as
- Also, alternatively, to avoid differentiation, rewrite the equation for in the form The value of minimizing is then given by )
--Lambiam 06:47, 7 September 2024 (UTC)- One advantage to this method is that it works just as well in multiple dimensions. A more general version is: Given k points P1, ... Pk, and l points Q1, ... Ql in Rn, with k+l≤n-1, find a formula for the points P and Q where P is on the affine space determined by the Pi's, Q is on the affine space determined by the Qi's, and P and Q are a close as possible to each other. It might be easier if the affine planes were given by systems of equations instead of as affine hulls. --RDBury (talk) 07:31, 7 September 2024 (UTC)
- PS. It might be worth mentioning that when you solve for lambda, the resulting point P will be the one where AP⊥P1P2, so you're basically just proving what was assumed in the original question. This holds for any smooth curve and even smooth surfaces; if P is the closest point to A on a smooth curve/surface, then PA is perpendicular to the tangent line/plane at P. --RDBury (talk) 01:53, 9 September 2024 (UTC)
Thanks. That is probably resolved, but I need to study the replies. -- SGBailey (talk) 18:02, 8 September 2024 (UTC)