distance point to line java

Solutions on MaxInterview for distance point to line java by the best coders in the world

showing results for - "distance point to line java"
Rudolph
03 Oct 2019
1//returns the distance between the infinite line(x1,y1)(x2,y2) and a point(x,y)
2public float pDistance(float x, float y, float x1, float y1, float x2, float y2) {
3
4      float A = x - x1; // position of point rel one end of line
5      float B = y - y1;
6      float C = x2 - x1; // vector along line
7      float D = y2 - y1;
8      float E = -D; // orthogonal vector
9      float F = C;
10
11      float dot = A * E + B * F;
12      float len_sq = E * E + F * F;
13
14      return (float) Math.abs(dot) / Math.sqrt(len_sq);
15    }