点至直线的距离和垂足点计算
//点到直线的垂足点
public static Coordinate getFootPoint(Coordinate point, Coordinate pnt1, Coordinate pnt2) { double A=pnt2.y-pnt1.y; //y2-y1 double B=pnt1.x-pnt2.x; //x1-x2; double C=pnt2.x*pnt1.y-pnt1.x*pnt2.y; //x2*y1-x1*y2 if (A * A + B * B < 1e-13) { return pnt1; //pnt1与pnt2重叠 } else if (Math.abs(A * point.x + B * point.y + C) < 1e-13) { return point; //point在直线上(pnt1_pnt2) } else { double x = (B * B * point.x - A * B * point.y - A * C) / (A * A + B * B); double y = (-A * B * point.x + A * A * point.y - B * C) / (A * A + B * B); Coordinate fpoint = new Coordinate(); fpoint.x = x; fpoint.y = y; return fpoint; } } public static double getDistancePoineToLine_planeCoord(Coordinate point, Coordinate pnt1, Coordinate pnt2) { //平面坐标中 double A=pnt2.y-pnt1.y; //y2-y1 double B=pnt1.x-pnt2.x; //x1-x2; double C=pnt2.x*pnt1.y-pnt1.x*pnt2.y; //x2*y1-x1*y2 if (A * A + B * B < 1e-13) { //pnt1与pnt2重叠 double dx = point.x - pnt1.x; double dy = point.y - pnt1.y; return Math.sqrt(dx * dx + dy * dy); } else if (Math.abs(A * point.x + B * point.y + C) < 1e-13) { return 0; //point在直线上(pnt1_pnt2) } else { double distance = Math.abs(A * point.x + B * point.y + C) / Math.sqrt(A * A + B * B); return distance; } } public static double getDistance_wgs84(Coordinate p1, Coordinate p2) { return getDistance_wgs84(p1.x,p1.y,p2.x,p2.y); } //计算两经纬度点的距离,单位米 public static double getDistance_wgs84(double p1_x,double p1_y, double p2_x,double p2_y) { double lat1=p1_x; double lon1=p1_y; double lat2=p2_x; double lon2=p2_y; // double earch_radius=6371008.8; // 地球半径 平均值 米 //用haversine公式计算球面两点间的距离。 //经纬度转换成弧度 double h_lat1=lat1*Math.PI/180.0; double h_lon1=lon1*Math.PI/180.0; double h_lat2=lat2*Math.PI/180.0; double h_lon2=lon2*Math.PI/180.0; //差值 double vlon=Math.abs(h_lon1-h_lon2); double vlat=Math.abs(h_lat1-h_lat2); // double h=HaverSin(vlat)+Math.cos(h_lat1)*Math.cos(h_lat2)*HaverSin(vlon); // double d=2*earch_radius*Math.asin(Math.sqrt(h)); // return d; } public static double HaverSin(double theta) { double v=Math.sin(theta/2); return v*v; };12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273//点到直线的距离 public static double getDistanceByPointToLine_笛卡尔坐标(Coordinate point, Coordinate pnt1, Coordinate pnt2) { double dis = 0; if (pnt1.x == pnt2.x) { if (pnt1.y == pnt2.y) { double dx = point.x - pnt1.x; double dy = point.y - pnt1.y; dis = Math.sqrt(dx * dx + dy * dy); } else dis = Math.abs(point.x - pnt1.x); } else { double lineK = (pnt2.y - pnt1.y) / (pnt2.x - pnt1.x); double lineC = (pnt2.x * pnt1.y - pnt1.x * pnt2.y) / (pnt2.x - pnt1.x); dis = Math.abs(lineK * point.x - point.y + lineC) / (Math.sqrt(lineK * lineK + 1)); } // return dis; }---------------------