Обзор

Пакеты

  • 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: 
 11: require_once 'exceptions.php';
 12: 
 13: /**
 14:  * Класс, описывающий размер как пару высота-ширина.
 15:  */
 16: 
 17: class Size {
 18: 
 19:     /**
 20:      * ширина
 21:      *
 22:      * @var int
 23:      */
 24:     private $width;
 25: 
 26:     /**
 27:      * высота
 28:      *
 29:      * @var int
 30:      */
 31: 
 32:     private $height;
 33: 
 34:     /**
 35:      * @param int $width
 36:      * @param int $height
 37:      * @throws IllegalArgumentException
 38:      */
 39: 
 40:     public function __construct($width, $height) {
 41:         $this->setWidth($width);
 42:         $this->setHeight($height);
 43:     }
 44: 
 45:     /**
 46:      * Устанавливает больше ли, (по высоте <b>или</b> по ширине)
 47:      * размер заданного размера.
 48:      *
 49:      * @param Size $s
 50:      * @return boolean
 51:      */
 52: 
 53:     public function greatThen(Size $s) {
 54:         return $this->getWidth() > $s->getWidth() || $this->getHeight() > $s->getHeight();
 55:     }
 56: 
 57:     /**
 58:      * Устанавливает меньше ли, (по высоте <b>и</b> по ширине)
 59:      * размер заданного размера.
 60:      *
 61:      * @param Size $s
 62:      * @return bool
 63:      */
 64: 
 65:     public function lessThen(Size $s) {
 66:         return !$this->greatThen($s) && !$this->equals($s);
 67:     }
 68: 
 69:     /**
 70:      * Устанавливает, меньше или равен ли размер (по высоте <b>и</b> по ширине)
 71:      * заданного размера.
 72:      *
 73:      * @param Size
 74:      * @return bool
 75:      */
 76: 
 77:     public function isInner(Size $s) {
 78:         return $this->getWidth() <= $s->getWidth() && $this->getHeight() <= $s->getHeight();
 79:     }
 80: 
 81:     /**
 82:      * Устанавливает равен ли заданный размер текущему.
 83:      *
 84:      * @param Size
 85:      * @return bool
 86:      */
 87: 
 88:     public function equals(Size $s) {
 89:         return $this->getWidth() == $s->getWidth() && $this->getHeight() == $s->getHeight();
 90:     }
 91: 
 92:     /**
 93:      * Менеят местами высоту и ширину размера, "переварачивая" его.
 94:      * @return Size
 95:      */
 96: 
 97:     public function flip() {
 98:         $t = $this->getWidth();
 99:         $this->setWidth($this->getHeight());
100:         $this->setHeight($t);
101:         return $this;
102:     }
103: 
104:     /**
105:      * Пропорционально уменьшает размер по заданной ширине.
106:      * Возвращает новый размер, не изменяя старый.
107:      *
108:      * @param int $width
109:      * @return Size
110:      * @throws IllegalArgumentExceptions
111:      */
112: 
113:     public function getByWidth($width) {
114:         if (!is_integer($width)) {
115:             throw new IllegalArgumentExceptions();
116:         }
117: 
118:         if ($width >= $this->getWidth()) {
119:             return $this;
120:         }
121: 
122:         $height = (int) round($this->getHeight() * $width / $this->getWidth());
123:         return new Size($width, $height);
124:     }
125: 
126:     /**
127:      * Пропорционально уменьшает размер по заданной высоте.
128:      * Возвращает новый размер, не изменяя старый.
129:      *
130:      * @param int $height
131:      * @return Size
132:      * @throws IllegalArgumentException */
133: 
134:     public function getByHeight($height) {
135:         if (!is_integer($height)) {
136:             throw new IllegalArgumentException();
137:         }
138: 
139:         if ($height >= $this->getHeight()) {
140:             return $this;
141:         }
142: 
143:         $width = (int) round($this->getWidth() * $height / $this->getHeight());
144:         return new Size($width, $height);
145:     }
146: 
147:     /**
148:      * Вписывает размер в рамки, пропорционально уменьшая его.
149:      * Возвращает новый размер, не изменяя старый.
150:      *
151:      * @return Size
152:      * @throws IllegalArgumentException
153:      */
154: 
155:     public function getByFrame() { // Size $frame || $width, $height
156:         $args = func_get_args();
157:         if (count($args) == 2) {
158:             $this->getByFrame(new Size($args[0], $args[1]));
159:         } else if (count($args) == 1 && $args[0] instanceof Size) {
160:             $frame = $args[0];
161:         } else {
162:             throw new IllegalArgumentException();
163:         }
164: 
165:         if ($frame->getWidth() <= 0 || $frame->getHeight() <= 0)
166:             throw new IllegalArgumentException();
167: 
168:         if ($this->isInner($frame))
169:             return $this;
170: 
171:         $height = $frame->getHeight();
172:         $width = $frame->getWidth();
173: 
174:         if ($this->getWidth() / $width > $this->getHeight() / $height)
175:             return $this->getByWidth($width);
176: 
177:         return $this->getByHeight($height);
178:     }
179: 
180:     /**
181:      * Складывает высоту и ширину размера с координатами точки или
182:      * высотой и шириной другого размера и возвращает получившийся размер.
183:      *
184:      * @param Size $s
185:      * @param Size|Point $obj
186:      * @return Size
187:      * @throws IllegalArgumentException
188:      */
189: 
190:     public static function add(Size $s, $obj) {
191:         if($obj instanceof Size) {
192:             return new Size($s->getWidth() + $obj->getWidth(), $s->getHeight() + $obj->getHeight());
193:         } else if ($obj instanceof Point) {
194:             return new Size($s->getWidth() + $obj->getX(),  $s->getHeigth() + $obj->getY());
195:         }
196:         throw new IllegalArgumentException();
197:     }
198: 
199:     /**
200:      *
201:      * Вычитает из высоты и ширины размера координаты точки или
202:      * высоту и ширину другого размера и возвращает получившийся размер.
203:      *
204:      * @param Size $s
205:      * @param Size|Point $obj
206:      * @return Size
207:      * @throws IllegalArgumentException
208:      */
209: 
210:     public static function subtract(Size $s, $obj) {
211:         if($obj instanceof Size) {
212:             return new Size($s->getWidth() - $obj->getWidth(), $s->getHeight() - $obj->getHeight());
213:         } else if ($obj instanceof Point) {
214:             return new Size($s->getWidth() - $obj->getX(),  $s->getHeigth() - $obj->getY());
215:         }
216:         throw new IllegalArgumentException();
217:     }
218: 
219:     public function getWidth() {
220:         return $this->width;
221:     }
222: 
223:     public function getHeight() {
224:         return $this->height;
225:     }
226: 
227:     /**
228:      * @param int
229:      * @throws IllegalArgumentException
230:      */
231: 
232:     public function setWidth($width) {
233:         if (is_integer($width)) {
234:             $this->width = $width;
235:         } else {
236:             throw new IllegalArgumentException();
237:         }
238:     }
239: 
240:     /**
241:      * @param int
242:      * @throws IllegalArgumentException
243:      */
244: 
245:     public function setHeight($height) {
246:         if (is_integer($height)) {
247:             $this->height = $height;
248:         } else {
249:             throw new IllegalArgumentException();
250:         }
251:     }
252: 
253:     public function __toString() {
254:         return "{width: {$this->width}, height: {$this->height}}";
255:     }
256: }
257: ?>
API documentation generated by ApiGen 2.8.0