retanglesを重複チェック

ホアングエン:

私は、矩形間の重複をチェックする割り当てをやっています。誰かがこれを行う方法私のアイデアを与えることはできますか?

例えば:所与plot1(12,12,6,6)とplot2(12,12,3,2)。結果は完全にplot1内部plot2ですが、私は、なぜ下することはできません。

注:私は、コンストラクタの部分で何かを間違っていたなら、私を修正してください。

public class Plot {

    private int x; //default
    private int y; //default
    private int depth; //default
    private int width; //default

    Plot(){
        x = 0; 
        y = 0; 
        depth = 1; 
        width = 1; 
    }

    public Plot (Plot p) { //Copy constructor
        this.x = p.x;
        this.y = p.y;
        this.width = p.width;
        this.depth = p.depth;
    }

    public Plot(int x, int y, int width, int depth) {
        this.x = x;
        this.y = y;
        this.width = width;
        this.depth = depth;
    }

    public boolean overlaps(Plot plot) {

    }

    public boolean encompasses(Plot plot) {

    }
    // Getters and setters for all
}
Nosrep:

あなたは使用することができますintersects()から、メソッドをjava.awt.Rectangleオーバーラップのために:

public boolean overlaps(Plot plot) {
    return new Rectangle(x, y, width, depth).intersects(plot.getX(), plot.getY(), plot.getWidth(), plot.getDepth());
}

そしてcontains()含まれるため:

public boolean overlaps(Plot plot) {
    return new Rectangle(x, y, width, depth).contains(plot.getX(), plot.getY(), plot.getWidth(), plot.getDepth());
}

場合はgetWidth()代わりに、上下の長さを意味し、ちょうど逆plot.getWidth()plot.getDepth

(これは、プロットにゲッターを有すると仮定されます)

おすすめ

転載: http://43.154.161.224:23101/article/api/json?id=371224&siteId=1