Distance between point and a line passing through other two points

Chris Angelico rosuav at gmail.com
Wed Feb 7 06:39:03 EST 2018


On Wed, Feb 7, 2018 at 10:06 PM, Dhananjay <dhananjay.c.joshi at gmail.com> wrote:
> Hello all,
>
> I have 3 points with coordinates (x0,y0,z0), (x1,y1,z1) and (x2,y2,z2).
> I also have a line joining points (x1,y1,z1) and (x2,y2,z2).
> For example,
> p0=[5.0, 5.0, 5.0]
> p1=[3.0, 3.0, 3.0]
> p2=[4.0, 4.0, 4.0]
>
> a = np.array(p0)
> b = np.array(p1)
> c = np.array(p2)
>
> I want to write a script that can calculate shortest distance d between
> point (x0,y0,z0) and the line((x1,y1,z1), (x2,y2,z2)).
> In other words,
> d = distance(a, line(b,c))
> Since I have information of the coordinates of these points only, I am not
> sure how to put it into python script to get distance d.
>
> On searching Internet, some solutions are discussed for 2D coordinates
> (i.e. for (x0,y0), (x1,y1) and (x2,y2) ).
> However, I need solution for 3D coordinates.
>
> Any direction or suggestion would be great help.
> Thanking you in advance,

With a line and a point not on that line, you can find exactly one
plane that contains them all. (If the point is on the line, well, its
distance is zero and nothing else matters. Your example points aren't
particularly interesting, as the line through (3,3,3) and (4,4,4) also
passes through (5,5,5).) So ultimately, this is solved the same way.

Geometrically, you find the distance between a point and a line by
dropping a perpendicular to that line and measuring that distance.
Imagine drawing a triangle between the three points, dropping a perp,
and measuring it off. Or type "distance point to line three
dimensions" into your favourite web search engine and find a formula
that way. Either way, once you have the mathematics settled, it should
be fairly easy to translate that into Python.

Enjoy!

ChrisA



More information about the Python-list mailing list