Completed
Push — master ( 1ba964...086041 )
by Dan
02:34
created

Mask::intersects()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 19
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 5

Importance

Changes 0
Metric Value
dl 0
loc 19
ccs 10
cts 10
cp 1
rs 8.8571
c 0
b 0
f 0
cc 5
eloc 12
nc 5
nop 4
crap 5
1
<?php
2
3
namespace SixtyNine\Cloud;
4
5
6
use Imagine\Image\BoxInterface;
7
use Imagine\Image\PointInterface;
8
9
class Mask
10
{
11
    private $drawnBoxes = array();
12
13
    /** {@inheritdoc} */
14 4
    public function add(PointInterface $pos, BoxInterface $box)
15
    {
16 4
        $this->drawnBoxes[] = array(
17 4
            'pos' => $pos,
18 4
            'box' => $box
19
        );
20 4
    }
21
22
    public function reset()
23
    {
24
        $this->drawnBoxes = array();
25
    }
26
27 4
    public function overlaps(PointInterface $pos, BoxInterface $box)
28
    {
29 4
        foreach ($this->drawnBoxes as $dBox) {
30 4
            if ($this->intersects($pos, $box, $dBox['pos'], $dBox['box'])) {
31 4
                return true;
32
            }
33
        }
34 4
        return false;
35
    }
36
37 4
    protected function intersects(
38
        PointInterface $pos1, BoxInterface $box1,
39
        PointInterface $pos2, BoxInterface $box2
40
    ) {
41 4
        if ($pos1->getY() + $box1->getHeight() < $pos2->getY()) {
42 2
            return false;
43
        }
44 4
        if ($pos1->getY() > $pos2->getY() + $box2->getHeight()) {
45 3
            return false;
46
        }
47 4
        if ($pos1->getX() > $pos2->getX() + $box2->getWidth()) {
48 2
            return false;
49
        }
50 4
        if ($pos1->getX() + $box1->getWidth() < $pos2->getX()) {
51 1
            return false;
52
        }
53
54 4
        return true;
55
    }
56
}
57