1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SixtyNine\Cloud\Model; |
4
|
|
|
|
5
|
|
|
use Imagine\Image\BoxInterface; |
6
|
|
|
use Imagine\Image\Point; |
7
|
|
|
use Imagine\Image\PointInterface; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* An axis-aligned rectangle with collision detection |
11
|
|
|
*/ |
12
|
|
|
class Box |
13
|
|
|
{ |
14
|
|
|
/** @var int */ |
15
|
|
|
protected $x; |
16
|
|
|
/** @var int */ |
17
|
|
|
protected $y; |
18
|
|
|
/** @var int */ |
19
|
|
|
protected $width; |
20
|
|
|
/** @var int */ |
21
|
|
|
protected $height; |
22
|
|
|
/** @var int */ |
23
|
|
|
protected $top; |
24
|
|
|
/** @var int */ |
25
|
|
|
protected $bottom; |
26
|
|
|
/** @var int */ |
27
|
|
|
protected $left; |
28
|
|
|
/** @var int */ |
29
|
|
|
protected $right; |
30
|
|
|
|
31
|
8 |
|
public function __construct($x, $y, $width, $height) |
32
|
|
|
{ |
33
|
8 |
|
$this->x = $x; |
34
|
8 |
|
$this->y = $y; |
35
|
8 |
|
$this->width = $width; |
36
|
8 |
|
$this->height = $height; |
37
|
|
|
|
38
|
8 |
|
$this->update(); |
39
|
8 |
|
} |
40
|
|
|
|
41
|
1 |
|
public static function constructFromImagine(PointInterface $point, BoxInterface $box) |
42
|
|
|
{ |
43
|
1 |
|
return new self($point->getX(), $point->getY(), $box->getWidth(), $box->getHeight()); |
44
|
|
|
} |
45
|
|
|
|
46
|
8 |
|
protected function update() |
47
|
|
|
{ |
48
|
8 |
|
$this->left = $this->x; |
49
|
8 |
|
$this->right = $this->x + $this->width; |
50
|
8 |
|
$this->top = $this->y; |
51
|
8 |
|
$this->bottom = $this->y + $this->height; |
52
|
8 |
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Detect box collision |
56
|
|
|
* This algorithm only works with Axis-Aligned boxes! |
57
|
|
|
* @param Box $box The other rectangle to test collision with |
58
|
|
|
* @return boolean True is the boxes collide, false otherwise |
59
|
|
|
*/ |
60
|
8 |
|
public function intersects(Box $box) |
61
|
|
|
{ |
62
|
8 |
|
return ($this->getLeft() < $box->getRight() |
63
|
8 |
|
&& $this->getRight() > $box->getLeft() |
64
|
8 |
|
&& $this->getTop() < $box->getBottom() |
65
|
8 |
|
&& $this->getBottom() > $box->getTop() |
66
|
|
|
); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @param Box $box |
71
|
|
|
* @return bool |
72
|
|
|
*/ |
73
|
4 |
|
public function inside(Box $box) |
74
|
|
|
{ |
75
|
4 |
|
return ($this->getLeft() >= $box->getLeft() |
76
|
4 |
|
&& $this->getRight() <= $box->getRight() |
77
|
4 |
|
&& $this->getTop() >= $box->getTop() |
78
|
4 |
|
&& $this->getBottom() <= $box->getBottom() |
79
|
|
|
); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @param int $deltaX |
84
|
|
|
* @param int $deltaY |
85
|
|
|
* @return \SixtyNine\Cloud\Model\Box |
86
|
|
|
*/ |
87
|
4 |
|
public function move($deltaX, $deltaY) |
88
|
|
|
{ |
89
|
4 |
|
return new self($this->getX() + $deltaX, $this->getY() + $deltaY, $this->getWidth(), $this->getHeight()); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @return int |
94
|
|
|
*/ |
95
|
6 |
|
public function getBottom() |
96
|
|
|
{ |
97
|
6 |
|
return $this->bottom; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @return int |
102
|
|
|
*/ |
103
|
6 |
|
public function getHeight() |
104
|
|
|
{ |
105
|
6 |
|
return $this->height; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @return int |
110
|
|
|
*/ |
111
|
8 |
|
public function getLeft() |
112
|
|
|
{ |
113
|
8 |
|
return $this->left; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @return int |
118
|
|
|
*/ |
119
|
8 |
|
public function getRight() |
120
|
|
|
{ |
121
|
8 |
|
return $this->right; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* @return int |
126
|
|
|
*/ |
127
|
6 |
|
public function getTop() |
128
|
|
|
{ |
129
|
6 |
|
return $this->top; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* @return int |
134
|
|
|
*/ |
135
|
6 |
|
public function getWidth() |
136
|
|
|
{ |
137
|
6 |
|
return $this->width; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* @return int |
142
|
|
|
*/ |
143
|
4 |
|
public function getX() |
144
|
|
|
{ |
145
|
4 |
|
return $this->x; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* @return int |
150
|
|
|
*/ |
151
|
4 |
|
public function getY() |
152
|
|
|
{ |
153
|
4 |
|
return $this->y; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* @return Point |
158
|
|
|
*/ |
159
|
4 |
|
public function getPosition() |
160
|
|
|
{ |
161
|
4 |
|
return new Point($this->getX(), $this->getY()); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* @return \Imagine\Image\Box |
166
|
|
|
*/ |
167
|
3 |
|
public function getDimensions() |
168
|
|
|
{ |
169
|
3 |
|
return new \Imagine\Image\Box($this->getWidth(), $this->getHeight()); |
170
|
|
|
} |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
|