Box::getY()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
namespace SixtyNine\DataTypes;
4
5
/**
6
 * An axis-aligned rectangle with collision detection
7
 * @package SixtyNine\DataTypes
8
 * @SuppressWarnings(PHPMD.ShortVariable)
9
 */
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
119
    {
120 1
        return new self($this->getX() + $deltaX, $this->getY() + $deltaY, $this->getWidth(), $this->getHeight());
121
    }
122
123
    /**
124
     * @param int $increment
125
     * @return Box
126
     */
127 1
    public function resize($increment) : Box
128
    {
129 1
        return new self(
130 1
            $this->getX() - $increment,
131 1
            $this->getY() - $increment,
132 1
            $this->getWidth() + 2 * $increment,
133 1
            $this->getHeight() + 2 * $increment
134
        );
135
    }
136
137
    /**
138
     * @return float
139
     */
140 14
    public function getBottom() : float
141
    {
142 14
        return $this->bottom;
143
    }
144
145
    /**
146
     * @return float
147
     */
148 19
    public function getHeight() : float
149
    {
150 19
        return $this->height;
151
    }
152
153
    /**
154
     * @return float
155
     */
156 20
    public function getLeft() : float
157
    {
158 20
        return $this->left;
159
    }
160
161
    /**
162
     * @return float
163
     */
164 17
    public function getRight() : float
165
    {
166 17
        return $this->right;
167
    }
168
169
    /**
170
     * @return float
171
     */
172 14
    public function getTop() : float
173
    {
174 14
        return $this->top;
175
    }
176
177
    /**
178
     * @return float
179
     */
180 19
    public function getWidth() : float
181
    {
182 19
        return $this->width;
183
    }
184
185
    /**
186
     * @return float
187
     */
188 21
    public function getX() : float
189
    {
190 21
        return $this->x;
191
    }
192
193
    /**
194
     * @return float
195
     */
196 21
    public function getY() : float
197
    {
198 21
        return $this->y;
199
    }
200
201
    /**
202
     * @return Vector
203
     */
204 1
    public function getPosition() : Vector
205
    {
206 1
        return new Vector($this->getX(), $this->getY());
207
    }
208
209
    /**
210
     * @return Vector
211
     */
212 1
    public function getDimensions() : Vector
213
    {
214 1
        return new Vector($this->getWidth(), $this->getHeight());
215
    }
216
217
    /**
218
     * @return Vector
219
     */
220 18
    public function getCenter() : Vector
221
    {
222 18
        return new Vector(
223 18
            $this->x + $this->width / 2,
224 18
            $this->y + $this->height / 2
225
        );
226
    }
227
228
    /**
229
     * @return string
230
     */
231 1
    public function __toString() : string
232
    {
233 1
        return sprintf('[%s, %s] x [%s, %s]', $this->x, $this->y, $this->width, $this->height);
234
    }
235
236 1
    public function serialize(): string
237
    {
238 1
        return json_encode([
239 1
            'x' => $this->x,
240 1
            'y' => $this->y,
241 1
            'width' => $this->width,
242 1
            'height' => $this->height,
243
        ]);
244
    }
245
}
246