Archived

This topic is now archived and is closed to further replies.

Endemoniada

Point->Line-Segment Distance ?

Recommended Posts

D=((x1-x2)^2+(y1-y2)^2)^(1/2)

EDIT: Anyway you slice it, you need two points established, unless you're trying to ask something else.

[edited by - nervo on July 2, 2003 12:20:26 PM]

Share this post


Link to post
Share on other sites
Hi,

Maybe I didn''t explain it correctly.

I want to find the shortest distance between Point C and Line Segment AB (in 2D.)

So Line Segment AB is (Ax,Ay) to Bx,By) and Point C is (Cx,Cy).

Your formula didn''t even take into account the point I''m trying find the distance from.

Thanks.

Share this post


Link to post
Share on other sites
Find the line perpendicular to the line that passes through the point. Then find the distance from that point to the intersection of the line and the line segment.


How appropriate. You fight like a cow.

Share this post


Link to post
Share on other sites
quote:
Original post by Endemoniada
Hi,

Maybe I didn''t explain it correctly.

I want to find the shortest distance between Point C and Line Segment AB (in 2D.)

So Line Segment AB is (Ax,Ay) to Bx,By) and Point C is (Cx,Cy).

Your formula didn''t even take into account the point I''m trying find the distance from.

Thanks.



I wouldn''t say that ''maybe'' you asked incorrectly because this question in no way resembles your original one.

Okay:
Just as Sneftel said you need to find a line that meets perpendicular with the other line segment. Once you understand that concept yields the shortest distance then:

let m = slope of line segment AB

We will call the other line CD with slope m2

Slope of a line that is perpendicular to segment AB is simply the negative inverse of segment AB''s slope. hence

m2 = -(1/m)

Now with the slope determined of segment CD we simply plug that into the equation y = mx + b because now we need to find the constant that gives the needed vertical translation of the line.

Therefore all you would need to do is "plug in" the x and y value of the point that you have into the equation and that will give you the constant if their is one other than zero.

Since you want to find the actual distance now just equate both line forumlas to determine at what point they intersect.
(mx + b) = (m2x + b2)

Finally plug in the two points into the original distance formula that I gave you, and your answer is there after a quick plug and chug.

Got it?


Share this post


Link to post
Share on other sites
It is really fairly simple to find the shortest distance between a point C and line segment AB. First determine shortest distance between C and the line D that passes through AB. That is, find the intersection E of the line perpendicular to D that contains C. Now test if E is on the line segment AB. If it is, then the shortest distance is the length of CE. If it isn''t, then simply take the shorter of AC and BC.

Share this post


Link to post
Share on other sites
Heh, this just reminded me of an old calculus problem I had a few years ago. It was exactly this problem. Finding the shortest distance between a point and line segment (not line as some above seem to be doing). It is was in the chapter on the calculus of variations which is suited for this type of minimization problem.

Share this post


Link to post
Share on other sites
Are you speaking of optimization problems?

Others might not agree with me, but I sorta thought your solution to the above problem was kind of cryptic.

Share this post


Link to post
Share on other sites
I don't really like to give explicit code, as I'd rather see people learn the math behind the code. I'll clearly give you a hint however.



Good Luck!

[edited by - Mastaba on July 3, 2003 8:53:33 PM]

Share this post


Link to post
Share on other sites


point PointOnSegment(point C, point A, point B)
{
point D = (C.x - A.x, C.y - A.y); // D = C - A

point E = (B.x - A.x, B.y - A.y); // E = B - A

float DdotE (D.x * E.x + D.y * E.y); // DdotE = D . E

float e2 = E.x*E.x + E.y*E.y; // e2 = E . E


if (DdotE < 0.0f)
return A;

if (DdotE > e2)
return B;

DdotE /= e2; // DdotE = (D . E) / (E . E)


point F = (E.x * DdotE, E.y * DdotE); // F = E * (D . E) / (E . E)


return point(A.x + F.x, A.y + F.y); // return A + F

}

Share this post


Link to post
Share on other sites