1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SixtyNine\DataTypes; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* An axis-aligned rectangle with collision detection |
7
|
|
|
*/ |
8
|
|
|
class Box |
9
|
|
|
{ |
10
|
|
|
/** @var float */ |
11
|
|
|
protected $x; |
12
|
|
|
|
13
|
|
|
/** @var float */ |
14
|
|
|
protected $y; |
15
|
|
|
|
16
|
|
|
/** @var float */ |
17
|
|
|
protected $width; |
18
|
|
|
|
19
|
|
|
/** @var float */ |
20
|
|
|
protected $height; |
21
|
|
|
|
22
|
|
|
/** @var float */ |
23
|
|
|
protected $top; |
24
|
|
|
|
25
|
|
|
/** @var float */ |
26
|
|
|
protected $bottom; |
27
|
|
|
|
28
|
|
|
/** @var float */ |
29
|
|
|
protected $left; |
30
|
|
|
|
31
|
|
|
/** @var float */ |
32
|
|
|
protected $right; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Constructor |
36
|
|
|
* @param float $x |
37
|
|
|
* @param float $y |
38
|
|
|
* @param float $width |
39
|
|
|
* @param float $height |
40
|
|
|
*/ |
41
|
26 |
|
public function __construct($x, $y, $width, $height) |
42
|
|
|
{ |
43
|
26 |
|
$this->x = $x; |
44
|
26 |
|
$this->y = $y; |
45
|
26 |
|
$this->width = $width; |
46
|
26 |
|
$this->height = $height; |
47
|
|
|
|
48
|
26 |
|
$this->update(); |
49
|
26 |
|
} |
50
|
|
|
|
51
|
10 |
|
protected function strictComparator($x, $y) { |
52
|
10 |
|
return $x < $y; |
53
|
|
|
} |
54
|
|
|
|
55
|
5 |
|
protected function nonStrictComparator($x, $y) { |
56
|
5 |
|
return $x <= $y; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Factory method. |
61
|
|
|
* @param float $x |
62
|
|
|
* @param float $y |
63
|
|
|
* @param float $width |
64
|
|
|
* @param float $height |
65
|
|
|
* @return Box |
66
|
|
|
*/ |
67
|
5 |
|
public static function create($x, $y, $width, $height) : Box |
68
|
|
|
{ |
69
|
5 |
|
return new self($x, $y, $width, $height); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Update the left, right, top, and bottom coordinates. |
74
|
|
|
*/ |
75
|
26 |
|
public function update() |
76
|
|
|
{ |
77
|
26 |
|
$this->left = $this->x; |
78
|
26 |
|
$this->right = $this->x + $this->width; |
79
|
26 |
|
$this->top = $this->y; |
80
|
26 |
|
$this->bottom = $this->y + $this->height; |
81
|
26 |
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Detect box collision |
85
|
|
|
* This algorithm only works with Axis-Aligned boxes! |
86
|
|
|
* @param Box $box The other rectangle to test collision with |
87
|
|
|
* @param bool $strict If true, boxes "touching" each other are not intersecting, otherwise they are |
88
|
|
|
* @return boolean True is the boxes collide, false otherwise |
89
|
|
|
*/ |
90
|
9 |
|
public function intersects(Box $box, $strict = true) : bool |
91
|
|
|
{ |
92
|
9 |
|
$comparator = $strict ? array($this, 'strictComparator') : array($this, 'nonStrictComparator'); |
93
|
|
|
|
94
|
9 |
|
return $comparator($this->getLeft(), $box->getRight()) |
95
|
9 |
|
&& $comparator($box->getLeft(), $this->getRight()) |
96
|
9 |
|
&& $comparator($this->getTop(), $box->getBottom()) |
97
|
9 |
|
&& $comparator($box->getTop(), $this->getBottom()); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @param Box $box |
102
|
|
|
* @return bool |
103
|
|
|
*/ |
104
|
6 |
|
public function inside(Box $box, $strict = false) : bool |
105
|
|
|
{ |
106
|
6 |
|
$comparator = $strict ? array($this, 'strictComparator') : array($this, 'nonStrictComparator'); |
107
|
|
|
|
108
|
6 |
|
return $comparator($box->getLeft(), $this->getLeft()) |
109
|
6 |
|
&& $comparator($this->getRight(), $box->getRight()) |
110
|
6 |
|
&& $comparator($box->getTop(), $this->getTop()) |
111
|
6 |
|
&& $comparator($this->getBottom(), $box->getBottom()); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @param float $deltaX |
116
|
|
|
* @param float $deltaY |
117
|
|
|
* @return Box |
118
|
|
|
*/ |
119
|
1 |
|
public function move($deltaX, $deltaY) : Box |
120
|
|
|
{ |
121
|
1 |
|
return new self($this->getX() + $deltaX, $this->getY() + $deltaY, $this->getWidth(), $this->getHeight()); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* @param int $increment |
126
|
|
|
* @return Box |
127
|
|
|
*/ |
128
|
1 |
|
public function resize($increment) : Box |
129
|
|
|
{ |
130
|
1 |
|
return new self( |
131
|
1 |
|
$this->getX() - $increment, |
132
|
1 |
|
$this->getY() - $increment, |
133
|
1 |
|
$this->getWidth() + 2 * $increment, |
134
|
1 |
|
$this->getHeight() + 2 * $increment |
135
|
|
|
); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* @return float |
140
|
|
|
*/ |
141
|
14 |
|
public function getBottom() : float |
142
|
|
|
{ |
143
|
14 |
|
return $this->bottom; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* @return float |
148
|
|
|
*/ |
149
|
19 |
|
public function getHeight() : float |
150
|
|
|
{ |
151
|
19 |
|
return $this->height; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* @return float |
156
|
|
|
*/ |
157
|
20 |
|
public function getLeft() : float |
158
|
|
|
{ |
159
|
20 |
|
return $this->left; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* @return float |
164
|
|
|
*/ |
165
|
17 |
|
public function getRight() : float |
166
|
|
|
{ |
167
|
17 |
|
return $this->right; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* @return float |
172
|
|
|
*/ |
173
|
14 |
|
public function getTop() : float |
174
|
|
|
{ |
175
|
14 |
|
return $this->top; |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* @return float |
180
|
|
|
*/ |
181
|
19 |
|
public function getWidth() : float |
182
|
|
|
{ |
183
|
19 |
|
return $this->width; |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* @return float |
188
|
|
|
*/ |
189
|
21 |
|
public function getX() : float |
190
|
|
|
{ |
191
|
21 |
|
return $this->x; |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* @return float |
196
|
|
|
*/ |
197
|
21 |
|
public function getY() : float |
198
|
|
|
{ |
199
|
21 |
|
return $this->y; |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* @return Vector |
204
|
|
|
*/ |
205
|
1 |
|
public function getPosition() : Vector |
206
|
|
|
{ |
207
|
1 |
|
return new Vector($this->getX(), $this->getY()); |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* @return Vector |
212
|
|
|
*/ |
213
|
1 |
|
public function getDimensions() : Vector |
214
|
|
|
{ |
215
|
1 |
|
return new Vector($this->getWidth(), $this->getHeight()); |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
/** |
219
|
|
|
* @return Vector |
220
|
|
|
*/ |
221
|
18 |
|
public function getCenter() : Vector |
222
|
|
|
{ |
223
|
18 |
|
return new Vector( |
224
|
18 |
|
$this->x + $this->width / 2, |
225
|
18 |
|
$this->y + $this->height / 2 |
226
|
|
|
); |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
/** |
230
|
|
|
* @return string |
231
|
|
|
*/ |
232
|
1 |
|
public function __toString() : string |
233
|
|
|
{ |
234
|
1 |
|
return sprintf('[%s, %s] x [%s, %s]', $this->x, $this->y, $this->width, $this->height); |
235
|
|
|
} |
236
|
|
|
|
237
|
1 |
|
public function serialize(): string |
238
|
|
|
{ |
239
|
1 |
|
return json_encode([ |
240
|
1 |
|
'x' => $this->x, |
241
|
1 |
|
'y' => $this->y, |
242
|
1 |
|
'width' => $this->width, |
243
|
1 |
|
'height' => $this->height, |
244
|
|
|
]); |
245
|
|
|
} |
246
|
|
|
} |
247
|
|
|
|