File tree 1 file changed +61
-0
lines changed
1 file changed +61
-0
lines changed Original file line number Diff line number Diff line change
1
+ package algo ;
2
+
3
+ /**
4
+ * @ClassName MinPointToPolyline
5
+ * @Description 判断点到线段的最短距离
6
+ * @Author luokai
7
+ * @CreateDate 2020/8/16 21:57
8
+ * @UPpdateUser luokai
9
+ * @UpdateDate 2020/8/16 21:57
10
+ * @UpdateRemark
11
+ * @Version 1.0
12
+ * Copyright (c) 2019,武汉中地云申科技有限公司
13
+ * All rights reserved.
14
+ **/
15
+
16
+ import entity .Point ;
17
+
18
+ /***
19
+ * 原理分析
20
+ * 点到线段的最短距离 分为三种情况 a.点q的投影点在p1和p2的中间 b.点q的投影点在p1和p2的延长线p2端 c.点q的投影点在p1和p2延长线的p1端
21
+ * ---- ----
22
+ * (p1q p1p2)
23
+ * r = ---------------------
24
+ * ---- ----
25
+ * |p1p2||p1p2|
26
+ *
27
+ * ----
28
+ * p1p2 为p1p2的向量
29
+ *
30
+ * c为垂足
31
+ * distance = { |qc|, 0<r<1; }
32
+ * { |qp2|, r>=1; }
33
+ * { |qp1|, r<=0; }
34
+ *
35
+ */
36
+ public class MinPointToPolyline {
37
+
38
+ public double MinPointToPolyline (Point p1 , Point p2 , Point q ){
39
+ //两线段的向量点乘
40
+ double r = (p1 .getLon () - q .getLon ()) * (p1 .getLat () - q .getLat ()) + (p2 .getLon () - p1 .getLon ()) * (p2 .getLat () - p1 .getLat ());
41
+ //d 为p1p2模的平方
42
+ double d = (p2 .getLon () - p1 .getLon ()) * (p2 .getLon () - p1 .getLon ()) + (p2 .getLat () - p1 .getLat ()) * (p2 .getLat () - p1 .getLat ());
43
+ double distance ;
44
+ if (r <= 0 ){
45
+ //qp1
46
+ distance = Math .sqrt ((p1 .getLon () - q .getLon ()) * (p1 .getLon () - q .getLon ()) + (p1 .getLat () - q .getLat ()) * (p1 .getLat () -q .getLat ()));
47
+ }else if (r >= d ){
48
+ //qp2
49
+ distance = Math .sqrt ((p2 .getLon () - q .getLon ()) * (p2 .getLon () - q .getLon ()) + (p2 .getLat () - q .getLat ()) * (p2 .getLat () -q .getLat ()));
50
+ }else {
51
+ //利用点到直线的公式 cx和cy为垂足点的xy坐标
52
+ double ra = r / d ;
53
+ double cx = p1 .getLon () + (p2 .getLon () - p1 .getLon ()) * ra ;
54
+ double cy = p1 .getLat () + (p2 .getLat () - p1 .getLat ()) * ra ;
55
+ distance = Math .sqrt ((q .getLon () - cx ) * (q .getLon () - cx ) + (q .getLat () - cy ) * (q .getLat () - cy ));
56
+ }
57
+
58
+ return distance ;
59
+ }
60
+
61
+ }
You can’t perform that action at this time.
0 commit comments