1

I've got a problem where i need to find the intersection points of 2 rectangles. I know this question has already been asked here, but the solutions always returns a rectangle, whereas i just need the 2 or 4 intersection points

Community
  • 1
  • 1
  • show your work - what have you done, what is the specific problem, what errors do you get? – rmalchow May 19 '15 at 00:03
  • You've got 4 lines for each rectangle. How hard is it to test whether any two lines (one from each rectangle) intersect? (And, of course, make sure that the intersection lies along the edge of the rectangle, not beyond.) – Hot Licks May 19 '15 at 00:06
  • 1
    Are the rectangles axis-aligned? – samgak May 19 '15 at 01:45

1 Answers1

1

Perhaps this method offers what you're looking for

public List<Point> getIntersects(Rectangle2D a, Rectangle2D b) {
        if (!a.intersects(b)) return null;
        List<Point> points = new ArrayList<Point>();
        double ax = a.getX();
        double ay = a.getY();
        double aw = a.getWidth();
        double ah = a.getHeight();
        double bx = b.getX();
        double by = b.getY();
        double bw = b.getWidth();
        double bh = b.getHeight();
        if (ax <= bx) {
            if (ay < by) {
                points.add(new Point((int) (ax + aw), (int) by));
                points.add(new Point((int) (bx), (int) (ay + ah)));
            } else {
                points.add(new Point((int) (ax + aw), (int) (by + bw)));
                points.add(new Point((int) (bx), (int) (ay)));
            }
        } else return getIntersects(b, a);
        return points;
    }
Caleb Limb
  • 304
  • 1
  • 11