Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
epoxy
GitHub Repository: epoxy/proj11
Path: blob/master/SLICK_HOME/src/org/newdawn/slick/geom/Rectangle.java
1461 views
1
package org.newdawn.slick.geom;
2
3
/**
4
* An axis oriented used for shape bounds
5
*
6
* @author Kevin Glass
7
*/
8
public class Rectangle extends Shape {
9
/** The width of the box */
10
protected float width;
11
/** The height of the box */
12
protected float height;
13
14
/**
15
* Create a new bounding box
16
*
17
* @param x The x position of the box
18
* @param y The y position of the box
19
* @param width The width of the box
20
* @param height The hieght of the box
21
*/
22
public Rectangle(float x, float y, float width, float height) {
23
this.x = x;
24
this.y = y;
25
this.width = width;
26
this.height = height;
27
maxX = x+width;
28
maxY = y+height;
29
checkPoints();
30
}
31
32
/**
33
* Check if this rectangle contains a point
34
*
35
* @param xp The x coordinate of the point to check
36
* @param yp The y coordinate of the point to check
37
* @return True if the point is within the rectangle
38
*/
39
public boolean contains(float xp, float yp) {
40
if (xp <= getX()) {
41
return false;
42
}
43
if (yp <= getY()) {
44
return false;
45
}
46
if (xp >= maxX+1) {
47
return false;
48
}
49
if (yp >= maxY+1) {
50
return false;
51
}
52
53
return true;
54
}
55
56
/**
57
* Set the bounds of this rectangle based on the given rectangle
58
*
59
* @param other The other rectangle whose bounds should be applied
60
*/
61
public void setBounds(Rectangle other) {
62
setBounds(other.getX(), other.getY(), other.getWidth(), other.getHeight());
63
}
64
65
/**
66
* Set the bounds of this rectangle
67
*
68
* @param x The x coordinate of this rectangle
69
* @param y The y coordinate of this rectangle
70
* @param width The width to set in this rectangle
71
* @param height The height to set in this rectangle
72
*/
73
public void setBounds(float x, float y, float width, float height) {
74
setX(x);
75
setY(y);
76
setSize(width, height);
77
}
78
79
/**
80
* Set the size (widtha and height) of this rectangle
81
*
82
* @param width The width to set in this rectangle
83
* @param height The height to set in this rectangle
84
*/
85
public void setSize(float width, float height) {
86
setWidth(width);
87
setHeight(height);
88
}
89
90
91
/**
92
* Get the width of the box
93
*
94
* @return The width of the box
95
*/
96
public float getWidth() {
97
return width;
98
}
99
100
/**
101
* Get the height of the box
102
*
103
* @return The height of the box
104
*/
105
public float getHeight() {
106
return height;
107
}
108
109
/**
110
* Grow the rectangle at all edges by the given amounts. This will result in the
111
* rectangle getting larger around it's centre.
112
*
113
* @param h The amount to adjust horizontally
114
* @param v The amount to ajust vertically
115
*/
116
public void grow(float h, float v) {
117
setX(getX() - h);
118
setY(getY() - v);
119
setWidth(getWidth() + (h*2));
120
setHeight(getHeight() + (v*2));
121
}
122
123
/**
124
* Grow the rectangle based on scaling it's size
125
*
126
* @param h The scale to apply to the horizontal
127
* @param v The scale to appy to the vertical
128
*/
129
public void scaleGrow(float h, float v) {
130
grow(getWidth() * (h-1), getHeight() * (v-1));
131
}
132
133
/**
134
* Set the width of this box
135
*
136
* @param width The new width of this box
137
*/
138
public void setWidth(float width) {
139
if (width != this.width) {
140
pointsDirty = true;
141
this.width = width;
142
maxX = x+width;
143
}
144
}
145
146
/**
147
* Set the heightof this box
148
*
149
* @param height The height of this box
150
*/
151
public void setHeight(float height) {
152
if (height != this.height) {
153
pointsDirty = true;
154
this.height = height;
155
maxY = y+height;
156
}
157
}
158
159
/**
160
* Check if this box touches another
161
*
162
* @param shape The other shape to check against
163
* @return True if the rectangles touch
164
*/
165
public boolean intersects(Shape shape) {
166
if(shape instanceof Rectangle) {
167
Rectangle other = (Rectangle)shape;
168
if ((x > (other.x + other.width)) || ((x + width) < other.x)) {
169
return false;
170
}
171
if ((y > (other.y + other.height)) || ((y + height) < other.y)) {
172
return false;
173
}
174
return true;
175
}
176
else if(shape instanceof Circle) {
177
return intersects((Circle)shape);
178
}
179
else {
180
return super.intersects(shape);
181
}
182
}
183
184
protected void createPoints() {
185
// -1 so that the dimensions include the left and top lines of the rectangle
186
float useWidth = width - 1;
187
float useHeight = height - 1;
188
points = new float[8];
189
190
points[0] = x;
191
points[1] = y;
192
193
points[2] = x + useWidth;
194
points[3] = y;
195
196
points[4] = x + useWidth;
197
points[5] = y + useHeight;
198
199
points[6] = x;
200
points[7] = y + useHeight;
201
202
maxX = points[2];
203
maxY = points[5];
204
minX = points[0];
205
minY = points[1];
206
207
findCenter();
208
calculateRadius();
209
}
210
211
/**
212
* Check if a circle touches this rectangle
213
*
214
* @param other The circle to check against
215
* @return True if they touch
216
*/
217
private boolean intersects(Circle other) {
218
return other.intersects(this);
219
}
220
221
/**
222
* @see java.lang.Object#toString()
223
*/
224
public String toString() {
225
return "[Rectangle "+width+"x"+height+"]";
226
}
227
228
/**
229
* Check if a rectangle contains a point (static to use it everywhere)
230
*
231
* @param xp
232
* The x coordinate of the point to check
233
* @param yp
234
* The y coordinate of the point to check
235
* @param xr
236
* The x coordinate of the rectangle
237
* @param yr
238
* The y coordinate of the rectangle
239
* @param widthr
240
* The width of the rectangle
241
* @param heightr The height of the rectangle
242
* @return True if the point is within the rectangle
243
*/
244
public static boolean contains(float xp, float yp, float xr, float yr,
245
float widthr, float heightr) {
246
return (xp >= xr) && (yp >= yr) && (xp <= xr + widthr)
247
&& (yp <= yr + heightr);
248
}
249
250
/**
251
* Apply a transformation and return a new shape. This will not alter the current shape but will
252
* return the transformed shape.
253
*
254
* @param transform The transform to be applied
255
* @return The transformed shape.
256
*/
257
public Shape transform(Transform transform) {
258
checkPoints();
259
260
Polygon resultPolygon = new Polygon();
261
262
float result[] = new float[points.length];
263
transform.transform(points, 0, result, 0, points.length / 2);
264
resultPolygon.points = result;
265
resultPolygon.findCenter();
266
resultPolygon.checkPoints();
267
268
return resultPolygon;
269
}
270
}
271
272