@Test public void testCenter() { // test for rectangle final Polygon rectangle = new Polygon(Rectangle.TEST_RECTANGLE); final Location center = rectangle.center(); Assert.assertEquals(center, Location.forString("37.3292805,-122.030478")); // test for triangle final Polygon triangle = new Polygon(Arrays.asList(Location.forString("1,1"), Location.forString("2,2"), Location.forString("1,2"))); final Location triangleCenter = triangle.center(); Assert.assertEquals(triangleCenter, Location.forString("1.3333333,1.6666667")); }
/** * Triangulate this {@link Polygon}, using the JTS library. * * @return All the triangles that form this {@link Polygon}. */ public List<Polygon> triangles() { final ConformingDelaunayTriangulationBuilder trianguler = new ConformingDelaunayTriangulationBuilder(); // Populate the delaunay triangulation builder trianguler.setSites(JTS_POLYGON_CONVERTER.convert(this)); final GeometryCollection triangleCollection = (GeometryCollection) trianguler .getTriangles(JtsPrecisionManager.getGeometryFactory()); // Get the output and convert back to Core Polygons, filter out the extraneous polygons from // the Delaunay triangulation. return Iterables.stream(GeometryStreamer.streamPolygons(triangleCollection)) .map(JTS_POLYGON_CONVERTER.revert()) .filter(polygon -> fullyGeometricallyEncloses(polygon.center())).collectToList(); }