点至直线的距离和垂足点计算
//点到直线的垂足点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;
};
页:
[1]