A) 状況を理解するには:
1D 間隔に関する 2D 間隔 A および B の定義:
A = Ixa x Iya = [x1a, x2a] x [y1a, y2a]
B = Ixb x Iyb = [x1b, x2b] x [y1b, y2b]
それで
A is contained in B, iff
Ixa = [x1a, x2a] is contained in Ixb [x1b, x2b] and
Iya = [y1a, y2a] is contained in Iyb = [y1b, y2b].
使用する
I1 = [a, b] is contained in I2 = [c, d] iff c <= a and b <= d.
これは、Interval2D ( http://algs4.cs.princeton.edu/12oop/Interval2D.java.html ) および Intervall1D ( http://algs4.cs.princeton.edu/12oop/ )の交差メソッドの実装に似ています。Interval1D.java.html ) は、条件の論理的な逆をテストすることのみを示します。
B)今あなたの方法に:
左下 (x1a, y1a) と右上 (x2a, y2a) のポイントをチェックすると、contains(Point2D) でテストを実行できるようになります。
A is contained in B, iff B contains (x1a, y1a) and B contains (x2a, y2a).
醜いのは、Interval1D にはプライベートな左右の座標にアクセスするゲッターがあるのに対し、Interval2D にはプライベートな x および y (1 次元) 間隔にアクセスするゲッターがないことです。toString() 出力からそれらを解析することもできますが、それは見苦しく、作業が多すぎます。スーパークラスの作成
public class Rect {
public Interval1D x;
public Interval1D y;
public Interval2D r;
Rect(Interval1D px, Interval1D py) {
x = px;
y = py;
r = new Interval2D(px, py);
}
public boolean contains(Rect that) {
if (!this.r.contains(new Point2D(that.x.left(), that.y.left()))) return false;
if (!this.r.contains(new Point2D(that.x.right(), that.y.right()))) return false;
return true;
}
}
それを使うのはただ醜いです。