Class GeometricShapeBuilder

 1 /*
 2  * Copyright (c) 2016 Martin Davis.
 3  *
 4  * All rights reserved. This program and the accompanying materials
 5  * are made available under the terms of the Eclipse Public License 2.0
 6  * and Eclipse Distribution License v. 1.0 which accompanies this distribution.
 7  * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v20.html
 8  * and the Eclipse Distribution License is available at
 9  *
10  * http://www.eclipse.org/org/documents/edl-v10.php.
11  */
12  
13 package org.locationtech.jts.shape;
14  
15 import org.locationtech.jts.geom.Coordinate;
16 import org.locationtech.jts.geom.Envelope;
17 import org.locationtech.jts.geom.Geometry;
18 import org.locationtech.jts.geom.GeometryFactory;
19 import org.locationtech.jts.geom.LineSegment;
20  
21 public abstract class GeometricShapeBuilder 
22 {
23     protected Envelope extent = new Envelope(0101);
24     protected int numPts = 0;
25     protected GeometryFactory geomFactory;
26     
27     public GeometricShapeBuilder(GeometryFactory geomFactory)
28     {
29         this.geomFactory = geomFactory;
30     }
31     
32     public void setExtent(Envelope extent)
33     {
34         this.extent = extent;
35     }
36     
37     public Envelope getExtent()
38     {
39         return extent;
40     }
41     
42     public Coordinate getCentre()
43     {
44         return extent.centre();
45     }
46     
47     public double getDiameter()
48     {
49         return Math.min(extent.getHeight(), extent.getWidth());
50     }
51     
52     public double getRadius()
53     {
54         return getDiameter() / 2;
55     }
56     
57     public LineSegment getSquareBaseLine()
58     {
59         double radius = getRadius();
60         
61         Coordinate centre = getCentre();
62         Coordinate p0 = new Coordinate(centre.x - radius, centre.y - radius);
63         Coordinate p1 = new Coordinate(centre.x + radius, centre.y - radius);
64         return new LineSegment(p0, p1);
65     }
66     
67     public Envelope getSquareExtent()
68     {
69         double radius = getRadius();
70         
71         Coordinate centre = getCentre();
72         return new Envelope(centre.x - radius, centre.x + radius,
73                 centre.y - radius, centre.y + radius);
74     }
75     
76  
77   /**
78    * Sets the total number of points in the created {@link Geometry}.
79    * The created geometry will have no more than this number of points,
80    * unless more are needed to create a valid geometry.
81    */
82   public void setNumPoints(int numPts) { this.numPts = numPts; }
83  
84   public abstract Geometry getGeometry();
85  
86   protected Coordinate createCoord(double x, double y)
87   {
88       Coordinate pt = new Coordinate(x, y);
89       geomFactory.getPrecisionModel().makePrecise(pt);
90     return pt;
91   }
92   
93  
94 }
95