Jump to content
  • Advertisement

Archived

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

Endemoniada

Point->Line-Segment Distance ?

This topic is 5470 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, I know how to find the distance of a point to a line but not a line-segment. It could be one of either endpoints or a point in between. Does anyone have the formula ? Thank you.

Share this post


Link to post
Share on other sites
Advertisement
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
Nervo: OP is trying to find the shortest distance from a point to a line, restricted to a given segment on the line. Mastaba''s solution works.

Share this post


Link to post
Share on other sites
I see. In that case then the shortest distance would not necessarily be perpendicular to the segment and that would make my solution worthless.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!