# Untitled

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

## Recommended Posts

Hi i think i'll annoy you one more time with my newbie questions :D let's assume i have a point and a line in 3D space:
        /
o     /
/
/
/
/
/

and i connect the point to the line with the shortest path possible (perpendicular, even though on the picture it's not ;))
        /
o     /
\   /
\ /
/
/
/

what i'd like to know is the coord of the point on the line that the perpendicular touches / i think a picture will be clearer:
        /
o     /
\   /
\ /
o <-- i want to find this point
/
/


how can i do that? i could do it in 2D but in 3D i don't know... thanks!! (i don't need code, pseudo-code or an explaination will do)

##### Share on other sites
Take a look here
Distance Point to Line
There's explanation above, but basically they find the shortest distance from point to line, by first finding the point on the line (Pb) closest to the point P...
Then they calculate the distance from P to Pb... you just need Pb.

##### Share on other sites
Use the dot product. It's used to determine the projection of one vector onto another.

say your line is connected between points A and B, and you want the closest point to P on that line.

vec rayDir = (B - A).normalize();vec toP   = (P - A);float dotProduct = rayDir.dot( toP );vec closestPt = A +  dotProduct * rayDir;

Does that make sense?

##### Share on other sites
Quote:
 Original post by ajas95Use the dot product. It's used to determine the projection of one vector onto another.say your line is connected between points A and B, and you want the closest point to P on that line.vec rayDir = (B - A).normalize();vec toP = (P - A);float dotProduct = rayDir.dot( toP );vec closestPt = A + dotProduct * rayDir;Does that make sense?
This is right, but the square root can be avoided. Try:
vec rayDir = B - A;vec toP = P - A;float t = rayDir.dot(toP) / rayDir.dot(rayDir);vec closestPt = A + t * rayDir;

##### Share on other sites
thnaks to you all, it works just fine!

• 37
• 12
• 10
• 10
• 9
• ### Forum Statistics

• Total Topics
631360
• Total Posts
2999556
×