public int nextVertexIndex(final int currentVertexIndex) { verifyVertexIndex(currentVertexIndex); if (currentVertexIndex == size() - 1) { return 0; } else { return currentVertexIndex + 1; } }
private void verifyVertexIndex(final int index) { if (index < 0 || index >= size()) { throw new CoreException("Invalid Vertex Index {}.", index); } } }
public int previousVertexIndex(final int currentVertexIndex) { verifyVertexIndex(currentVertexIndex); if (currentVertexIndex == 0) { return size() - 1; } else { return currentVertexIndex - 1; } }
/** * Remove a vertex * * @param index * The index of the vertex to remove * @return The new {@link Polygon} without the specified vertex */ public Polygon withoutVertex(final int index) { if (index < 0 || index >= this.size()) { throw new CoreException("{} is not a vertex index of {}", index, this); } final List<Location> vertices = Iterables.asList(this); vertices.remove(index); return new Polygon(vertices); }
/** * The segments that belong to this {@link Polygon} that are attached to this vertex * * @param vertexIndex * the index of the vertex * @return The segments that belong to this {@link Polygon} that are attached to this vertex */ public List<Segment> attachedSegments(final int vertexIndex) { verifyVertexIndex(vertexIndex); final List<Segment> result = new ArrayList<>(); // Previous if (vertexIndex > 0) { result.add(segmentForIndex(vertexIndex - 1)); } else { result.add(segmentForIndex(size() - 1)); } // Next result.add(segmentForIndex(vertexIndex)); return result; }
|| this.size() < expectedNumberOfSides)
private java.awt.Polygon awtPolygon() { if (this.awtPolygon == null) { final int size = size(); final int[] xArray = new int[size]; final int[] yArray = new int[size]; int index = 0; for (final Location location : this) { xArray[index] = (int) location.getLongitude().asDm7(); yArray[index] = (int) location.getLatitude().asDm7(); index++; } this.awtPolygon = new java.awt.Polygon(xArray, yArray, size); } return this.awtPolygon; }
@Test public void testTriangulate() { final Polygon polygon = Polygon.SILICON_VALLEY_2; final List<Polygon> triangles = polygon.triangles(); Assert.assertEquals(3, triangles.size()); for (final Polygon triangle : triangles) { Assert.assertEquals(3, triangle.size()); } }
@Test public void testClosedLoop() { final Polygon polygon = Polygon.SILICON_VALLEY; final Polygon initialClosedLoop = new Polygon(polygon.closedLoop()); Assert.assertNotEquals( "Last and Last - 1 locations for the polygon should not be duplicate.", initialClosedLoop.last(), initialClosedLoop.get(initialClosedLoop.size() - 2)); final Polygon doubleClosedLoop = new Polygon(initialClosedLoop.closedLoop()); Assert.assertNotEquals("Last and Last - 1 locations for polygon should not be duplicate.", doubleClosedLoop.last(), doubleClosedLoop.get(doubleClosedLoop.size() - 2)); }