Пример #1
0
        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;
        }
Пример #2
0
  /**
   * Check if another polygon intersects with this polygon
   *
   * @param polygon
   * @return true if intersects, false otherwise
   */
  public boolean intersects(Polygon2D polygon) {

    if (!polygon.getBounds().intersects(getBounds())) {
      return false;
    }

    for (Enumeration e1 = edges(); e1.hasMoreElements(); ) {
      Line2D current = (Line2D) e1.nextElement();
      for (Enumeration e2 = polygon.edges(); e2.hasMoreElements(); ) {
        Line2D test = (Line2D) e2.nextElement();
        if (current.intersects(test)) {
          return true;
        }
      }
    }

    return false;
  }
Пример #3
0
  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);
  }
Пример #4
0
 /**
  * Creates a new instance of Polygon2D with vertices with the same values as the {@code p}
  * parameter.
  *
  * @param polygon The polygon to copy
  */
 public Polygon2D(Polygon2D polygon) {
   for (Enumeration e = polygon.vertices(); e.hasMoreElements(); ) {
     addVertex(new Vector2D((Vector2D) e.nextElement()));
   }
 }