计算2个矩形之间的交点POINTS

时间:2015-05-19 00:00:39

标签: java math intersection rectangles

我有一个问题,我需要找到2个矩形的交点。我知道这个问题已被问过here,但解决方案总是返回一个矩形,而我只需要2个或4个交叉点

1 个答案:

答案 0 :(得分:1)

也许这种方法提供了你想要的东西

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;
    }