We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpPrograms › Finding point-line distance in 3D
Page Index Toggle Pages: 1
Finding point-line distance in 3D (Read 2875 times)
Finding point-line distance in 3D
Oct 28th, 2008, 1:30pm
 
Hi,

I've been looking everywhere on the web but I can't find a solution to this problem that is understandable for someone without a mathematical background.

I have a line segment, defined by two points with 3 coordinates:
x1, y1, z1 and x2, y2, z2

and I have a point with coordinates x,y,z

...now how do I write, or where do I get some code which gives me the distance between the point and the line segment?

I would be very glad to finally get an answer to this (simple?) question.
Re: Finding point-line distance in 3D
Reply #1 - Oct 28th, 2008, 2:54pm
 
Take a look at this solution (it's 2d but it's similar in higher dimensions)
http://local.wasp.uwa.edu.au/~pbourke/geometry/pointline/
Re: Finding point-line distance in 3D
Reply #2 - Oct 28th, 2008, 3:12pm
 
the class "Rod" of my lib "Loc" will be an answer.
a sample sketch is
http://www.geocities.jp/classiclll_newweb/RodTest/applet/index.html

if you are interested, see
http://sourceforge.jp/projects/wrj4p5/wiki/Loc%28en%29
Re: Finding point-line distance in 3D
Reply #3 - Oct 28th, 2008, 3:13pm
 
Thanks Eskimoblood, I've indeed heard that calculating a line-point distance in 3d should be similar to calculating such a distance in 2d.

I've also managed to solve this kind of problem in 2d, but I have no clue what a "similar" calculation in 2d would look like. In which respects would the algorithms look alike?
Re: Finding point-line distance in 3D
Reply #4 - Oct 28th, 2008, 4:46pm
 
Thanks for the help! But strangely, with a pen and paper I got everything solved more quickly then with trying to implement or adjust solutions that were already on the web.

The resulting code is this:


float distPL3D (float[] p, float[] lB, float[] lE) // arrays each consisting of 3 floats: point, line begin and line end
{
   float sqrP_LB = sqrDistPP3D(p, lB);
   float sqrP_LE = sqrDistPP3D(p, lE);
   float sqrLB_LE = sqrDistPP3D(lB, lE);
   float LB_LE = sqrt(sqrLB_LE);
   float I_LB = (sqrP_LB + sqrLB_LE - sqrP_LE)/(2*LB_LE);
   if (I_LB < 0 ) return sqrt(sqrP_LB); // intersection point is before beginning line segment
   if (I_LB > LB_LE ) return sqrt(sqrP_LE); // intersection point is behind end line segment
/*    use this part if you want to know what the closest point on the line is.
   float u = I_LB/LB_LE;
   float[] closestPoint=  new float[]
   {
     lB[0]+ u*(lE[0]-lB[0]),
     lB[1]+ u*(lE[1]-lB[1]),
     lB[2]+ u*(lE[2]-lB[2])
   };
*/
   float distance = sqrt(sqrP_LB - I_LB*I_LB);
   return distance;
}
 
float sqrDistPP3D(float[] v1, float[] v2) {
     return (v1[0] - v2[0]) * (v1[0] - v2[0]) +
            (v1[1] - v2[1]) * (v1[1] - v2[1]) +
            (v1[2] - v2[2]) * (v1[2] - v2[2]);
}

Re: Finding point-line distance in 3D
Reply #5 - Nov 4th, 2008, 11:49pm
 
Hey Classiclll,

It's a little while later now and I've been struggling with loads of more 3d geometry problems. Just when I was about to torture cute animals just to watch them suffer I looked at your "Rod" class again, this time more closely, and I'm quite impressed! This class contains answers to a huge amount of very tricky problems. Thanks for the amount of effort you put in there and letting me use this. You have not only saved my life, but also that of countless cute animals...
Re: Finding point-line distance in 3D
Reply #6 - Nov 19th, 2009, 5:52pm
 
the package "Loc" doesn't depend on Processing, so you can use "Loc"'s staff in your ordinal java projects.
any feedbacks are welcomed。
pls enjoy.
Page Index Toggle Pages: 1