求点到线之间垂线的坐标点

package test;

public class Test {

public static void main(String[] args) {

    Point point = new Point();
    point.setLon(104.17989);
    point.setLat(30.74799);

    Point point1 = new Point();
    point1.setLon(103.80074);
    point1.setLat(30.82649);

    Point point2 = new Point();
    point2.setLon(104.13603);
    point2.setLat(30.77198);
    double[] res = getFootPoint(point,point1,point2);
    System.out.println(res[0]+","+res[1]);

}
public static double[] getFootPoint(Point point,Point point1,Point point2){
    double x0 = point.getLon();
    double y0 = point.getLat();
    double x1 = point1.getLon();
    double y1 = point1.getLat();
    double x2 = point2.getLon();
    double y2 = point2.getLat();

    double k1 = (y1-y2)/(x1-x2) * 1.0;
    double b1 = y1 - k1 * x1;

    double k2 = -1/k1 * 1.0;
    double b2 = y0 - k2 * x0;
    double xn = (b2-b1) / (k1-k2);
    double yn = k2*xn+b2;
    double[] abc = {xn,yn};
    return abc;
}

}

你可能感兴趣的:(java,算法,开发语言)