Untitled

Started by
3 comments, last by Marianne 17 years, 11 months ago
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)
Advertisement
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.
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?

Quote:Original post by ajas95
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?
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;
thnaks to you all, it works just fine!

This topic is closed to new replies.

Advertisement