| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 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(0, 1, 0, 1); |
| 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 |
|