【问题标题】:Calculating the intersection POINTS between 2 rectangles计算两个矩形之间的交点
【发布时间】:2015-05-19 00:00:39
【问题描述】:

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

【问题讨论】:

  • 展示你的工作——你做了什么,具体问题是什么,你遇到了什么错误?
  • 每个矩形有 4 行。测试任何两条线(每个矩形中的一条)是否相交有多难? (当然,要确保交叉点位于矩形的边缘,而不是超出。)
  • 矩形是否轴对齐?

标签: java math intersection rectangles


【解决方案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;
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-23
    • 1970-01-01
    • 2018-03-11
    • 2015-01-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多