Обзор

Пакеты

  • exceptions
  • geometry
  • image
  • PHP

Классы

  • Point
  • Rectangle
  • Size
  • Обзор
  • Пакет
  • Класс
  • Дерево
  1: <?php
  2: 
  3: /**
  4:  * @package geometry
  5:  * @author Антон Кургузенков <kurguzenkov@list.ru>
  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:      * @var Point
 22:      */
 23:     private $location;
 24: 
 25:     /**
 26:      * Размер прямоугольника
 27:      *
 28:      * @var Size
 29:      */
 30: 
 31:     private $size;
 32: 
 33:     /**
 34:      * Принимает либо позицию прямоугольника (Point) и его размер (Size),
 35:      * либо координаты позиции, высоту и ширину.
 36:      *
 37:      * @throws IllegalArgumentException;
 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:      * @see Size::flip()
 58:      *
 59:      * @return Rectangle
 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:      * @return Rectangle
 76:      * @throws IllegalArgumentException
 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:      * с заданным по оси X
 95:      *
 96:      * @param Rectangle $rect
 97:      * @return boolean
 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:      * с заданным по оси Y
116:      *
117:      * @param Rectangle $rect
118:      * @return boolean
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:      * @param Rectangle $rect
138:      * @return type
139:      */
140: 
141:     public function isIntersectsWith(Rectangle $rect) {
142:         return $this->isIntersectsWithX($rect) && $this->isIntersectsWithY($rect);
143:     }
144: 
145:     /**
146:      * Возвращает пересечение текущего прямоугольника с заданным.
147:      * В случае, если они не пересекаются, возвращает false.
148:      *
149:      * @param Rectangle $rect
150:      * @return boolean|Rectangle
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:      * @param Rectangle $rect
173:      * @return type
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:      * @return type
185:      */
186: 
187:     public function isNull() {
188:         return $this->getWidth() == 0 && $this->getHeight() == 0;
189:     }
190: 
191:     /**
192:      *
193:      * Центрирует текущий прямоугольник относительно заданного.
194:      *
195:      * Принимает либо прямоугольник, относительно которого производится центрирование,
196:      * либо координаты позиции прямоугольника, его высоту и ширину.
197:      *
198:      * @return Rectangle
199:      * @throws IllegalArgumentException
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:      * @return bool
222:      */
223: 
224:     public function isSquare() {
225:         return $this->getWidth() == $this->getHeight();
226:     }
227: 
228:     // getters
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:     // setters
291: 
292:     /**
293:      * @throws IllegalArgumentException
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:      * @throws IllegalArgumentException
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:      * @param int
324:      * @throws IllegalArgumentException
325:      */
326: 
327:     public function setX($x) {
328:         $this->location->setX($x);
329:     }
330: 
331:     /**
332:      * @param int
333:      * @throws IllegalArgumentException
334:      */
335: 
336:     public function setY($y) {
337:         $this->location->setY($y);
338:     }
339: 
340:     /**
341:      * @param int
342:      * @throws IllegalArgumentException
343:      */
344: 
345:     public function setWidth($width) {
346:         $this->size->setWidth($width);
347:     }
348: 
349:     /**
350:      * @param int
351:      * @throws IllegalArgumentException
352:      */
353: 
354:     public function setHeight($height) {
355:         $this->size->setHeight($height);
356:     }
357: 
358:     /**
359:      * Изменяет y-координату верхнего левого угла.
360:      *
361:      * @param int
362:      * @throws IllegalArgumentException
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:      * Изменяет, x-координату ыерхнего левого угла
380:      *
381:      * @param int
382:      * @throws IllegalArgumentException
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: ?>
API documentation generated by ApiGen 2.8.0