返回所有矩形的并集

问题描述 投票:0回答:2

我对Java有点陌生,但有一个问题,就是我无法忍受。

  • union(Rectangle ...矩形)] >>应返回由所有矩形的并集。如果矩形为空,则返回null。
  • 我创建了一个辅助方法来计算2个矩形的并集,然后以某种方式尝试将其集成到并集方法中,但没有成功。对于2个矩形的intersection

,我有点必须做同样的事情,但也无法完成。

你们能给我些帮助吗?下面是我的代码。

public class Rectangle {
    int x, y, width, height;

    public Rectangle(int xInput, int yInput, int widthInput, int heightInput) {
        if (xInput <= 0 || yInput <= 0 || widthInput <= 0 || heightInput <= 0) {
            return;
        }
        this.x = xInput;
        this.y = yInput;
        this.width = widthInput;
        this.height = heightInput;

    }

    public static Rectangle union(Rectangle... rectangles) {
        Rectangle s = new Rectangle(0, 0, 0, 0);
        if (rectangles.length != 0) {
            for (Rectangle r : rectangles) {
                s = unionOfTwo(s, r);
            }
            return s;
        } else {
            return null;
        }

    }

     public static Rectangle unionOfTwo(Rectangle rec1, Rectangle rec2) {

        int x1 = Utils.min(rec1.x, rec2.x);
        int x2 = Utils.max(rec1.x + rec1.width, rec2.x + rec2.width) - x1;
        int y1 = Utils.min(rec1.y, rec2.y);
        int y2 = Utils.max(rec1.y + rec1.height, rec2.y + rec2.height) - y1;
        return new Rectangle(x1, y1, x2, y2);
    }
}

我是Java的新手,有一个问题,就是我无法解决问题。 union(Rectangle ...矩形)应该返回所有矩形的Union所给定的矩形。如果矩形为空,则...

java algorithm geometry computational-geometry rectangles
2个回答
0
投票
  • 将所有矩形转换为[XMin,XMax] x [YMin,YMax]表示形式。


0
投票

问题在这里:

© www.soinside.com 2019 - 2024. All rights reserved.