• Welcome to the new COTI server. We've moved the Citizens to a new server. Please let us know in the COTI Website issue forum if you find any problems.
  • We, the systems administration staff, apologize for this unexpected outage of the boards. We have resolved the root cause of the problem and there should be no further disruptions.

Need an equation for the distance from a point to a line

ffilz

SOC-12
I need a formula for the distance from a point to the nearest point on a line in three dimensional space.

My line is defined by two sets of coordinates p1 (x1, y1, z1), p2 (x2, y2, z2).

The point is defined by a third set of coordinates p3 (x3, y3, z3).

My actual interest is in determining if p3 is within the cylinder defined by the line segment p1, p2, radius r.

Since the actual goal is finding stars in a 3-d field that are roughly "between" star 1 (p1) and star 2 (p2), given the distance from p3 to the line is <= r, I can determine the distance p3 is from each of p1 and p2, and if both are less than the distance between p1 and p2, then it is "roughly between". The shape defined by these criteria isn't quite a cylinder, but it's actually closer to what I want.

Possibly what I really want is some kind of ovoid or two intersecting cones or something.

Thanks for your help

Frank
 
Possible solution

If a problem is really hard, redefine the parameters of your problem. In this case. reset your coordinate space.

Define one of the origin points of the line as the (0,0,0) point of your coordiante space. Define the other point as lying on one of the axes.

Then transform the coordinates of the third point to the new system.

The problem then becomes trivially easy.
 
BeRKA, I've had another reference to that wolfram page, the next question is how to do those vector operations...

Does anyone know if there is a gcc library for vector functions?

Thanks

Frank
 
Good thing this is not homework

I thought that I was making things simpler.

I have checked out the wolfram site.

I have made a spreadsheet that does the maths for you.

I make no guarantees of its accuracy. It is something "I whipped up".

Test it yourself.
 

Attachments

Hmm, I tried it out, but one simple test seems to not be the answer I expected.

Line is defined as (0,0,0)-(10,10,10)
Point off line is defined as (10,0,0)

Length of line is sqrt(300) = 17.32

I would expect the distance from the point to the line to be sqrt(300)/2 = 8.66 since the line is the diagonal of a cube, the point is another vertex of the cube, I would expect the shortest distance to the line to be along the diagonal of the cube that runs from (10,0,0) to (0,10,10), which should intersect in the center of the cube.

The formula in the spreadsheet gives 8.16.

Frank
 
Bloody Geomtery

I think you are mistaken. You have made me learn more geometry than I did in highschool.

I just went back to the spreadsheet and tried a different calc.

I used the three points as points of a triangle. The distances between the points become the sides of the triangle. You will note that the triangle has 3 unequal sides.

P0 (10,10,0)
P1 (0,0,0)
P2 (10,10,10)

Side b P0 => P1 = 14.14214
Side a P0 => P2 = 10
Side c P1 => P2 = 17.32051

angle alpha = .615 radians by the law of cosines

The area = 70.710 by b*c*sin(alpha)/2

The height = 8.16 by 2*Area/Base

Your logic has seductive appeal. I do not know enough geometry to fault it. The output is not supported by the equations I have been able to implement. The two different implementations are quite different. Maybe I have put in the wrong sides into the calcs. I tried to move the point values around. I could not get the answer you came up with.

I have attached the new spreadsheet with the second implementation.
 

Attachments

Ok, looking at it carefully in those terms, I see how it works out. It's one of those things where the solution that seems logical isn't actually the correct solution.

I just came up with a simple way to show that the diagonal line segments that run from opposing vertices of a cube can't meet at right angles (and therefore do not represent the shortest distance from any vertex not on a part of a particular line segment). A cube has 8 vertices, and therefore, there are 4 diagonal line segments running between opposing vertices. The 4 line segments do all meet in the center of the cube. They can't meet at 90 degree angles since only three lines can meet at a single point, and each being at 90 degrees to the other two...

Therefore there must be a shorter distance between any of the six vertices not on a particular line segment (since the other six vertices must be the points on the surface of the cube farthest from the line segment connecting two opposing vertices).

Thanks for the help on this.

Frank
 
Determining Distance Between Two Cartesian Points

r = SQR(X^2 + Y^2 +Z^2)

Where {X,Y,Z} is the difference between the point of origin and the point of destination.

Thus {0,0,0} to {10,10,10,} is:

SQR(10^2 + 10^2 + 10^2) = SQR(300) = 17.32

Also

{-4,20,17} to {5,5,2} is:

SQR(9^2 + 15^2 + 19^2) = SQR(81 + 225 + 361) = SQR(667) = 25.83

It's no more complex than that.
 
I thought that I was making things simpler.

I have checked out the wolfram site.

I have made a spreadsheet that does the maths for you.

I make no guarantees of its accuracy. It is something "I whipped up".

Test it yourself.

Thanks again. I finally had a chance to use this formula. After I converted it into C code, it worked quite nicely for me and I have been able to select stars from the HYG database to use as stopovers between two arbitrary stars in my setup.

The results of everything I've done are documented on my Traveller Page.

Frank
 
re: Also

{-4,20,17} to {5,5,2} is:

SQR(9^2 + 15^2 + 19^2) = SQR(81 + 225 + 361) = SQR(667) = 25.83

Don't you mean, adjusting our value for z,

{-4,20,17} to {5,5,-2} is:

SQR(9^2 + 15^2 + 19^2) = SQR(81 + 225 + 361) = SQR(667) = 25.83?
 
Just to be clear, I didn't need a formula for the distance between two points, I needed a formula for the shortest distance between and point and a line defined by a second and third point in space.

Frank
 
I can give you a process for finding it; I can't reduce it to a formula because it's qualitative as I express it here, not quantitative. However:

Three points define a plane. Once you can define the plane, you reduce the problem to a planar problem - instead of the three points having coordinates of the form {x,y,z}, they have the alternate coordinates, relative to the defined plane, of {x',y'}. This is the hard part of the problem.

The two points that define the line that you are interested in finding the shortest distance to the third point from give you enough information to define the entire line using a formula of the form y'=mx'+k.

Once you have that formula, the shortest distance to the third point from that line will be on a line that is perpendicular to the original line, and which passes through the point. That line can be expressed using a formula of the form y'=(1/m)x'+j. The one point that is on both of those lines is the point on the line that is closest to the third point, and it should then be a trivial exercise for the student to determine the actual distance.
 
Getting to defining FT's plane, given line dscribed by points A & B and Point C , find distance AB, distance BC, and distance AC, and this gives you three legs of a triangle.

AB should, BTW, be the LONG arm. Sraw that, set your compass to distance AC and trace an arc; set to BC, and trace an overlapping arc, and then measure the height from line ab of the intersect.

There should be a conversion from those lengths to formulaic points, but I don't know them.
 
And to add one more clarification, JustinOz provided an xls with a formula back on page one. It works just fine for me.

Thanks for all the suggestions.

Frank
 
Back
Top