| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
package dr.geo; |
| 27 |
|
| 28 |
import dr.xml.XMLParser; |
| 29 |
import org.jdom.Element; |
| 30 |
|
| 31 |
import java.awt.geom.Point2D; |
| 32 |
import java.awt.geom.Rectangle2D; |
| 33 |
import java.util.ArrayList; |
| 34 |
import java.util.List; |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
public class Polygon2D extends AbstractPolygon2D { |
| 43 |
|
| 44 |
public Polygon2D(double[] x, double[] y) { |
| 45 |
if (x.length != y.length) { |
| 46 |
throw new RuntimeException("Unbalanced arrays"); |
| 47 |
} |
| 48 |
|
| 49 |
if (x[0] != x[x.length - 1] && y[0] != y[y.length - 1]) { |
| 50 |
double[] newX = new double[x.length + 1]; |
| 51 |
double[] newY = new double[y.length + 1]; |
| 52 |
System.arraycopy(x, 0, newX, 0, x.length); |
| 53 |
System.arraycopy(y, 0, newY, 0, y.length); |
| 54 |
newX[x.length] = x[0]; |
| 55 |
newY[y.length] = y[0]; |
| 56 |
this.x = newX; |
| 57 |
this.y = newY; |
| 58 |
} else { |
| 59 |
this.x = x; |
| 60 |
this.y = y; |
| 61 |
} |
| 62 |
length = this.x.length - 1; |
| 63 |
|
| 64 |
} |
| 65 |
|
| 66 |
public Polygon2D(List<Point2D> points, boolean closed) { |
| 67 |
this.point2Ds = points; |
| 68 |
if (!closed) { |
| 69 |
Point2D start = points.get(0); |
| 70 |
points.add(start); |
| 71 |
} |
| 72 |
convertPointsToArrays(); |
| 73 |
length = points.size() - 1; |
| 74 |
} |
| 75 |
|
| 76 |
public Polygon2D() { |
| 77 |
length = 0; |
| 78 |
point2Ds = new ArrayList<Point2D>(); |
| 79 |
} |
| 80 |
|
| 81 |
public Polygon2D(Element e) { |
| 82 |
List<Element> children = e.getChildren(); |
| 83 |
id = e.getAttributeValue(XMLParser.ID); |
| 84 |
|
| 85 |
parseCoordinates(e); |
| 86 |
} |
| 87 |
|
| 88 |
public double getProbability(Point2D Point2D, boolean outside) { |
| 89 |
boolean contains = containsPoint2D(Point2D); |
| 90 |
if (outside ^ contains) { |
| 91 |
return 1.0; |
| 92 |
} else { |
| 93 |
return 0.0; |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
public double getLogProbability(Point2D Point2D, boolean outside) { |
| 98 |
boolean contains = containsPoint2D(Point2D); |
| 99 |
if (outside ^ contains) { |
| 100 |
return 0.0; |
| 101 |
} else { |
| 102 |
return Double.NEGATIVE_INFINITY; |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
@Override |
| 107 |
public void setFillValue(double value) { |
| 108 |
throw new RuntimeException("setFillValue() call not allowed in Polygon2D; try Polygon2DSampling."); |
| 109 |
} |
| 110 |
|
| 111 |
@Override |
| 112 |
public double getFillValue() { |
| 113 |
throw new RuntimeException("getFillValue() call not allowed in Polygon2D; try Polygon2DSampling."); |
| 114 |
} |
| 115 |
|
| 116 |
@Override |
| 117 |
public double getLogFillValue() { |
| 118 |
throw new RuntimeException("getLogFillValue() call not allowed in Polygon2D; try Polygon2DSampling."); |
| 119 |
} |
| 120 |
|
| 121 |
@Override |
| 122 |
public boolean hasFillValue() { |
| 123 |
return false; |
| 124 |
} |
| 125 |
|
| 126 |
public static void main(String[] args) { |
| 127 |
|
| 128 |
Polygon2D polygon = new Polygon2D(); |
| 129 |
polygon.addPoint2D(new Point2D.Double(-10, -10)); |
| 130 |
polygon.addPoint2D(new Point2D.Double(-10, 50)); |
| 131 |
polygon.addPoint2D(new Point2D.Double(10, 50)); |
| 132 |
polygon.addPoint2D(new Point2D.Double(10, -10)); |
| 133 |
System.out.println(polygon); |
| 134 |
System.out.println(""); |
| 135 |
|
| 136 |
Point2D pt = new Point2D.Double(0, 0); |
| 137 |
System.out.println("polygon contains " + pt + ": " + polygon.containsPoint2D(pt)); |
| 138 |
pt = new Point2D.Double(100, 100); |
| 139 |
System.out.println("polygon contains " + pt + ": " + polygon.containsPoint2D(pt)); |
| 140 |
System.out.println(""); |
| 141 |
|
| 142 |
Rectangle2D boundingBox = new Rectangle2D.Double(0, 0, 100, 100); |
| 143 |
System.out.println(boundingBox); |
| 144 |
Polygon2D myClip = (Polygon2D)polygon.clip(boundingBox); |
| 145 |
System.out.println(myClip); |
| 146 |
|
| 147 |
} |
| 148 |
|
| 149 |
} |