点至直线的距离和垂足点计算
Posted ly570
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了点至直线的距离和垂足点计算相关的知识,希望对你有一定的参考价值。
点至直线的距离和垂足点计算
//点到直线的垂足点
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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
//点到直线的距离 (http://www.my516.com)
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;
---------------------
以上是关于点至直线的距离和垂足点计算的主要内容,如果未能解决你的问题,请参考以下文章