-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRect.java
230 lines (209 loc) · 7.57 KB
/
Rect.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
package breakout;
import java.util.Objects;
/**
* Represents a rectangle in a 2-dimensional integer coordinate system.
*
* @immutable
* @invar | getTopLeft() != null
* @invar | getBottomRight() != null
* @invar | getTopLeft().isUpAndLeftFrom(getBottomRight())
*/
public class Rect {
/**
* @invar | topLeft != null
* @invar | bottomRight != null
* @invar | topLeft.getX() <= bottomRight.getX()
* @invar | topLeft.getY() <= bottomRight.getY()
*/
private final Point topLeft;
private final Point bottomRight;
public static final Vector[] COLLISSION_DIRS = new Vector[] {
Vector.UP, Vector.DOWN, Vector.LEFT, Vector.RIGHT
};
/**
* Construct a new rectangle with given top-left and bottom-right coordinate.
*
* @pre | topLeft != null
* @pre | bottomRight != null
* @pre | topLeft.getX() <= bottomRight.getX()
* @pre | topLeft.getY() <= bottomRight.getY()
* @post | getTopLeft().equals(topLeft)
* @post | getBottomRight().equals(bottomRight)
*/
public Rect(Point topLeft, Point bottomRight) {
this.topLeft = topLeft;
this.bottomRight = bottomRight;
}
/** Return the top-left point of this rectangle */
public Point getTopLeft() {
return topLeft;
}
/** Return the bottom-right point of this rectangle */
public Point getBottomRight() {
return bottomRight;
}
/**
* Return whether given point `loc` is inside this rectangle.
*
* @pre | loc != null
* @post | result == (getTopLeft().isUpAndLeftFrom(loc) && loc.isUpAndLeftFrom(getBottomRight()))
*/
public boolean contains(Point loc) {
return getTopLeft().isUpAndLeftFrom(loc) && loc.isUpAndLeftFrom(getBottomRight());
}
// /**
// * Return whether this rectangle contains a given circle.
// *
// * @post | result == (getTopLeft().plus(new Vector(loc.getDiameter(),loc.getDiameter())).isUpAndLeftFrom(getBottomRight()) &&
// * | minusMargin(loc.getRadius()).contains(loc.getCenter()))
// */
// public boolean contains(Circle loc) {
// return getTopLeft().plus(new Vector(loc.getDiameter(),loc.getDiameter())).isUpAndLeftFrom(getBottomRight()) &&
// minusMargin(loc.getRadius()).contains(loc.getCenter());
// }
/**
* Return whether this rectangle contains a given other rectangle.
*
* @post | result == (getTopLeft().isUpAndLeftFrom(other.getTopLeft()) &&
* | other.getBottomRight().isUpAndLeftFrom(getBottomRight()))
*/
public boolean contains(Rect other) {
return getTopLeft().isUpAndLeftFrom(other.getTopLeft()) &&
other.getBottomRight().isUpAndLeftFrom(getBottomRight());
}
// /**
// * Check whether this rectangle intersects with the given ball and if so, return the direction from the ball to the rectangle.
// * This direction may be an approximation for simplicity.
// *
// * @pre | ball != null
// * @post | result == null || (result.getSquareLength() == 1 && this.contains(ball.getOutermostPoint(result)))
// */
// public Vector collideWith(Circle ball) {
// for (Vector coldir : COLLISSION_DIRS) {
// Point c = ball.getOutermostPoint(coldir);
// if(contains(c)) {
// return coldir;
// }
// }
// return null;
// }
/**
* Return the rectangle obtained by subtracting an inner margin from all sides of this rectangle.
*
* @pre getTopLeft().plus(new Vector(2*dx,2*dy)).isUpAndLeftFrom(getBottomRight())
* @post | result != null
* @post | result.getTopLeft().equals(getTopLeft().plus(new Vector(dx,dy)))
* @post | result.getBottomRight().equals(getBottomRight().minus(new Vector(dx,dy)))
*/
public Rect minusMargin(int dx, int dy) {
Vector dv = new Vector(dx, dy);
return new Rect( topLeft.plus(dv),
bottomRight.minus(dv));
}
/**
* Return the rectangle obtained by subtracting an inner margin from all sides of this rectangle.
*
* @pre getTopLeft().plus(new Vector(2*d,2*d)).isUpAndLeftFrom(getBottomRight())
* @post | result != null
* @post | result.getTopLeft().equals(getTopLeft().plus(new Vector(d,d)))
* @post | result.getBottomRight().equals(getBottomRight().minus(new Vector(d,d)))
*/
public Rect minusMargin(int d) {
Vector dv = new Vector(d,d);
return new Rect( topLeft.plus(dv),
bottomRight.minus(dv));
}
/**
* Return the point inside this rectangle that is as close as possible to a given point p.
*
* @pre | p != null
* @post | result.getX() == Math.min(getBottomRight().getX(), Math.max(getTopLeft().getX(), p.getX()))
* @post | result.getY() == Math.min(getBottomRight().getY(), Math.max(getTopLeft().getY(), p.getY()))
*/
public Point constrain(Point p) {
int nx = Math.min(getBottomRight().getX(), Math.max(getTopLeft().getX(), p.getX()));
int ny = Math.min(getBottomRight().getY(), Math.max(getTopLeft().getY(), p.getY()));
return new Point(nx, ny);
}
/**
* Return the width of this rectangle.
*
* post | getBottomRight().getX() - getTopLeft().getX()
*/
public int getWidth() {
return bottomRight.getX() - topLeft.getX();
}
/**
* Return the height of this rectangle.
*
* @post | result == (getBottomRight().getY() - getTopLeft().getY())
*/
public int getHeight() {
return bottomRight.getY() - topLeft.getY();
}
@Override
public int hashCode() {
return Objects.hash(bottomRight, topLeft);
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Rect other = (Rect) obj;
return Objects.equals(bottomRight, other.bottomRight) && Objects.equals(topLeft, other.topLeft);
}
// /**
// * Move the argument circle by the least amount so that it falls entirely within this rect.
// *
// * @pre | c.getDiameter() < getWidth()
// * @post | contains(result)
// * @post | result.getCenter().equals(this.minusMargin(c.getRadius()).constrain(c.getCenter()))
// */
// public Circle constrain(Circle c) {
// Rect r = this.minusMargin(c.getRadius());
// Point nc = r.constrain(c.getCenter());
// return new Circle(nc,c.getDiameter());
// }
/**
* Detects whether two Rectangle objects are colliding and returns the normal vector of the plane of collision using a Vector object.
* In case no collision is detected, null is returned.
* @inspects | this, other
* @creates | result
* @pre | other != null
* @post | result == null || result.getSquareLength() == 1
*/
public Vector overlap(Rect other) {
int thisLeftX = this.topLeft.getX();
int thisRightX = this.bottomRight.getX();
int thisCenterX = this.topLeft.getX() + (this.bottomRight.getX() - this.topLeft.getX())/2;
int thisTopY = this.topLeft.getY();
int thisBottomY = this.bottomRight.getY();
int thisCenterY = this.topLeft.getY() + (this.bottomRight.getY() - this.topLeft.getY())/2;
int otherLeftX = other.getTopLeft().getX();
int otherRightX = other.getBottomRight().getX();
int otherTopY = other.getTopLeft().getY();
int otherBottomY = other.getBottomRight().getY();
// Collision at the right side of this
if (thisRightX >= otherLeftX && thisLeftX < otherLeftX && thisCenterY >= otherTopY && thisCenterY <= otherBottomY) {
return Vector.RIGHT;
}
// Collision at the left side of this
if (thisLeftX <= otherRightX && thisRightX > otherRightX && thisCenterY >= otherTopY && thisCenterY <= otherBottomY) {
return Vector.LEFT;
}
// Collision at the top of this
if (thisBottomY >= otherTopY && thisTopY < otherTopY && thisCenterX >= otherLeftX && thisCenterX <= otherRightX) {
return Vector.DOWN;
}
// Collision at the bottom of this
if (thisTopY <= otherBottomY && thisBottomY > otherBottomY && thisCenterX >= otherLeftX && thisCenterX <= otherRightX) {
return Vector.UP;
}
// No collision
return null;
}
}