| 1 |
<?php |
| 2 |
/** |
| 3 |
* Copyright (C) 2020 Graham Breach |
| 4 |
* |
| 5 |
* This program is free software: you can redistribute it and/or modify |
| 6 |
* it under the terms of the GNU Lesser General Public License as published by |
| 7 |
* the Free Software Foundation, either version 3 of the License, or |
| 8 |
* (at your option) any later version. |
| 9 |
* |
| 10 |
* This program is distributed in the hope that it will be useful, |
| 11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 |
* GNU Lesser General Public License for more details. |
| 14 |
* |
| 15 |
* You should have received a copy of the GNU Lesser General Public License |
| 16 |
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 |
*/ |
| 18 |
/** |
| 19 |
* For more information, please contact <graham@goat1000.com> |
| 20 |
*/ |
| 21 |
|
| 22 |
namespace Goat1000\SVGGraph; |
| 23 |
|
| 24 |
/** |
| 25 |
* Class for measuring |
| 26 |
*/ |
| 27 |
class BoundingBox { |
| 28 |
|
| 29 |
public $x1, $x2, $y1, $y2; |
| 30 |
|
| 31 |
public function __construct($x1, $y1, $x2, $y2) |
| 32 |
{ |
| 33 |
$this->x1 = $x1; |
| 34 |
$this->x2 = $x2; |
| 35 |
$this->y1 = $y1; |
| 36 |
$this->y2 = $y2; |
| 37 |
} |
| 38 |
|
| 39 |
/** |
| 40 |
* Returns the width of the box |
| 41 |
*/ |
| 42 |
public function width() |
| 43 |
{ |
| 44 |
return $this->x2 - $this->x1; |
| 45 |
} |
| 46 |
|
| 47 |
/** |
| 48 |
* Returns the height of the box |
| 49 |
*/ |
| 50 |
public function height() |
| 51 |
{ |
| 52 |
return $this->y2 - $this->y1; |
| 53 |
} |
| 54 |
|
| 55 |
/** |
| 56 |
* Expands the box to fit the new sides |
| 57 |
*/ |
| 58 |
public function grow($x1, $y1, $x2, $y2) |
| 59 |
{ |
| 60 |
$this->x1 = min($this->x1, $x1); |
| 61 |
$this->y1 = min($this->y1, $y1); |
| 62 |
$this->x2 = max($this->x2, $x2); |
| 63 |
$this->y2 = max($this->y2, $y2); |
| 64 |
} |
| 65 |
|
| 66 |
/** |
| 67 |
* Expands using another BoundingBox |
| 68 |
*/ |
| 69 |
public function growBox(BoundingBox $box) |
| 70 |
{ |
| 71 |
$this->x1 = min($this->x1, $box->x1); |
| 72 |
$this->y1 = min($this->y1, $box->y1); |
| 73 |
$this->x2 = max($this->x2, $box->x2); |
| 74 |
$this->y2 = max($this->y2, $box->y2); |
| 75 |
} |
| 76 |
|
| 77 |
/** |
| 78 |
* Moves the box by $x, $y |
| 79 |
*/ |
| 80 |
public function offset($x, $y) |
| 81 |
{ |
| 82 |
$this->x1 += $x; |
| 83 |
$this->y1 += $y; |
| 84 |
$this->x2 += $x; |
| 85 |
$this->y2 += $y; |
| 86 |
} |
| 87 |
|
| 88 |
/** |
| 89 |
* Flips the Y-axis values |
| 90 |
*/ |
| 91 |
public function flipY() |
| 92 |
{ |
| 93 |
$tmp = $this->y1; |
| 94 |
$this->y1 = -$this->y2; |
| 95 |
$this->y2 = -$tmp; |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
|