Completed
Push — master ( 5bcf33...3feb07 )
by Dan
27:10
created

Box::intersects()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
ccs 5
cts 5
cp 1
rs 9.2
cc 4
eloc 5
nc 4
nop 1
crap 4
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 24
    public function __construct($x, $y, $width, $height)
32
    {
33 24
        $this->x = $x;
34 24
        $this->y = $y;
35 24
        $this->width = $width;
36 24
        $this->height = $height;
37
38 24
        $this->update();
39 24
    }
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 24
    protected function update()
47
    {
48 24
        $this->left = $this->x;
49 24
        $this->right = $this->x + $this->width;
50 24
        $this->top = $this->y;
51 24
        $this->bottom = $this->y + $this->height;
52 24
    }
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 10
    public function intersects(Box $box)
61
    {
62 10
        return ($this->getLeft() < $box->getRight()
63 10
            && $this->getRight() > $box->getLeft()
64 10
            && $this->getTop() < $box->getBottom()
65 10
            && $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
    public function resize($count)
93
    {
94
        return new self(
95
            $this->getX() - $count,
96
            $this->getY() - $count,
97
            $this->getWidth() + 2 * $count,
98
            $this->getHeight() + 2 * $count
99
        );
100
    }
101
102
    /**
103
     * @return int
104
     */
105 8
    public function getBottom()
106
    {
107 8
        return $this->bottom;
108
    }
109
110
    /**
111
     * @return int
112
     */
113 21
    public function getHeight()
114
    {
115 21
        return $this->height;
116
    }
117
118
    /**
119
     * @return int
120
     */
121 10
    public function getLeft()
122
    {
123 10
        return $this->left;
124
    }
125
126
    /**
127
     * @return int
128
     */
129 10
    public function getRight()
130
    {
131 10
        return $this->right;
132
    }
133
134
    /**
135
     * @return int
136
     */
137 8
    public function getTop()
138
    {
139 8
        return $this->top;
140
    }
141
142
    /**
143
     * @return int
144
     */
145 21
    public function getWidth()
146
    {
147 21
        return $this->width;
148
    }
149
150
    /**
151
     * @return int
152
     */
153 19
    public function getX()
154
    {
155 19
        return $this->x;
156
    }
157
158
    /**
159
     * @return int
160
     */
161 19
    public function getY()
162
    {
163 19
        return $this->y;
164
    }
165
166
    /**
167
     * @return Point
168
     */
169 4
    public function getPosition()
170
    {
171 4
        return new Point($this->getX(), $this->getY());
172
    }
173
174
    /**
175
     * @return \Imagine\Image\Box
176
     */
177 3
    public function getDimensions()
178
    {
179 3
        return new \Imagine\Image\Box($this->getWidth(), $this->getHeight());
180
    }
181
182
    function __toString()
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
183
    {
184
        return sprintf('(%s, %s) x (%s, %s)', $this->x, $this->y, $this->width, $this->height);
185
    }
186
}
187
188