Search code examples
javalineintersectionpointrectangles

Detect intersections of lines with rectangles


enter image description here

I want to detect intersection points of random lines with random rectangels. My Line class get 4 coordinates and makes a line out of them. My intersectionWith function gets 2 lines and returns the intersection point, and if there is no point, it returns null.

I have 2 functions: One finds the closest intersection point and returns it (I think it's irrelevant here): Located in Line.java

public Point closestIntersectionToStartOfLine(Rectangle rect){
    Line[] rectLines = new Line[4];
    Point[] IntersectionPoints = new Point[4];
    double minDistance=0;
    //up
    rectLines[0] = new Line(rect.getUpperLeft().getX(), rect.getUpperLeft().getY(), rect.getWidth()+rect.getUpperLeft().getX(), rect.getUpperLeft().getY());
    //right
    rectLines[1] = new Line(rect.getWidth()+rect.getUpperLeft().getX(), rect.getUpperLeft().getY(), rect.getUpperLeft().getX()+rect.getWidth(), rect.getHeight()+rect.getUpperLeft().getY());
    //down
    rectLines[2] = new Line(rect.getUpperLeft().getX()+rect.getWidth(), rect.getHeight()+rect.getUpperLeft().getY(), rect.getUpperLeft().getX(), rect.getUpperLeft().getY()+rect.getHeight());
    //left
    rectLines[3] = new Line(rect.getUpperLeft().getX(), rect.getUpperLeft().getY()+rect.getHeight(), rect.getUpperLeft().getX(), rect.getUpperLeft().getY());

    for (int i=0; i<4; i++) {
        if (intersectionWith(rectLines[i]) != null)
            IntersectionPoints[i] = intersectionWith(rectLines[i]);
    }
    for (int i=0; i<IntersectionPoints.length; i++) {
        if (i == 0)
            minDistance = rect.getUpperLeft().distance(IntersectionPoints[i]);
        else {
            if (rect.getUpperLeft().distance(IntersectionPoints[i]) < minDistance)
                Point.closestIntersectionToStartOfLine = IntersectionPoints[i];
        }
    }
    return Point.closestIntersectionToStartOfLine;
}

And this function keep ALL intersection points of a given line into a list. Located in Rectangle.java

 // Return a (possibly empty) List of intersection points
// with the specified line.
public java.util.List<Point> intersectionPoints(Line line) {
    List<Point> intersectionPointsList = new ArrayList<Point>();
    Line[] rectangleSides = new Line[4];
    Point[] corners = new Point[4];
    rectangleCorners(corners);
    recatngleSides(rectangleSides, corners);
    for (int i = 0; i < 4; i++) {
        Point p = line.intersectionWith(rectangleSides[i]);
        if (p != null && !intersectionPointsList.contains(p)) {
            intersectionPointsList.add(p);
        }
    }
    return intersectionPointsList;
}
public void rectangleCorners(Point[] corners) {
    double width = this.getWidth();
    double height = this.getHeight();
    double x = this.upperLeft.getX();
    double y = this.upperLeft.getY();
    //upper left
    corners[0] = this.getUpperLeft();
    //upper right
    corners[1] = new Point(x + width, y);
    //down right
    corners[2] = new Point(x + width, y + height);
    //down left
    corners[3] = new Point(x, y + height);
}

/**
 * recatngleSides.
 * Saves the lines in the 4 rectangle edges.
 *
 * @param sides  - an empty array of lines
 * @param corners - an array of edge points.
 */
public void recatngleSides(Line[] sides, Point[] corners) {
    // up
    sides[0] = new Line(corners[0], corners[1]);
    // right
    sides[1] = new Line(corners[1], corners[2]);
    // down
    sides[2] = new Line(corners[2], corners[3]);
    // left
    sides[3] = new Line(corners[3], corners[0]);
}

}

I don't know why but it recognises random points as intersection points as you can see in the picture.


Solution

  • Because, if you look where your 'random' points are, they are actually where the sides of non-intersecting rectangles would be if you extend the sides to infinity. You need to actually check there is an intersection with the line segment that is the rectangle side.