pointlinedistance.java

来自「基于Java的地图数据管理软件。使用MySQL数据库管理系统。」· Java 代码 · 共 184 行

JAVA
184
字号
package net.aetherial.gis.our.auto.check.repeattrk;

/**
 * <p>Title: </p>
 *
 * <p>Description: 取得点到线段的距离</p>
 *
 * <p>Copyright: Copyright (c) 2004</p>
 *
 * <p>Company: </p>
 *
 * @author not attributable
 * @version 1.0
 */
public class PointLineDistance {
  /**
   *  ax + by + c = 0
   */
  private double a = 0.0;
  private double b = 0.0;
  private double c = 0.0;
  /**
   * 线段的斜率
   */
  private double k = 0.0;
  /**
   * 垂足坐标
   */
  private double x_cz = 0.0;
  private double y_cz = 0.0;
  /**
   * 线段的两点坐标
   */
  private double x1 = 0.0;
  private double y1 = 0.0;

  private double x2 = 0.0;
  private double y2 = 0.0;

  /**
   * 第三点坐标
   */
  private double x0 = 0.0;
  private double y0 = 0.0;
  /**
   * 取得点到线段的距离
   */
  public PointLineDistance() {
  }
  /**
   * 设置参数,端点1
   */
  public void setPoint1(double x1,double y1){
    this.x1 = x1;
    this.y1 = y1;
  }
  /**
   * 设置参数,端点2
   */
  public void setPoint2(double x2,double y2){
    this.x2 = x2;
    this.y2 = y2;
  }
  /**
   * 设置参数,端点0
   */
  public void setPoint0(double x0,double y0){
    this.x0 = x0;
    this.y0 = y0;
  }
  /**
   * 把本类里的数据,用本类的方法进行计算
   */
  public double getDistance(){
    this.setLineParam();
    this.setChuiZu();
    if (this.isInArea()) {
      return this.getDistanceOfLine();
    }else{
      return this.getDistanceOfPoint();
    }
  }

  /**
   *取得直线的参数a,b,c,以及k
   */
  private void setLineParam(){
    this.a = y2 - y1;
    this.b = x2 - x1;
    this.c = (y1 * x2) - (x1 * y2);
    this.k = (y2 - y1) / (x2 - x1);
  }

  /**
   * 取得点与两点构成直线的距离
   */
  private double getDistanceOfLine(){
    double d = 0.0;
    double base =  this.a * this.a  + this.b * this.b;
    d = Math.abs((this.a * x0 + this.b * y0 + this.c)/Math.pow(base,0.5));
    return d;
  }
  private double getDistanceOfPoint(){
    double d1 = Math.pow(((x0-x1) * (x0-x1) + (y0-y1) * (y0-y1)),0.5);
    double d2 = Math.pow(((x0-x2) * (x0-x2) + (y0-y2) * (y0-y2)),0.5);
    if(d1 < d2){
      return d1;
    }else{
      return d2;
    }
  }
  /**
   * 取得垂足的坐标
   */
  private void setChuiZu(){
    this.x_cz = (k * k * x1 + k * (y0 - y1) + x0) / (k * k + 1);
    this.y_cz = k * (this.x_cz - x1) + y1;
  }
  /**
   * 判断垂足点是否在线段的范围内
   * 如果在范围内,返回true
   */
  private boolean isInArea(){
    if (((this.x_cz <this.x1)&&(this.x_cz >this.x2))&&((this.y_cz <this.y1)&&(this.x_cz >this.y2)))
    {
      return true;
    }
    else if (((this.x_cz <this.x1)&&(this.x_cz >this.x2))&&((this.y_cz >this.y1)&&(this.x_cz <this.y2)))
    {
      return true;
    }
    else if (((this.x_cz >this.x1)&&(this.x_cz <this.x2))&&((this.y_cz <this.y1)&&(this.x_cz>this.y2)))
    {
      return true;
    }
    else if (((this.x_cz >this.x1)&&(this.x_cz <this.x2))&&((this.y_cz >this.y1)&&(this.x_cz <this.y2)))
    {
      return true;
    }
    else{
      return false;
    }
  }
  /**
   * 1.点和垂足构成的直线:
   *   直线的斜率是:k1 = -(y2-y1)/(x2-x1);  [(x2-x1) != 0;]
   *   直线经过点(x0,y0)
   *   所以直线的公式为:
   *   y -k1 * x +k1 * x0 * y0 = 0;
   *   ===>A1 = -k1, B1 = 1, C1 = k1 * x0 * y0;
   *
   *   如果x2 == x1,即(x2-x1) == 0;
   *   y = y0;
   *   ===>A1 = 0, B1 = 1, C1 = -y0;
   *
   * 2.两点构成直线
   *   直线的斜率是:k2 = (y2-y1)/(x2-x1);   [(x2-x1) != 0;]
   *   直线经过点(x1,y1)
   *   所以直线的公式为:
   *   y -k2 * x +k2 * x1 * y1 = 0;
   *   ===>A2 = -k2, B2 = 1, C2 = k2 * x1 * y1;
   *
   *   如果x2 == x1,即(x2-x1) == 0;
   *   y = x1 = x2;
   *   ===>A1 = 0, B1 = 1, C1 = -x1 = -x2;
   *
   * 3.垂足坐标
   *   y -k1 * x +k1 * x0 * y0 = 0;
   *   y -k2 * x +k2 * x1 * y1 = 0;
   *   ===>
   *   y = (A1 * C2 - C1 * A2) / (A2 * B1 - A1 * B2);
   *   x = (C2 * B1 - C1 * B2) / (A1 * B2 - A2 * B1);
   * 4.
   *
   */
  public static void main(String args[]){
    PointLineDistance pd = new PointLineDistance();
    pd.setPoint0(1,1);
    pd.setPoint1(2,2);
    pd.setPoint2(5,2);
    System.out.println("" + pd.getDistance());
  }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?