-
Notifications
You must be signed in to change notification settings - Fork 0
/
AlignedRectangle2D.java
60 lines (49 loc) · 1.46 KB
/
AlignedRectangle2D.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
public class AlignedRectangle2D {
public boolean isEmpty = true;
private Point2D min = new Point2D(0,0);
private Point2D max = new Point2D(0,0);
public AlignedRectangle2D() {
}
public AlignedRectangle2D( Point2D p0, Point2D p1 ) {
bound( p0 );
bound( p1 );
}
public void clear() { isEmpty = true; }
// Enlarge the rectangle as necessary to contain the given point
public void bound( Point2D p ) {
if ( isEmpty ) {
min.copy(p);
max.copy(p);
isEmpty = false;
}
else {
if ( p.x() < min.x() ) min.p[0] = p.x();
else if ( p.x() > max.x() ) max.p[0] = p.x();
if ( p.y() < min.y() ) min.p[1] = p.y();
else if ( p.y() > max.y() ) max.p[1] = p.y();
}
}
// Enlarge the rectangle as necessary to contain the given rectangle
public void bound( AlignedRectangle2D rect ) {
bound( rect.min );
bound( rect.max );
}
public boolean isEmpty() { return isEmpty; }
public boolean contains( Point2D p ) {
return !isEmpty
&& min.x() <= p.x() && p.x() <= max.x()
&& min.y() <= p.y() && p.y() <= max.y();
}
public boolean contains( AlignedRectangle2D r ) {
if ( isEmpty ) return false;
if ( r.isEmpty ) return true;
return min.x() <= r.min.x() && r.max.x() <= max.x()
&& min.y() <= r.min.y() && r.max.y() <= max.y();
}
public Point2D getMin() { return min; }
public Point2D getMax() { return max; }
public Vector2D getDiagonal() { return Point2D.diff(max,min); }
public Point2D getCenter() {
return Point2D.average( min, max );
}
}