View previous topic :: View next topic |
Author |
Message |
Kimi
Joined: 30 Jan 2005 Posts: 23 Location: Argentina
|
Distance between points |
Posted: Wed Feb 23, 2005 11:19 am |
|
|
Hello
I have 3 points with 3 coordinates each one (X,Y,Z). This coordinates are float points number.
Suppose P1, P2, and P3. I need to calculate the ortogonal distance (the shortest distance) between P3 and a line generated by the point P1 and P2.
Any idea of how I can do this this PIC C?
Thanks!
Kimi |
|
|
Mark
Joined: 07 Sep 2003 Posts: 2838 Location: Atlanta, GA
|
|
Posted: Wed Feb 23, 2005 5:26 pm |
|
|
The same way you would with a C program running on a PC. It will just be a bit slower. |
|
|
Douglas Kennedy
Joined: 07 Sep 2003 Posts: 755 Location: Florida
|
|
Posted: Wed Feb 23, 2005 9:08 pm |
|
|
I guess it's in the math so here goes
The points p1 p2 p3 are always coplanar so they form a simple triangle in 2 dimensions
Let the lengths a=distance p1 to p2 b=distance p2 to p3 and c distance p1 to p3
Your triangle now has sides a, b, and c.
Now Calculate S, where S = (a+b+c)/2
So the the area of your Triangle is SQRT(s*(s-a)(s-b)(s-c))
Let x be the length of the orthoganal line between p1 p2 to the point p3
then x represents the height of your triangle p1 p2 p3 so it's area is (height times base)/2
you're almost done
So a.x/2=sqrt(s*(s-a)(s-b)(s-c)) and now you can solve for x the distance you wanted.
Note:
to get the distance d ( p1 to p2) and you have p1 is(x1,y1.z1) and p2 is(x2,y2,z2)
you have d=SQRT( (x1-x2)^2 +(y1-y2)^2+(z1-z2)^2)
Now I'm a fan of the CORDIC which can be thought of as binary angles
the angles are those with tangents of 1 1/2 1/4 1/8 and so on
any angle can be expressed as a sum of these fundamental binary angles
Then by shifting the points representing an object can be rotated in space in this way the plane formed by p1 p2 p3 is rotated into the XY plane by rotational geometry changes and the calculation is reduced to shifts adds and subtractions.
You have cartesians and floats so expect a lot of number crunching on the PIC as it computes the above
Hope I'm not doing your homework assignment for you. |
|
|
Mark
Joined: 07 Sep 2003 Posts: 2838 Location: Atlanta, GA
|
|
Posted: Wed Feb 23, 2005 9:38 pm |
|
|
Douglas Kennedy wrote: | Hope I'm not doing your homework assignment for you. |
Thus my short response |
|
|
Kimi
Joined: 30 Jan 2005 Posts: 23 Location: Argentina
|
|
Posted: Thu Feb 24, 2005 5:02 am |
|
|
Wouw! Yesterday I was trying to find a relationship between sides of the trieangle, but never take in consideration the Area. Thanks very much!
I will test it today!
Thanks!!!
Kimi |
|
|
|