Shortest distance between point and line java code example
Example 1: distance point to line java
//returns the distance between the infinite line(x1,y1)(x2,y2) and a point(x,y)
public float pDistance(float x, float y, float x1, float y1, float x2, float y2) {
float A = x - x1; // position of point rel one end of line
float B = y - y1;
float C = x2 - x1; // vector along line
float D = y2 - y1;
float E = -D; // orthogonal vector
float F = C;
float dot = A * E + B * F;
float len_sq = E * E + F * F;
return (float) Math.abs(dot) / Math.sqrt(len_sq);
}
Example 2: distance of a point from a line python
d = norm(np.cross(p2-p1, p1-p3))/norm(p2-p1)