Обзор

Пакеты

  • exceptions
  • geometry
  • image
  • PHP

Классы

  • Point
  • Rectangle
  • Size
  • Обзор
  • Пакет
  • Класс
  • Дерево
  1: <?php
  2: 
  3: /**
  4:  * @package geometry
  5:  * @author Антон Кургузенков <kurguzenkov@list.ru>
  6:  *
  7:  * @version 0.01
  8:  * @since 2012-11-09
  9:  */
 10: require_once 'exceptions.php';
 11: 
 12: /**
 13:  * Класс, описываюший точку на плоскости.
 14:  */
 15: class Point {
 16: 
 17:     /**
 18:      * Координата x точки
 19:      *
 20:      * @var int
 21:      */
 22:     private $x;
 23: 
 24:     /**
 25:      * Координата y точки
 26:      *
 27:      * @var int
 28:      */
 29:     private $y;
 30: 
 31:     /**
 32:      * @param int
 33:      * @param int
 34:      * @throws IllegalArgumentException
 35:      */
 36:     public function __construct($x, $y) {
 37:         $this->setX($x);
 38:         $this->setY($y);
 39:     }
 40: 
 41:     /**
 42:      * Смещает точку на указанную точку, складывая их координаты.
 43:      * Метод возвращает смещённую точку.
 44:      *
 45:      * @param Point
 46:      * @return Point
 47:      */
 48:     public function offset(Point $p) {
 49:         $this->setX($this->getX() + $p->getX());
 50:         $this->setY($this->getY() + $p->getY());
 51:         return $this;
 52:     }
 53: 
 54:     /**
 55:      * Складывает координаты точки с координатами точки или высотой и шириной
 56:      * размера и возвращяет новую точку.
 57:      *
 58:      * @param Point
 59:      * @param Point|Size
 60:      * @return Point
 61:      * @throws IllegalArgumentException
 62:      */
 63:     public static function add(Point $p, $obj) {
 64:         if ($obj instanceof Point) {
 65:             return new Point($p->getX() + $obj->getX(), $p->getY() + $obj->getY());
 66:         } else if ($obj instanceof Size) {
 67:             return new Point($p->getX() + $obj->getWidth(), $p->getY() + $obj->getHeight());
 68:         }
 69:         throw new IllegalArgumentException();
 70:     }
 71: 
 72:     /**
 73:      * Вычитает из координат точки координаты другой точки или
 74:      * высоту и ширину размера.
 75:      *
 76:      * @param Point
 77:      * @param Point|Size
 78:      * @return Point
 79:      * @throws IllegalArgumentException
 80:      */
 81:     public static function subtract(Point $p, $obj) {
 82:         if ($obj instanceof Point) {
 83:             return self::add($p, new Point(-$obj->getX(), -$obj->getY()));
 84:         } else if ($obj instanceof Size) {
 85:             return self::add($p, new Point(-$obj->getWidth(), -$obj->getHeight()));
 86:         }
 87:         throw new IllegalArgumentException();
 88:     }
 89: 
 90:     /**
 91:      * Сравнивает две точки
 92:      *
 93:      * @param Point
 94:      * @return boolean
 95:      */
 96:     public function equals(Point $p) {
 97:         return $this->getX() == $p->getX() && $this->getY() == $p->getY();
 98:     }
 99: 
100:     public function getX() {
101:         return $this->x;
102:     }
103: 
104:     public function getY() {
105:         return $this->y;
106:     }
107: 
108:     /**
109:      * @param int
110:      * @throws IllegalArgumentException
111:      */
112: 
113:     public function setX($x) {
114:         if (is_integer($x)) {
115:             $this->x = $x;
116:         } else {
117:             throw new IllegalArgumentException();
118:         }
119:     }
120: 
121:     /**
122:      * @param int
123:      * @throws IllegalArgumentException
124:      */
125: 
126:     public function setY($y) {
127:         if (is_integer($y)) {
128:             $this->y = $y;
129:         } else {
130:             throw new IllegalArgumentException();
131:         }
132:     }
133: 
134:     public function __toString() {
135:         return "{x: {$this->x}, y: {$this->y}}";
136:     }
137: 
138: }
139: 
140: ?>
API documentation generated by ApiGen 2.8.0