Lauren_G
Lauren_G

Reputation: 65

Calculate the points of a single line between two rectangles in Java

Assuming we have calculated the line segment between the centers of two rectangles that do not overlap.

...What's the easiest way in Java to recalculate the points of that line segment if we only consider the two points of intersection between the rectangles?

For example: Diagram of line intersection

Upvotes: 0

Views: 390

Answers (2)

Lauren_G
Lauren_G

Reputation: 65

WJS, your posted answer gave me the idea...For each rectangle, I just need to loop through the 4 lines representing each side of a rectangle and locate the first one which intersects the centre line.

Calculating each of the 4 lines per rectangle is easy. I would then just subject each line to the following code:

public class Intersection {
    
    private static class Point {
        double x, y;
 
        Point(double x, double y) {
            this.x = x;
            this.y = y;
        }
 
        @Override public String toString() {
            return String.format("{%f, %f}", x, y);
        }
    }
 
    private static class Line {
        Point s, e;
 
        Line(Point s, Point e) {
            this.s = s;
            this.e = e;
        }
    }
 
    private static final Point findIntersection(Line l1, Line l2) {
        double a1 = l1.e.y - l1.s.y;
        double b1 = l1.s.x - l1.e.x;
        double c1 = a1 * l1.s.x + b1 * l1.s.y;
 
        double a2 = l2.e.y - l2.s.y;
        double b2 = l2.s.x - l2.e.x;
        double c2 = a2 * l2.s.x + b2 * l2.s.y;
 
        double delta = a1 * b2 - a2 * b1;
        return new Point((b2 * c1 - b1 * c2) / delta, (a1 * c2 - a2 * c1) / delta);
    }
 
    public static void main(String[] args) {
        Line l1 = new Line(new Point(4, 0), new Point(6, 10));
        Line l2 = new Line(new Point(0, 3), new Point(10, 7));
        System.out.println(findIntersection(l1, l2));
 
        l1 = new Line(new Point(0, 3), new Point(10, 3));
        l2 = new Line(new Point(4, 0), new Point(4, 10));
        System.out.println(findIntersection(l1, l2));
    }
}  

Upvotes: 0

WJS
WJS

Reputation: 40034

This is essentially a math problem. There may be some methods that can help but that simply requires perusing the Math class and the java.awt.geom package.

  • first, find the center coordinates of each rectangle using origins and width and height.
  • then find the equation of the line between those coordinates y = mx + b. m is slope and b is y intercept. Here, x* and y* values are the centers of the rectangles. m = (y1-y2)/(x1-x2). b = y1 - m*x1 or you can also use y2 and x2.
  • then find the point of intersection of that line with each side. Again, you will need to use the origins and width and height to help figure this out. You may need to apply the equation just derived to determine if the line intersects a side or a top or bottom.
  • then using those points of intersection, find the line using the distance formula (think Pythagoras). Or Math.hypot()

Note: Keep in mind that, as previously alluded to, the line may not always intersect on the sides. Depending on the relative locations of the rectangles, it could be tops and bottoms or the combination top and side or bottom and side. I recommend you do this on paper and cover all the possibilities before trying to code it.

Upvotes: 1

Related Questions