1: <?php
2:
3: 4: 5: 6:
7:
8: require_once 'exceptions.php';
9: require_once 'Point.php';
10: require_once 'Size.php';
11:
12: 13: 14:
15:
16: class Rectangle {
17:
18: 19: 20: 21: 22:
23: private $location;
24:
25: 26: 27: 28: 29:
30:
31: private $size;
32:
33: 34: 35: 36: 37: 38:
39:
40: public function __construct() {
41: $args = func_get_args();
42: if (count($args) == 4) {
43: $this->setLocation($args[0], $args[1]);
44: $this->setSize($args[2], $args[3]);
45: } else if (count($args) == 2
46: && $args[0] instanceof Point
47: && $args[1] instanceof Size) {
48:
49: $this->setLocation($args[0]);
50: $this->setSize($args[1]);
51: }
52: }
53:
54: 55: 56: 57: 58: 59: 60:
61:
62: public function flip() {
63: $t = $this->getWidth();
64: $this->setWidth($this->getHeight());
65: $this->setHeight($t);
66: return $this;
67: }
68:
69: 70: 71: 72: 73: 74: 75: 76: 77:
78:
79: public function inflate() {
80: $args = func_get_args();
81: if (count($args) == 2) {
82: $this->setWidth($this->getWidth() + $args[0]);
83: $this->setHeight($this->getHeight() + $args[1]);
84: } else if (count($args) == 1) {
85: $this->setSize(Size::add($this->getSize(), $args[0]));
86: } else {
87: throw new IllegalArgumentException();
88: }
89: return $this;
90: }
91:
92: 93: 94: 95: 96: 97: 98:
99:
100: public function isIntersectsWithX(Rectangle $rect) {
101: if ($rect->isNull() || $this->isNull()) {
102: return false;
103: }
104: $left1 = $this->getLeft();
105: $left2 = $rect->getLeft();
106: if ($left1 < $left2) {
107: return $left2 - $left1 < $this->getWidth();
108: } else if ($left1 >= $left2) {
109: return $left1 - $left2 < $rect->getWidth();
110: }
111: }
112:
113: 114: 115: 116: 117: 118: 119:
120:
121: public function isIntersectsWithY(Rectangle $rect) {
122: if ($rect->isNull() || $this->isNull()) {
123: return false;
124: }
125: $top1 = $this->getTop();
126: $top2 = $rect->getTop();
127: if ($top1 < $top2) {
128: return $top2 - $top1 < $this->getHeight();
129: } else if ($top1 >= $top2) {
130: return $top1 - $top2 < $rect->getHeight();
131: }
132: }
133:
134: 135: 136: 137: 138: 139:
140:
141: public function isIntersectsWith(Rectangle $rect) {
142: return $this->isIntersectsWithX($rect) && $this->isIntersectsWithY($rect);
143: }
144:
145: 146: 147: 148: 149: 150: 151:
152:
153: public function getIntersectsWith(Rectangle $rect) {
154: if (!$this->isIntersectsWith($rect)) {
155: return false;
156: }
157: $left = max($rect->getLeft(), $this->getLeft());
158: $top = max($rect->getTop(), $this->getTop());
159:
160: $right = min($rect->getRight(), $this->getRight());
161: $bottom = min($rect->getBottom(), $this->getBottom());
162:
163: $width = $right - $left;
164: $height = $bottom - $top;
165:
166: return new Rectangle($left, $top, $width, $height);
167: }
168:
169: 170: 171: 172: 173: 174:
175:
176: public function isInner(Rectangle $rect) {
177: return $this->getTop() >= 0 && $this->getLeft() >= 0 &&
178: $this->getBottom() <= $rect->getHeight() && $this->getRight() <= $rect->getWidth();
179: }
180:
181: 182: 183: 184: 185:
186:
187: public function isNull() {
188: return $this->getWidth() == 0 && $this->getHeight() == 0;
189: }
190:
191: 192: 193: 194: 195: 196: 197: 198: 199: 200:
201:
202: public function center() {
203: $args = func_get_args();
204: if (count($args) == 4) {
205: return $this->center(new Rectangle($args[0], $args[1], $args[2], $args[3]));
206: } else if (count($args) == 1 && $args[0] instanceof Rectangle) {
207: $rect = $args[0];
208:
209: $left = (int)(($rect->getWidth() - $this->getWidth()) / 2) + $rect->getLeft();
210: $top = (int)(($rect->getHeight() - $this->getHeight()) / 2) + $rect->getTop();
211:
212: $this->setLeft($left);
213: $this->setTop($top);
214: return $this;
215: }
216: throw new IllegalArgumentException();
217: }
218:
219: 220: 221: 222:
223:
224: public function isSquare() {
225: return $this->getWidth() == $this->getHeight();
226: }
227:
228:
229:
230: public function getLocation() {
231: return clone $this->location;
232: }
233:
234: public function getSize() {
235: return clone $this->size;
236: }
237:
238: public function getX() {
239: return $this->location->getX();
240: }
241:
242: public function getY() {
243: return $this->location->getY();
244: }
245:
246: public function getWidth() {
247: return abs($this->getSize()->getWidth());
248: }
249:
250: public function getHeight() {
251: return abs($this->getSize()->getHeight());
252: }
253:
254: public function getBottom() {
255: if ($this->getSize()->getHeight() > 0) {
256: return $this->getY() + $this->getHeight();
257: } else {
258: return $this->getY();
259: }
260: }
261:
262: public function getRight() {
263: if ($this->getSize()->getWidth() < 0) {
264: return $this->getX();
265: } else {
266: return $this->getX() + $this->getWidth();
267: }
268: }
269:
270: public function getTop() {
271: if ($this->getSize()->getHeight() > 0) {
272: return $this->getY();
273: } else {
274: return $this->getY() - $this->getHeight();
275: }
276: }
277:
278: public function getLeft() {
279: if ($this->getSize()->getWidth() > 0) {
280: return $this->getX();
281: } else {
282: return $this->getX() - $this->getWidth();
283: }
284: }
285:
286: public function getTopLeft() {
287: return new Point($this->getLeft(), $this->getTop());
288: }
289:
290:
291:
292: 293: 294:
295:
296: public function setLocation() {
297: $args = func_get_args();
298: if (count($args) == 2) {
299: $this->location = new Point($args[0], $args[1]);
300: } else if(count($args) == 1 && $args[0] instanceof Point) {
301: $this->location = $args[0];
302: } else {
303: throw new IllegalArgumentException();
304: }
305: }
306:
307: 308: 309:
310:
311: public function setSize() {
312: $args = func_get_args();
313: if (count($args) == 2) {
314: $this->size= new Size($args[0], $args[1]);
315: } else if(count($args) == 1 && $args[0] instanceof Size) {
316: $this->size = $args[0];
317: } else {
318: throw new IllegalArgumentException();
319: }
320: }
321:
322: 323: 324: 325:
326:
327: public function setX($x) {
328: $this->location->setX($x);
329: }
330:
331: 332: 333: 334:
335:
336: public function setY($y) {
337: $this->location->setY($y);
338: }
339:
340: 341: 342: 343:
344:
345: public function setWidth($width) {
346: $this->size->setWidth($width);
347: }
348:
349: 350: 351: 352:
353:
354: public function setHeight($height) {
355: $this->size->setHeight($height);
356: }
357:
358: 359: 360: 361: 362: 363:
364:
365: public function setTop($top) {
366: if (!is_int($top)) {
367: throw new IllegalArgumentException();
368: }
369:
370: if ($this->size->getHeight() > 0) {
371: $this->setY($top);
372: } else {
373: $y = $top + $this->getHeight();
374: $this->setY($y);
375: }
376: }
377:
378: 379: 380: 381: 382: 383:
384:
385: public function setLeft($left) {
386: if (!is_int($left)) {
387: throw new IllegalArgumentException();
388: }
389:
390: if ($this->size->getWidth() > 0) {
391: $this->setX($left);
392: } else {
393: $x = $left + $this->getWidth();
394: $this->setX($x);
395: }
396: }
397:
398: public function __toString() {
399: return "{location: {$this->location}, size: {$this->size}, left: {$this->getLeft()}, top: {$this->getTop()}}";
400: }
401: }
402: ?>