public Object parseXMLObject(XMLObject xo) throws XMLParseException { LinkedList<Point2D> Point2Ds = new LinkedList<Point2D>(); boolean closed; Polygon2D polygon; if (xo.getChild(Polygon2D.class) != null) { // This is a regular polygon polygon = (Polygon2D) xo.getChild(Polygon2D.class); } else { // This is an arbitrary polygon KMLCoordinates coordinates = (KMLCoordinates) xo.getChild(KMLCoordinates.class); closed = xo.getAttribute(CLOSED, false); if ((!closed && coordinates.length < 3) || (closed && coordinates.length < 4)) throw new XMLParseException( "Insufficient point2Ds in polygon '" + xo.getId() + "' to define a polygon in 2D"); for (int i = 0; i < coordinates.length; i++) Point2Ds.add(new Point2D.Double(coordinates.x[i], coordinates.y[i])); polygon = new Polygon2D(Point2Ds, closed); } polygon.setFillValue(xo.getAttribute(FILL_VALUE, 0.0)); return polygon; }
public static void main(String[] args) { Polygon2D polygon = new Polygon2D(); polygon.addPoint2D(new Point2D.Double(-10, -10)); polygon.addPoint2D(new Point2D.Double(-10, 50)); polygon.addPoint2D(new Point2D.Double(10, 50)); polygon.addPoint2D(new Point2D.Double(10, -10)); System.out.println(polygon); System.out.println(""); Point2D pt = new Point2D.Double(0, 0); System.out.println("polygon contains " + pt + ": " + polygon.containsPoint2D(pt)); pt = new Point2D.Double(100, 100); System.out.println("polygon contains " + pt + ": " + polygon.containsPoint2D(pt)); System.out.println(""); Rectangle2D boundingBox = new Rectangle2D.Double(0, 0, 100, 100); // defines lower-left corner and width/height System.out.println(boundingBox); Polygon2D myClip = polygon.clip(boundingBox); System.out.println(myClip); }