Example #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));
  }
Example #2
0
  /**
   * Tests if the shape contains a point.
   *
   * @param p coord point
   * @return true if shape contains p
   */
  public boolean contains(Point2D p) {
    double x = startPoint.getX();
    double y = startPoint.getY();

    double xEnd = endPoint.getX();
    double yEnd = endPoint.getY();

    Line2D line = new Line2D.Double(x, y, xEnd, yEnd);

    return line.intersects(p.getX(), p.getY(), 10, 10);
  }
Example #3
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());
  }
Example #4
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;
  }