예제 #1
0
  /**
   * Check if a point is contained within this polygon
   *
   * @param point
   * @return true if contained, false otherwise
   */
  public boolean contains(Vector2D point) {

    if (!getBounds().contains(point)) {
      System.out.println("AAA: " + point);
      return false;
    }

    Line2D verticalRay = new Line2D(point, new Vector2D(point.getX(), Double.POSITIVE_INFINITY));
    int intersectionCount = 0;
    boolean contains = false;

    generateEdges();

    for (Enumeration e = edges.elements(); e.hasMoreElements(); ) {
      Line2D testLine = (Line2D) e.nextElement();
      double minY = Math.min(testLine.getStart().getY(), testLine.getEnd().getY());
      double maxY = Math.max(testLine.getStart().getY(), testLine.getEnd().getY());

      if (testLine.getGradient() == Double.POSITIVE_INFINITY) {
        if (point.getX() == testLine.getStart().getX()
            && point.getY() >= minY
            && point.getY() <= maxY) {
          contains = true;
        }
      } else {
        if (verticalRay.intersects(testLine)) {
          intersectionCount++;
        }
      }
    }

    return (contains || (intersectionCount % 2 == 1));
  }
예제 #2
0
  /**
   * Check if a line intersects with this polygon
   *
   * @param line
   * @return True if intersects, false otherwise
   */
  public boolean intersects(Line2D line) {

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

    generateEdges();

    for (Enumeration e = edges.elements(); e.hasMoreElements(); ) {
      Line2D current = (Line2D) e.nextElement();
      if (current.intersects(line)) {
        return true;
      }
    }

    return contains(line.getStart()) || contains(line.getEnd());
  }