Completed
Push — master ( b97227...875476 )
by Dan
02:57
created

Box::getRight()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
namespace SixtyNine\Cloud\Model;
4
5
use Imagine\Image\BoxInterface;
6
use Imagine\Image\PointInterface;
7
8
/**
9
 * An axis-aligned rectangle with collision detection
10
 */
11
class Box
12
{
13
    /** @var int */
14
    protected $x;
15
    /** @var int */
16
    protected $y;
17
    /** @var int */
18
    protected $width;
19
    /** @var int */
20
    protected $height;
21
    /** @var int */
22
    protected $top;
23
    /** @var int */
24
    protected $bottom;
25
    /** @var int */
26
    protected $left;
27
    /** @var int */
28
    protected $right;
29
30 2
    public function __construct($x, $y, $width, $height)
31
    {
32 2
        $this->x = $x;
33 2
        $this->y = $y;
34 2
        $this->width = $width;
35 2
        $this->height = $height;
36
37 2
        $this->update();
38 2
    }
39
40 1
    public static function constructFromImagine(PointInterface $point, BoxInterface $box)
41
    {
42 1
        return new self($point->getX(), $point->getY(), $box->getWidth(), $box->getHeight());
43
    }
44
45 2
    protected function update()
46
    {
47 2
        $this->left = $this->x;
48 2
        $this->right = $this->x + $this->width;
49 2
        $this->top = $this->y;
50 2
        $this->bottom = $this->y + $this->height;
51 2
    }
52
53
    /**
54
     * Detect box collision
55
     * This algorithm only works with Axis-Aligned boxes!
56
     * @param Box $box The other rectangle to test collision with
57
     * @return boolean True is the boxes collide, false otherwise
58
     */
59 4
    function intersects(Box $box)
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...
60
    {
61 4
        return ($this->getLeft() < $box->getRight() &&
62 4
           $this->getRight() > $box->getLeft() &&
63 4
           $this->getTop() < $box->getBottom() &&
64 4
           $this->getBottom() > $box->getTop())
65
        ;
66
    }
67
68
    /**
69
     * @return int
70
     */
71 2
    public function getBottom()
72
    {
73 2
        return $this->bottom;
74
    }
75
76
    /**
77
     * @return int
78
     */
79
    public function getHeight()
80
    {
81
        return $this->height;
82
    }
83
84
    /**
85
     * @return int
86
     */
87 4
    public function getLeft()
88
    {
89 4
        return $this->left;
90
    }
91
92
    /**
93
     * @return int
94
     */
95 4
    public function getRight()
96
    {
97 4
        return $this->right;
98
    }
99
100
    /**
101
     * @return int
102
     */
103 2
    public function getTop()
104
    {
105 2
        return $this->top;
106
    }
107
108
    /**
109
     * @return int
110
     */
111
    public function getWidth()
112
    {
113
        return $this->width;
114
    }
115
116
    /**
117
     * @return int
118
     */
119
    public function getX()
120
    {
121
        return $this->x;
122
    }
123
124
    /**
125
     * @return int
126
     */
127
    public function getY()
128
    {
129
        return $this->y;
130
    }
131
}
132
133