JTS algorithm package

來源:互聯網
上載者:User

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.