1 | <?php |
||
10 | class Box |
||
11 | { |
||
12 | /** @var float */ |
||
13 | protected $x; |
||
14 | |||
15 | /** @var float */ |
||
16 | protected $y; |
||
17 | |||
18 | /** @var float */ |
||
19 | protected $width; |
||
20 | |||
21 | /** @var float */ |
||
22 | protected $height; |
||
23 | |||
24 | /** @var float */ |
||
25 | protected $top; |
||
26 | |||
27 | /** @var float */ |
||
28 | protected $bottom; |
||
29 | |||
30 | /** @var float */ |
||
31 | protected $left; |
||
32 | |||
33 | /** @var float */ |
||
34 | protected $right; |
||
35 | |||
36 | /** |
||
37 | * Constructor |
||
38 | * @param float $x |
||
39 | * @param float $y |
||
40 | * @param float $width |
||
41 | * @param float $height |
||
42 | */ |
||
43 | 26 | public function __construct($x, $y, $width, $height) |
|
44 | { |
||
45 | 26 | $this->x = $x; |
|
46 | 26 | $this->y = $y; |
|
47 | 26 | $this->width = $width; |
|
48 | 26 | $this->height = $height; |
|
49 | |||
50 | 26 | $this->update(); |
|
51 | 26 | } |
|
52 | |||
53 | /** |
||
54 | * Factory method. |
||
55 | * @param float $x |
||
56 | * @param float $y |
||
57 | * @param float $width |
||
58 | * @param float $height |
||
59 | * @return Box |
||
60 | */ |
||
61 | 5 | public static function create($x, $y, $width, $height) : Box |
|
62 | { |
||
63 | 5 | return new self($x, $y, $width, $height); |
|
64 | } |
||
65 | |||
66 | /** |
||
67 | * Update the left, right, top, and bottom coordinates. |
||
68 | */ |
||
69 | 26 | public function update() |
|
70 | { |
||
71 | 26 | $this->left = $this->x; |
|
72 | 26 | $this->right = $this->x + $this->width; |
|
73 | 26 | $this->top = $this->y; |
|
74 | 26 | $this->bottom = $this->y + $this->height; |
|
75 | 26 | } |
|
76 | |||
77 | /** |
||
78 | * Detect box collision |
||
79 | * This algorithm only works with Axis-Aligned boxes! |
||
80 | * @param Box $box The other rectangle to test collision with |
||
81 | * @param bool $strict If true, boxes "touching" each other are not intersecting, otherwise they are |
||
82 | * @return boolean True is the boxes collide, false otherwise |
||
83 | * @SuppressWarnings(PHPMD.BooleanArgumentFlag) |
||
84 | */ |
||
85 | 9 | public function intersects(Box $box, $strict = true) : bool |
|
86 | { |
||
87 | 9 | $class = Comparators::class; |
|
88 | 9 | $comparator = $strict ? array($class, 'strictComparator') : array($class, 'nonStrictComparator'); |
|
89 | |||
90 | 9 | return $comparator($this->getLeft(), $box->getRight()) |
|
91 | 9 | && $comparator($box->getLeft(), $this->getRight()) |
|
92 | 9 | && $comparator($this->getTop(), $box->getBottom()) |
|
93 | 9 | && $comparator($box->getTop(), $this->getBottom()); |
|
94 | } |
||
95 | |||
96 | /** |
||
97 | * @param Box $box |
||
98 | * @param bool $strict |
||
99 | * @return bool |
||
100 | * @SuppressWarnings(PHPMD.BooleanArgumentFlag) |
||
101 | */ |
||
102 | 6 | public function inside(Box $box, $strict = false) : bool |
|
103 | { |
||
104 | 6 | $class = Comparators::class; |
|
105 | 6 | $comparator = $strict ? array($class, 'strictComparator') : array($class, 'nonStrictComparator'); |
|
106 | |||
107 | 6 | return $comparator($box->getLeft(), $this->getLeft()) |
|
108 | 6 | && $comparator($this->getRight(), $box->getRight()) |
|
109 | 6 | && $comparator($box->getTop(), $this->getTop()) |
|
110 | 6 | && $comparator($this->getBottom(), $box->getBottom()); |
|
111 | } |
||
112 | |||
113 | /** |
||
114 | * @param float $deltaX |
||
115 | * @param float $deltaY |
||
116 | * @return Box |
||
117 | */ |
||
118 | 1 | public function move($deltaX, $deltaY) : Box |
|
122 | |||
123 | /** |
||
124 | * @param int $increment |
||
125 | * @return Box |
||
126 | */ |
||
127 | 1 | public function resize($increment) : Box |
|
136 | |||
137 | /** |
||
138 | * @return float |
||
139 | */ |
||
140 | 14 | public function getBottom() : float |
|
144 | |||
145 | /** |
||
146 | * @return float |
||
147 | */ |
||
148 | 19 | public function getHeight() : float |
|
152 | |||
153 | /** |
||
154 | * @return float |
||
155 | */ |
||
156 | 20 | public function getLeft() : float |
|
160 | |||
161 | /** |
||
162 | * @return float |
||
163 | */ |
||
164 | 17 | public function getRight() : float |
|
168 | |||
169 | /** |
||
170 | * @return float |
||
171 | */ |
||
172 | 14 | public function getTop() : float |
|
176 | |||
177 | /** |
||
178 | * @return float |
||
179 | */ |
||
180 | 19 | public function getWidth() : float |
|
184 | |||
185 | /** |
||
186 | * @return float |
||
187 | */ |
||
188 | 21 | public function getX() : float |
|
192 | |||
193 | /** |
||
194 | * @return float |
||
195 | */ |
||
196 | 21 | public function getY() : float |
|
200 | |||
201 | /** |
||
202 | * @return Vector |
||
203 | */ |
||
204 | 1 | public function getPosition() : Vector |
|
208 | |||
209 | /** |
||
210 | * @return Vector |
||
211 | */ |
||
212 | 1 | public function getDimensions() : Vector |
|
216 | |||
217 | /** |
||
218 | * @return Vector |
||
219 | */ |
||
220 | 18 | public function getCenter() : Vector |
|
227 | |||
228 | /** |
||
229 | * @return string |
||
230 | */ |
||
231 | 1 | public function __toString() : string |
|
235 | |||
236 | 1 | public function serialize(): string |
|
245 | } |
||
246 |