📜  距离点到线java代码示例

📅  最后修改于: 2022-03-11 14:52:07.688000             🧑  作者: Mango

代码示例1
//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);
    }