/* * Copyright (c) 2016 Vivid Solutions. * * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License 2.0 * and Eclipse Distribution License v. 1.0 which accompanies this distribution. * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v20.html * and the Eclipse Distribution License is available at * * http://www.eclipse.org/org/documents/edl-v10.php. */ package org.locationtech.jts.generator; import org.locationtech.jts.geom.Coordinate; import org.locationtech.jts.geom.Geometry; import org.locationtech.jts.geom.GeometryFactory; import org.locationtech.jts.geom.LinearRing; import org.locationtech.jts.geom.Polygon; import org.locationtech.jts.operation.valid.IsValidOp; /** * * This class is used to create a polygon within the specified bounding box. * * Successive calls to create may or may not return the same geometry topology. * * @author David Zwiers, Vivid Solutions. */ public class PolygonGenerator extends GeometryGenerator { protected int numberPoints = 4; protected int numberHoles = 0; protected int generationAlgorithm = 0; /** * Creates rectangular polygons */ public static final int BOX = 0; /** * Creates polygons whose points will not be rectangular when there are more than 4 points */ public static final int ARC = 1; private static final int RUNS = 5; /** * As the user increases the number of points, the probability of creating a random valid polygon decreases. * Please take not of this when selecting the generation style, and the number of points. * * May return null if a geometry could not be created. * * @see #getNumberPoints() * @see #setNumberPoints(int) * @see #getGenerationAlgorithm() * @see #setGenerationAlgorithm(int) * * @see #BOX * @see #ARC * * @see org.locationtech.jts.generator.GeometryGenerator#create() * * @throws IllegalStateException When the alg is not valid or the number of points is invalid * @throws NullPointerException when either the Geometry Factory, or the Bounding Box are undefined. */ public Geometry create() { if(geometryFactory == null){ throw new NullPointerException("GeometryFactory is not declared"); } if(boundingBox == null || boundingBox.isNull()){ throw new NullPointerException("Bounding Box is not declared"); } if(numberPoints<4){ throw new IllegalStateException("Too few points"); } double x = boundingBox.getMinX(); // base x double dx = boundingBox.getMaxX()-x; double y = boundingBox.getMinY(); // base y double dy = boundingBox.getMaxY()-y; Polygon p = null; for(int i=0;i0?1:0); // npts atleast 1 if(i%2 == 1){ // odd vert double cy = dy/npts; if(i > 1) // down cy *=-1; double tx = coords[cindex-1].x; double sy = coords[cindex-1].y; for(int j=0;j 1) // down cx *=-1; double ty = coords[cindex-1].y; double sx = coords[cindex-1].x; for(int j=0;j