Distance between point and a line passing through other two points
On 2/7/18 6:06 AM, Dhananjay 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,
> -- Joshi
This sounds a lot more like a math problem then a python problem (unless
python has a geometry package to use for this).
The 3D solution is very close to the 2D version, if you draw the angle
p0 -> p1 -> p2 and take the cross product of the vectors p1-p0 and p2-p1
the result will be a vector perpendicular to plane defined by p0, p1, p2
with a magnitude of
|p1-p0| * |p2-p1| * sin(angle between line (p1,p0) and (p2,p1))
The distance you want is |p1-p0| * sin(angle between line (p1,p0) and
With a bit of thought you should be able to get the answer.