Skip to content

Commit 0bc57d6

Browse files
committed
GIS基本算法--02判断折线段的第二段的拐向
1 parent 57d4f68 commit 0bc57d6

File tree

2 files changed

+72
-0
lines changed

2 files changed

+72
-0
lines changed

src/algo/PolylineDirection.java

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package algo;
2+
3+
/**
4+
* @ClassName PolylineDirection
5+
* @Description 判断折线的方向
6+
* @Author luokai
7+
* @CreateDate 2020/8/16 17:37
8+
* @UPpdateUser luokai
9+
* @UpdateDate 2020/8/16 17:37
10+
* @UpdateRemark
11+
* @Version 1.0
12+
* Copyright (c) 2019,武汉中地云申科技有限公司
13+
* All rights reserved.
14+
**/
15+
16+
import entity.Point;
17+
18+
/**
19+
* 折线段的拐向判断方法可以直接由矢量叉积的性质推出。对于有公共端点的线段p0p1和p1p2,通过计算(p2 - p0)× (p1 - p0)的符号便可以确定折线段的拐向:
20+
* 若(p2 - p0)× (p1 - p0) > 0,则p0p1在p1点拐向右侧后得到p1p2。
21+
* 若(p2 - p0)× (p1 - p0) < 0,则p0p1在p1点拐向左侧后得到p1p2。
22+
* 若(p2 - p0)× (p1 - p0) = 0,则p0、p1、p2三点共线
23+
* 左侧 右侧是指 相对于po---p1方向
24+
*/
25+
public class PolylineDirection {
26+
private final String right = "右拐";
27+
private final String left = "左拐";
28+
private final String line = "共线";
29+
30+
public String PolylineDirection(Point p0, Point p1, Point p2){
31+
32+
String location = null;
33+
if((p2.getLon() - p0.getLon()) * (p1.getLat() - p0.getLat()) - (p2.getLat() - p0.getLat()) * (p1.getLon() - p0.getLon()) > 0){
34+
location = right;
35+
}else if((p2.getLon() - p0.getLon()) * (p1.getLat() - p0.getLat()) - (p2.getLat() - p0.getLat()) * (p1.getLon() - p0.getLon()) < 0){
36+
location = left;
37+
}else if((p2.getLon() - p0.getLon()) * (p1.getLat() - p0.getLat()) - (p2.getLat() - p0.getLat()) * (p1.getLon() - p0.getLon()) == 0){
38+
location = line;
39+
}
40+
return location;
41+
}
42+
43+
}

src/test/Test02.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package test;
2+
3+
import algo.PolylineDirection;
4+
import entity.Point;
5+
6+
/**
7+
* @ClassName Test02
8+
* @Description TODO
9+
* @Author luokai
10+
* @CreateDate 2020/8/16 18:11
11+
* @UPpdateUser luokai
12+
* @UpdateDate 2020/8/16 18:11
13+
* @UpdateRemark
14+
* @Version 1.0
15+
* Copyright (c) 2019,武汉中地云申科技有限公司
16+
* All rights reserved.
17+
**/
18+
public class Test02 {
19+
public static void main(String args[]){
20+
Point p0 = new Point(10,20);
21+
Point p1 = new Point(12,20);
22+
Point p2 = new Point(15,30);
23+
24+
PolylineDirection polylineDirection = new PolylineDirection();
25+
String text = polylineDirection.PolylineDirection(p0, p1, p2);
26+
System.out.println(text);
27+
}
28+
29+
}

0 commit comments

Comments
 (0)