Completed
Push — master ( c4f09d...5f5e3f )
by Dan
05:45 queued 03:21
created

Mask::reset()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 3
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 2
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 3
    public function add(PointInterface $pos, BoxInterface $box)
15
    {
16 3
        $this->drawnBoxes[] = array(
17 3
            'pos' => $pos,
18 3
            'box' => $box
19
        );
20 3
    }
21
22
    public function reset()
23
    {
24
        $this->drawnBoxes = array();
25
    }
26
27 3
    public function overlaps(PointInterface $pos, BoxInterface $box)
28
    {
29 3
        foreach ($this->drawnBoxes as $dBox) {
30 3
            if ($this->intersects($pos, $box, $dBox['pos'], $dBox['box'])) {
31 3
                return true;
32
            }
33
        }
34 3
        return false;
35
    }
36
37 3
    protected function intersects(
38
        PointInterface $pos1, BoxInterface $box1,
39
        PointInterface $pos2, BoxInterface $box2
40
    ) {
41 3
        if ($pos1->getY() + $box1->getHeight() < $pos2->getY()) {
42 3
            return false;
43
        }
44 3
        if ($pos1->getY() > $pos2->getY() + $box2->getHeight()) {
45 3
            return false;
46
        }
47 3
        if ($pos1->getX() > $pos2->getX() + $box2->getWidth()) {
48 1
            return false;
49
        }
50 3
        if ($pos1->getX() + $box1->getWidth() < $pos2->getX()) {
51
            return false;
52
        }
53
54 3
        return true;
55
    }
56
}
57