角度分析(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