/** * Creates a {@link WritablePolygon2D} from a list of (x, y) coordinates, * with {@link BoundaryType#UNSPECIFIED unspecified} * {@link MaskPredicate#boundaryType() boundaries}. */ public static WritablePolygon2D polygon2D( final double[] x, final double[] y ) { return new DefaultWritablePolygon2D( x, y ); }
private void updateMinMax() { min[ 0 ] = min[ 1 ] = Double.POSITIVE_INFINITY; max[ 0 ] = max[ 1 ] = Double.NEGATIVE_INFINITY; for ( int i = 0; i < numVertices(); i++ ) { final double px = x.get( i ); final double py = y.get( i ); expandMinMax( px, py, px, py ); } }
/** * If the given vertex has more than 2 dimensions, the higher dimensions * will be ignored. */ @Override public void addVertex( final int index, final RealLocalizable vertex ) { final double px = vertex.getDoublePosition( 0 ); final double py = vertex.getDoublePosition( 1 ); x.insert( index, px ); y.insert( index, py ); expandMinMax(px, py, px, py); }
/** * Creates a 2D polygon with the provided vertices. * * @param vertices * List of vertices which will be copied. Each vertex should have * a position in 2D space, positions beyond 2D will be ignored. */ public DefaultWritablePolygon2D( final List< ? extends RealLocalizable > vertices ) { // Regions.getBoundsReal(...) could create an interval with n > 2, if // the first vertex had n > 2. Instead create 2D interval, and then set // min/max. super( 2 ); x = new VertexList( vertices.size() ); y = new VertexList( vertices.size() ); populateXY( vertices ); }
@Override public void removeVertex( final int index ) { x.removeAt( index ); y.removeAt( index ); updateMinMax(); }
@Override public void addVertices( int index, Collection< RealLocalizable > vertices ) { x.makeRoom( index, vertices.size() ); y.makeRoom( index, vertices.size() ); int offset = index; for ( final RealLocalizable vertex : vertices ) { x.setQuick( offset, vertex.getDoublePosition( 0 ) ); y.setQuick( offset, vertex.getDoublePosition( 1 ) ); offset++; } final RealInterval bounds = GeomMaths.getBoundsReal( vertices ); expandMinMax( bounds.realMin( 0 ), bounds.realMin( 1 ), bounds.realMax( 0 ), bounds.realMax( 1 ) ); }
/** * Creates a {@link WritablePolygon2D} from a list of vertices, with * {@link BoundaryType#UNSPECIFIED unspecified} * {@link MaskPredicate#boundaryType() boundaries}. */ public static WritablePolygon2D polygon2D( final List< ? extends RealLocalizable > vertices ) { return new DefaultWritablePolygon2D( vertices ); }
return new DefaultWritablePolygon2D(L);
protected static Polygon2D getPolygon() { final List<RealPoint> vertices = new ArrayList<>(); try { Files.lines(Paths.get(AbstractFeatureTest.class.getResource("2d_geometric_features_polygon.txt").toURI())) .forEach(l -> { String[] coord = l.split(" "); RealPoint v = new RealPoint(new double[]{ Double.parseDouble(coord[0]), Double.parseDouble(coord[1])}); vertices.add(v); }); } catch (IOException | URISyntaxException exc) { exc.printStackTrace(); } return new DefaultWritablePolygon2D(vertices); }
/** * Rotates the given Polygon2D consisting of a list of RealPoints by the * given angle about the given center. * * @param inPoly A Polygon2D consisting of a list of RealPoint RealPoints * @param angle the rotation angle * @param center the rotation center * @return a rotated polygon */ private Polygon2D rotate(final Polygon2D inPoly, final double angle, final RealLocalizable center) { List<RealLocalizable> out = new ArrayList<>(); for (RealLocalizable RealPoint : GeomUtils.vertices(inPoly)) { // double angleInRadians = Math.toRadians(angleInDegrees); double cosTheta = Math.cos(angle); double sinTheta = Math.sin(angle); double x = cosTheta * (RealPoint.getDoublePosition(0) - center .getDoublePosition(0)) - sinTheta * (RealPoint.getDoublePosition(1) - center.getDoublePosition(1)) + center.getDoublePosition(0); double y = sinTheta * (RealPoint.getDoublePosition(0) - center .getDoublePosition(0)) + cosTheta * (RealPoint.getDoublePosition(1) - center.getDoublePosition(1)) + center.getDoublePosition(1); out.add(new RealPoint(x, y)); } return new DefaultWritablePolygon2D(out); }
@Override public Polygon2D calculate(final Polygon2D input) { double min_x = Double.POSITIVE_INFINITY; double max_x = Double.NEGATIVE_INFINITY; double min_y = Double.POSITIVE_INFINITY; double max_y = Double.NEGATIVE_INFINITY; for (final RealLocalizable rl : GeomUtils.vertices(input)) { if (rl.getDoublePosition(0) < min_x) { min_x = rl.getDoublePosition(0); } if (rl.getDoublePosition(0) > max_x) { max_x = rl.getDoublePosition(0); } if (rl.getDoublePosition(1) < min_y) { min_y = rl.getDoublePosition(1); } if (rl.getDoublePosition(1) > max_y) { max_y = rl.getDoublePosition(1); } } final List<RealLocalizable> bounds = new ArrayList<>(); bounds.add(new RealPoint(min_x, min_y)); bounds.add(new RealPoint(min_x, max_y)); bounds.add(new RealPoint(max_x, max_y)); bounds.add(new RealPoint(max_x, min_y)); return new DefaultWritablePolygon2D(bounds); }
return new DefaultWritablePolygon2D(p);