-
Notifications
You must be signed in to change notification settings - Fork 0
/
geometry.php
468 lines (459 loc) · 21.8 KB
/
geometry.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
<?php
/**
* @file arkascha/urithmetic/geometry.php
* @copyright 2013-2014 Christian Reiner, Hamburg, Germany, mailto:[email protected]
* @license GNU Affero General Public license version 3 (AGPL)
* @author Christian Reiner <[email protected]>
* @brief Part of package 'PHP Urithmetic', an implementation of unit based arithmetic
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
* License as published by the Free Software Foundation; either
* version 3 of the license, or any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU AFFERO GENERAL PUBLIC LICENSE for more details.
*
* You should have received a copy of the GNU Affero General Public
* License along with this library.
* If not, see <http://www.gnu.org/licenses/>.
*
*/
namespace arkascha\urithmetic\geometry;
use \arkascha\urithmetic;
require_once 'urithmetic.php';
/**
* class Align
* @author Christian Reiner <[email protected]>
* @brief Geometric alignments
*/
class Align
{
const TOP = 1;
const BOTTOM = 2;
const LEFT = 4;
const RIGHT = 8;
} // class Align
/**
* @class Length
* @author Christian Reiner <[email protected]>
* @brief Unit based description of a length holding a single value: length
**/
class Length extends urithmetic\Container
{
static protected $type = __CLASS__;
static protected $attr = array('length');
protected function __construct($length, $unit)
{
$this->unit = urithmetic\Unit::validate($unit);
if (!($length instanceof urithmetic\Value))
throw new urithmetic\Exception(urithmetic\Exception::UNSUITED, __CLASS__, __FUNCTION__, "Unsuited value object 'length'", array('object'=>$length));
$this->length = $length->cloneToUnit($this->unit);
}
static public function fromLength ($length, $unit=NULL)
{
if (!($length instanceof Length))
throw new urithmetic\Exception(urithmetic\Exception::UNSUITED, __CLASS__, __FUNCTION__, "Unsuited length object 'length'", array('object'=>$length));
return $length->cloneToUnit($unit);
}
static public function fromValue ($length, $unit=NULL)
{
if (!($length instanceof urithmetic\Value))
throw new urithmetic\Exception(urithmetic\Exception::UNSUITED, __CLASS__, __FUNCTION__, "Unsuited value object 'length'", array('object'=>$length));
return new self($length, $unit?$unit:$length->unit);
}
static public function fromScalar ($length, $unit)
{
if (!(is_numeric($length)))
throw new urithmetic\Exception(urithmetic\Exception::UNSUITED, __CLASS__, __FUNCTION__, "Unsuited scalar object 'length'", array('object'=>$length));
return self::fromValue(urithmetic\Value::fromScalar($length, $unit), $unit);
}
public function getScalar($unit=NULL) {
$length = $this->length->cloneToUnit($unit);
return $length->toScalar(); }
public function getLength($unit=NULL) { return $this->length->cloneToUnit($unit); }
public function getValue ($unit=NULL) { return $this->length->cloneToUnit($unit); }
public function getSum ($length, $unit=NULL) { return Length::fromScalar($this->getScalar($unit)+$length->getScalar($unit), $unit?$unit:$this->unit); }
public function getDiff ($length, $unit=NULL) { return Length::fromScalar($this->getScalar($unit)-$length->getScalar($unit), $unit?$unit:$this->unit); }
public function getScaled($factor) { return self::fromScalar($factor*$this->getScalar(), $this->unit); }
} // class Length
/**
* @class Size
* @author Christian Reiner <[email protected]>
* @brief Unit based description of a size holding two values: width and height
**/
class Size extends urithmetic\Container
{
static protected $type = __CLASS__;
static protected $attr = array('width', 'height');
private function __construct($width, $height, $unit)
{
$this->unit = urithmetic\Unit::validate($unit);
if ($width instanceof Length)
$this->width = $width->morphToUnit($this->unit);
else throw new urithmetic\Exception(urithmetic\Exception::UNSUITED, __CLASS__, __FUNCTION__, "Unsuited length object 'width'", array('object'=>$width));
if ($height instanceof Length)
$this->height = $height->morphToUnit($this->unit);
else throw new urithmetic\Exception(urithmetic\Exception::UNSUITED, __CLASS__, __FUNCTION__, "Unsuited length object 'height'", array('object'=>$height));
}
/**
* @method fromLengths
* @brief Constructs a Units\Size object from two given Units\Length objects interpreted as width and height.
* @param $width urithmetic\Length width of the size
* @param $height urithmetic\Length height of the size
* @return Units\Size
* @access public
*/
static public function fromLengths ($width, $height, $unit)
{
foreach (array('width','height') as $attr)
if (!($$attr instanceof Length))
throw new urithmetic\Exception(urithmetic\Exception::UNSUITED, __CLASS__, __FUNCTION__, sprintf("Unsuited length object '%s'", $attr), array('object'=>$$attr));
return new self($width, $height, $unit?$unit:$width->unit);
}
/**
* @method fromValues
* @brief Constructs a Units\Size object from two given Units\Value objects interpreted as width and height.
* @param $width urithmetic\Value width of the size
* @param $height urithmetic\Value height of the size
* @return Units\Size
* @access public
*/
static public function fromValues ($width, $height, $unit=NULL)
{
foreach (array('width','height') as $attr)
if (!($$attr instanceof urithmetic\Value))
throw new urithmetic\Exception(urithmetic\Exception::UNSUITED, __CLASS__, __FUNCTION__, sprintf("Unsuited value object '%s'", $attr), array('object'=>$$attr));
return self::fromLengths(Length::fromValue($width, $unit), Length::fromValue($height, $unit), $unit);
}
/**
* @method fromScalars
* @brief Constructs a Units\Size object from two given numeric scalar values interpreted as width and height.
* @param $width numeric width of the size
* @param $height numeric height of the size
* @return Units\Size
* @access public
*/
static public function fromScalars($width, $height, $unit)
{
foreach (array('width','height') as $attr)
if (!(is_numeric($$attr)))
throw new urithmetic\Exception(urithmetic\Exception::UNSUITED, __CLASS__, __FUNCTION__, sprintf("Unsuited scalar object '%s'", $attr), array('object'=>$$attr));
return self::fromValues(urithmetic\Value::fromScalar($width, $unit), urithmetic\Value::fromScalar($height, $unit), $unit);
}
/**
* @method fromSize
* @brief Constructs a Units\Size object from another given Units\Size object.
* @param $size Units\Size object
* @return Units\Size
* @access public
*/
static public function fromSize ($size, $unit=NULL)
{
if (!($pos instanceof Size))
throw new urithmetic\Exception(urithmetic\Exception::UNSUITED, __CLASS__, __FUNCTION__, "Unsuited size object 'pos'", array('object'=>$size));
return $size->cloneToUnit($unit);
}
/**
* @method fromImageFile
* @brief Constructs a Units\Size object from a given image file specified as path or url.
* @param $file string Path or url of a file to be examined
* @return Units\Size
* @access public
*/
static public function fromImageFile($file, $unit)
{
if (empty($file) || !file_exists($file) || !is_readable($file))
throw new urithmetic\Exception(urithmetic\Exception::UNSUITED, __CLASS__, __FUNCTION__, "Unsuited path object 'file'", array('object'=>$file));
try { list($width, $height) = getimagesize($file); }
catch (urithmetic\Exception $e) { throw urithmetic\Exception::morphPHPException($e); }
return Size::fromLengths(Length::fromScalar($width, urithmetic\Unit::PT), Length::fromScalar($height, urithmetic\Unit::PT), $unit);
}
/**
* @method getScalars
* @brief Returns two numeric scalars, the width and the height of a Units\Size object
* @param $unit const An optional Units\Unit constant which specifies the desired output unit.
* @return array Associative array holding two numeric scalar values 'width' and 'height'
* @access public
*/
public function getScalars($unit=NULL)
{
$unit = $unit ? urithmetic\Unit::validate($unit) : $this->unit;
$width = $this->width->cloneToUnit($unit);
$height = $this->height->cloneToUnit($unit);
return array($width->getScalar(), $height->getScalar());
}
/**
* @method getWidth
* @brief Returns a Units\Length object, the width of a Units\Size object
* @param $unit const An optional Units\Unit constant which specifies the desired output unit.
* @return Units\Length object
* @access public
*/
public function getWidth ($unit=NULL) { return $this->width->cloneToUnit($unit); }
/**
* @method getHeight
* @brief Returns a Units\Length object, the height of a Units\Size object
* @param $unit const An optional Units\Unit constant which specifies the desired output unit.
* @return Units\Length object
* @access public
*/
public function getHeight ($unit=NULL) { return $this->height->cloneToUnit($unit); }
/**
* @method getScaled
* @brief Returns a scaled version of a Units\Size object
* @param $factor numeric A numeric factor by which the objects internal width and size is multiplied
* @return Units\Size object
* @access public
*/
public function getScaled ($factor) { return self::fromLengths($this->width->getScaled($factor), $this->height->getScaled($factor)); }
} // class Size
/**
* @class Pos
* @author Christian Reiner <[email protected]>
* @brief Unit based description of a position object holding two values: left and top
**/
class Pos extends urithmetic\Container
{
static protected $type = __CLASS__;
static protected $attr = array('left', 'top');
private function __construct($left, $top, $unit)
{
$this->unit = urithmetic\Unit::validate($unit);
if ($left instanceof Length)
$this->left = $left->morphToUnit($this->unit);
else throw new urithmetic\Exception(urithmetic\Exception::UNSUITED, __CLASS__, __FUNCTION__, "Unsuited length object 'left'", array('object'=>$left));
if ($top instanceof Length)
$this->top = $top->morphToUnit($this->unit);
else throw new urithmetic\Exception(urithmetic\Exception::UNSUITED, __CLASS__, __FUNCTION__, "Unsuited length object 'right'", array('object'=>$top));
}
static public function fromPos ($pos, $unit=NULL)
{
if ($pos instanceof Pos)
return $pos->cloneToUnit($unit);
else throw new urithmetic\Exception(urithmetic\Exception::UNSUITED, __CLASS__, __FUNCTION__, "Unsuited pos object 'pos'", array('object'=>$pos));
}
static public function fromLengths($left, $top, $unit=NULL)
{
foreach (array('left','top') as $attr)
if (!($$attr instanceof Length))
throw new urithmetic\Exception(urithmetic\Exception::UNSUITED, __CLASS__, __FUNCTION__, sprintf("Unsuited length object '%s'", $attr), array('object'=>$$attr));
return new self($left, $top, $unit?$unit:$left->unit);
}
static public function fromValues ($left, $top, $unit=NULL)
{
foreach (array('left','top') as $attr)
if (!($$attr instanceof urithmetic\Value))
throw new urithmetic\Exception(urithmetic\Exception::UNSUITED, __CLASS__, __FUNCTION__, sprintf("Unsuited value object '%s'", $attr), array('object'=>$$attr));
return self::fromLengths(Length::fromValue($left, $unit), Length::fromValue($top, $unit), $unit);
}
static public function fromScalars($left, $top, $unit)
{
foreach (array('left','top') as $attr)
if (!(is_numeric($$attr)))
throw new urithmetic\Exception(urithmetic\Exception::UNSUITED, __CLASS__, __FUNCTION__, sprintf("Unsuited scalar object '%s'", $attr), array('object'=>$$attr));
return self::fromValues(urithmetic\Value::fromScalar($left, $unit), urithmetic\Value::fromScalar($top, $unit), $unit);
}
static public function fromImageFile($file, $unit)
{
if (empty($file) || !file_exists($file) || !is_readable($file))
throw new urithmetic\Exception(urithmetic\Exception::UNSUITED, __CLASS__, __FUNCTION__, "Unsuited path object 'file'", array('object'=>$file));
try { list($width, $height) = getimagesize($file); }
catch (urithmetic\Exception $e) { throw urithmetic\Exception::morphPHPException($e); }
return Pos::fromScalars($width, $height, $unit);
}
public function getScalars($unit=NULL)
{
$unit = $unit ? urithmetic\Unit::validate($unit) : $this->unit;
$left = $this->left->cloneToUnit($unit);
$top = $this->top->cloneToUnit($unit);
return array($left->getScalar(), $top->getScalar());
}
public function getLeft ($unit=NULL) { return $this->left->cloneToUnit($unit); }
public function getTop ($unit=NULL) { return $this->top->cloneToUnit($unit); }
public function getScaled($factor) { return self::fromLengths($this->left->getScaled($factor), $this->top->getScaled($factor)); }
public function getSum ($pos, $unit=NULL)
{
if (!($pos instanceof Pos))
throw new urithmetic\Exception(urithmetic\Exception::UNSUITED, __CLASS__, __FUNCTION__, "Unsuited pos object 'pos'", array('object'=>$pos));
// morph to target unit
$unit = $unit ? urithmetic\Unit::validate($unit) : $this->unit;
$pos->morphToUnit($unit);
$this->morphToUnit($unit);
$this->left = $this->left->getSum($pos->left, $unit);
$this->top = $this->top->getSum($pos->top, $unit);
return $this;
}
public function getDiff ($pos, $unit=NULL)
{
if (!($pos instanceof Pos))
throw new urithmetic\Exception(urithmetic\Exception::UNSUITED, __CLASS__, __FUNCTION__, "Unsuited pos object 'pos'", array('object'=>$pos));
// morph to target unit
$unit = $unit ? urithmetic\Unit::validate($unit) : $this->unit;
$pos->morphToUnit($unit);
$this->morphToUnit($unit);
$this->left = $this->left->getDiff($pos->left, $unit);
$this->top = $this->top->getDiff($pos->top, $unit);
return $this;
}
} // class Pos
/**
* @class Area
* @author Christian Reiner <[email protected]>
* @brief Unit based description of an space object holding four values: left, top, right and bottom
**/
class Area extends urithmetic\Container
{
static protected $type = __CLASS__;
static protected $attr = array('pos', 'size');
private function __construct($pos, $size, $unit)
{
$this->unit = urithmetic\Unit::validate($unit);
if ($pos instanceof Pos)
$this->data['pos'] = $pos->morphToUnit($this->unit);
else throw new urithmetic\Exception(urithmetic\Exception::UNSUITED, __CLASS__, __FUNCTION__, 'Unsuited pos object', array('object'=>$pos));
if ($size instanceof Size)
$this->data['size'] = $size->morphToUnit($this->unit);
else throw new urithmetic\Exception(urithmetic\Exception::UNSUITED, __CLASS__, __FUNCTION__, 'Unsuited size object', array('object'=>$size));
}
static public function fromArea ($area, $unit=NULL)
{
if (!($area instanceof Area))
throw new urithmetic\Exception(urithmetic\Exception::UNSUITED, __CLASS__, __FUNCTION__, sprintf("Unsuited area object 'area'"), array('object'=>$area));
return $area->cloneToUnit($unit);
}
static public function fromPosSize($pos, $size, $unit=NULL)
{
if (!($pos instanceof Pos))
throw new urithmetic\Exception(urithmetic\Exception::UNSUITED, __CLASS__, __FUNCTION__, sprintf("Unsuited pos object '%pos'"), array('object'=>$pos));
if (!($size instanceof Size))
throw new urithmetic\Exception(urithmetic\Exception::UNSUITED, __CLASS__, __FUNCTION__, sprintf("Unsuited size object '%size'"), array('object'=>$size));
return new Area($pos, $size, $unit?$unit:$pos->unit);
}
static public function fromLengths ($left, $top, $width, $height, $unit=NULL)
{
foreach (array('left','top','width','height') as $attr)
if (!($$attr instanceof Length))
throw new urithmetic\Exception(urithmetic\Exception::UNSUITED, __CLASS__, __FUNCTION__, sprintf("Unsuited length object '%s'", $attr), array('object'=>$$attr));
return self::fromPosSize(Pos::fromLengths ($left, $top, $unit),
Size::fromLengths($width, $height, $unit));
}
static public function fromValues ($left, $top, $width, $height, $unit=NULL)
{
foreach (array('left','top','width','height') as $attr)
if (!($$attr instanceof urithmetic\Value))
throw new urithmetic\Exception(urithmetic\Exception::UNSUITED, __CLASS__, __FUNCTION__, sprintf("Unsuited value object '%s'", $attr), array('object'=>$$attr));
return self::fromLengths(Length::fromValue($left, $unit),
Length::fromValue($top, $unit),
Length::fromValue($width, $unit),
Length::fromValue($height, $unit), $unit);
}
static public function fromScalars($left, $top, $width, $height, $unit)
{
foreach (array('left','top','width','height') as $attr)
if (!(is_numeric($$attr)))
throw new urithmetic\Exception(urithmetic\Exception::UNSUITED, __CLASS__, __FUNCTION__, sprintf("Unsuited scalar object '%s'", $attr), array('object'=>$$attr));
return self::fromValues(urithmetic\Value::fromScalar($left, $unit),
urithmetic\Value::fromScalar($top, $unit),
urithmetic\Value::fromScalar($width, $unit),
urithmetic\Value::fromScalar($height, $unit), $unit);
}
static public function fromImageFile($file, $unit)
{
if (empty($file) || !file_exists($file) || !is_readable($file))
throw new urithmetic\Exception(urithmetic\Exception::UNSUITED, __CLASS__, __FUNCTION__, "Unsuited path object 'file'", array('object'=>$file));
try { list($width, $height) = getimagesize($file); }
catch (urithmetic\Exception $e) { throw urithmetic\Exception::morphPHPException($e); }
return Area::fromScalars(0, 0, $width, $height, $unit);
}
public function getScalars($unit=NULL)
{
$unit = $unit ? urithmetic\Unit::validate($unit) : $this->unit;
$pos = $this->pos->cloneToUnit($unit);
$size = $this->size->cloneToUnit($unit);
return array($pos->left->getScalar(), $pos->top->getScalar(), $size->width->getScalar(), $size->height->getScalar());
}
public function getSize ($unit=NULL) { return $this->size->morphToUnit($unit); }
public function getPos ($unit=NULL) { return $this->pos->morphToUnit($unit); }
public function getLeft ($unit=NULL) { return $this->pos->left->morphToUnit($unit); }
public function getTop ($unit=NULL) { return $this->pos->top->morphToUnit($unit); }
public function getWidth ($unit=NULL) { return $this->size->width->morphToUnit($unit); }
public function getHeight($unit=NULL) { return $this->size->height->morphToUnit($unit); }
public function getRight ($unit=NULL) { return $this->left->getSum($this->width, $unit); }
public function getBottom($unit=NULL) { return $this->top->getSum($this->height, $unit); }
public function getScaled($factor) { return self::fromPosSize($this->pos->getScaled($factor), $this->size->getScaled($factor)); }
public function moveTo($pos, $unit=NULL)
{
if (!($pos instanceof Pos))
throw new urithmetic\Exception(urithmetic\Exception::UNSUITED, __CLASS__, __FUNCTION__, "Unsuited pos object 'pos'", array('object'=>$pos));
// morph to target unit
$unit = $unit ? urithmetic\Unit::validate($unit) : $this->unit;
$pos->morphToUnit($unit);
$this->morphToUnit($unit);
// new pos
$this->pos = $pos;
return $this;
}
public function moveBy($pos, $unit=NULL)
{
if (!($pos instanceof Pos))
throw new urithmetic\Exception(urithmetic\Exception::UNSUITED, __CLASS__, __FUNCTION__, "Unsuited pos object 'pos'", array('object'=>$pos));
// morph to target unit
$unit = $unit ? urithmetic\Unit::validate($unit) : $this->unit;
$pos->morphToUnit($unit);
$this->morphToUnit($unit);
// new pos
$this->pos = $this->pos->getSum($pos, $unit);
return $this;
}
public function putSize($size, $align=0, $unit=NULL)
{
if (!($size instanceof Size))
throw new urithmetic\Exception(urithmetic\Exception::UNSUITED, __CLASS__, __FUNCTION__, "Unsuited size object 'size'", array('object'=>$size));
// morph to target unit
$unit = $unit ? urithmetic\Unit::validate($unit) : $this->unit;
$size->morphToUnit($unit);
$this->morphToUnit($unit);
// new size
$ratioWidth = $size->width->getScalar() / $this->size->width->getScalar();
$ratioHeight = $size->height->getScalar() / $this->size->height->getScalar();
$ratio = max($ratioWidth, $ratioHeight);
return (1>=$ratio) ? $size : Size::fromScalars($size->width->getScalar()/$ratio, $size->height->getScalar()/$ratio, $unit);
}
public function fitSize($size, $align=0, $unit=NULL)
{
// new size
$scaled = $this->putSize($size, $align, $unit);
// new pos
switch ($align &(~Align::TOP) &(~Align::BOTTOM)) {
case Align::LEFT:
$newPosLeft = $this->pos->left;
break;
case Align::RIGHT:
$widthDiff = $this->size->width->getDiff($scaled->width);
$newPosLeft = $this->pos->left->getSum($widthDiff);
break;
default:
$widthDiff = $this->size->width->getDiff($scaled->width);
$newPosLeft = $this->pos->left->getSum($widthDiff->getScaled(0.5));
}
switch ($align &(~Align::LEFT) &(~Align::RIGHT)) {
case Align::TOP:
$newPosTop = $this->pos->top;
break;
case Align::BOTTOM:
$heightDiff = $this->size->height->getDiff($scaled->height);
$newPosTop = $this->pos->top->getSum($heightDiff);
break;
default:
$heightDiff = $this->size->height->getDiff($scaled->height);
$newPosTop = $this->pos->top->getSum($heightDiff->getScaled(0.5));
}
$newPos = Pos::fromLengths($newPosLeft, $newPosTop);
// final result is a area
return Area::fromPosSize($newPos, $scaled);
} // function fitSize
} // class Area
?>