角度分析(Angle):
com.vividsolutions.jts.algorithm.Angle
Coordinate tip1 = new Coordinate(0,0);Coordinate tail = new Coordinate(1,1);Coordinate tip2 = new Coordinate(4,3);Angle angle = new Angle();//返回兩個無向最小差異角度,範圍 [0, 180]double radia1 = angle.angle(tip1, tail);double radia2 = angle.angle(tail, tip2);System.out.println(angle.toDegrees(angle.diff(radia1, radia2)));//返回兩個向量之間的最小夾角,範圍[0,180]double ang1 = angle.angleBetween(tip1, tail, tip2);System.out.println(angle.toDegrees(ang1));//從angle到angle按什麼方向旋轉// public static final int CLOCKWISE = -1; 順時針//public static final int COUNTERCLOCKWISE = 1; 逆時針System.out.println(angle.getTurn(radia1, radia2));// 判斷從tip1->tail->tip2的夾角是否是銳角System.out.println(angle.isAcute(tip1, tail,tip2));// 判斷從tip1->tail->tip2的夾角是否是鈍角System.out.println(angle.isObtuse(tip1, tail,tip2));//角度->弧度的轉換System.out.println(angle.toDegrees(Math.PI));//弧度->角度的轉換System.out.println(angle.toRadians(180));
輸出結果:
11.309932474020213
168.6900675259798
-1
false
true
180.0
3.141592653589793
幾何的基礎演算法(CGAlgorithms):
com.vividsolutions.jts.algorithm.CGAlgorithms
GeometryFactory.java
import com.vividsolutions.jts.geom.Geometry;import com.vividsolutions.jts.io.ParseException;import com.vividsolutions.jts.io.WKTReader;public class GeometryFactory {private WKTReader reader;private static GeometryFactory instance = null;public static synchronized GeometryFactory getInstance() {if (instance == null) {instance = new GeometryFactory();}return instance;}public void getReader() {reader = new WKTReader();}public Geometry buildGeo(String str) {try {if (reader == null) {reader = new WKTReader();}return reader.read(str);} catch (ParseException e) {throw new RuntimeException("buildGeometry Error", e);}}}
private static GeometryFactory factory = GeometryFactory.getInstance();//定義環Geometry g = factory.buildGeo("LINESTRING (0 0,1 1,2 2, 5 5,0 5,0 0)");Coordinate[] coords = g.getCoordinates();//點在環上System.out.println(CGAlgorithms.isPointInRing(new Coordinate(4,4), coords));//點在環內System.out.println(CGAlgorithms.isPointInRing(new Coordinate(1,2), coords));//計算線段AB到CD的最短距離System.out.println(CGAlgorithms.distanceLineLine(new Coordinate(1,1), new Coordinate(2,2), new Coordinate(2,0), new Coordinate(3,0)));//計算點A到CD的最短距離System.out.println(CGAlgorithms.distancePointLine(new Coordinate(0,0), new Coordinate[]{new Coordinate(1,1), new Coordinate(2,0), new Coordinate(2,2), new Coordinate(1,1)}));//環是否是逆時針旋轉導向System.out.println(CGAlgorithms.isCCW(new Coordinate[]{new Coordinate(1,1), new Coordinate(2,0), new Coordinate(2,2), new Coordinate(1,1)}));//點是否線上段上System.out.println(CGAlgorithms.isOnLine(new Coordinate(2,1),new Coordinate[]{new Coordinate(2,0), new Coordinate(2,2), new Coordinate(1,1)}));//求兩線的交點,返回的是座標點。HCoordinate hcoord = new HCoordinate(new Coordinate(0,0), new Coordinate(5,5),new Coordinate(0,4),new Coordinate(4,0));try {System.out.println(hcoord.getX());System.out.println(hcoord.getY());} catch (NotRepresentableException e1) {//拋出此異常,說明兩條線段平行。e1.printStackTrace();}//等價HCoordinate hd = new HCoordinate();try {System.out.println(hd.intersection(new Coordinate(0,0), new Coordinate(5,5),new Coordinate(0,4),new Coordinate(4,0)));} catch (NotRepresentableException e) {//拋出此異常,說明兩條線段平行。e.printStackTrace();}//判斷點(Point) 和對象(Geometry) 之間的關係//public final static int INTERIOR = 0; 在內部//public final static int BOUNDARY = 1; 在邊界上//public final static int EXTERIOR = 2; 在外部//public final static int NONE = -1;PointLocator pl = new PointLocator();//線上上System.out.println(pl.locate(new Coordinate(0,0), factory.buildGeo("LINESTRING (0 0,1 1,2 2, 5 5,0 5)")));//在面上System.out.println(pl.locate(new Coordinate(0,0), factory.buildGeo("POLYGON ((0 0,1 1,2 2,5 5,0 5,0 0))")));//在面內System.out.println(pl.locate(new Coordinate(1,3), factory.buildGeo("POLYGON ((0 0,1 1,2 2,5 5,0 5,0 0))")));//在面外System.out.println(pl.locate(new Coordinate(10,10), factory.buildGeo("POLYGON ((0 0,1 1,2 2,5 5,0 5,0 0))")));
輸出結果:
true
true
1.4142135623730951
1.4142135623730951
true
true
2.0
2.0
(2.0, 2.0, NaN)
1
1
0
2