Distance between point and a line (from two points)
For the above-mentioned answers to work, the points need to be numpy arrays, here's a working example:
import numpy as np
p1=np.array([0,0])
p2=np.array([10,10])
p3=np.array([5,7])
d=np.cross(p2-p1,p3-p1)/np.linalg.norm(p2-p1)
np.cross
returns the z-coordinate of the cross product only for 2D vectors. So the first norm
in the accepted answer is not needed, and is actually dangerous if p3
is an array of vectors rather than a single vector. Best just to use
d=np.cross(p2-p1,p3-p1)/norm(p2-p1)
which for an array of points p3
will give you an array of distances from the line.
Try using the norm function from numpy.linalg
d = norm(np.cross(p2-p1, p1-p3))/norm(p2-p1)