• Create Account

# In need of an algorythm!

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

2 replies to this topic

### #1iwoplaza  Members   -  Reputation: 174

Like
0Likes
Like

Posted 10 January 2014 - 12:10 PM

Hi.

The picture shows a point and a line in a 2d space. Does someone know what algorythm do I use to calculate the '?' in the picture? It is the distance between the 'point' and the line, but the angle between the line and the "distance line" has to be 90 degrees. I want to use that in my physics engine. And can someone help me find the 3D equivalent of that as well?

### #2Brother Bob  Moderators   -  Reputation: 10026

Like
6Likes
Like

Posted 10 January 2014 - 12:27 PM

POPULAR

You want to find the closest point on a line segment. It's a simple linear algebra problem and there are plenty of information on the net.

In short, let A and B be the points at the end of the line segments, and P the point near the line segment. Let AB = normalize(B-A) be the normalized vector from A to B, and AP = P-A be the vector from A to P. The point in the line segment closest to the point P is the projection of AP onto AB: C = A + AB*dot(AP, AB).

That solution assumes the line segment is infinitely long or that the point C is between A and B. If this is not the case, and you want to cap the point C at the ends of the line segment, then cap the result of the dot product between 0 and 1 before proceeding with the calculations.

### #3iwoplaza  Members   -  Reputation: 174

Like
0Likes
Like

Posted 10 January 2014 - 12:55 PM

Thanks a lot man, that got me on the track .

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

PARTNERS