Test Failed
Branch master (529f7c)
by Tom
07:56 queued 05:50
created

Polygon   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 83
Duplicated Lines 0 %

Test Coverage

Coverage 11.36%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 41
dl 0
loc 83
ccs 5
cts 44
cp 0.1136
rs 10
c 1
b 0
f 0
wmc 21

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 2
A findInterceptLongitude() 0 10 2
B isPointInPolygon() 0 33 10
A doLinesCrossLatitude() 0 8 5
A __toString() 0 8 2
1
<?php
2
3
namespace NoaaCapAlerts\Model\Polygon;
4
5
class Polygon
6
{
7
    private $points;
8
9
    /**
10
     * Polygon constructor.
11
     * @param array $points
12
     */
13 3
    function __construct(array $points = null)
14
    {
15 3
        if ($points === null) {
16 3
            $points = [];
17
        }
18
19 3
        $this->points = $points;
20 3
    }
21
22
    public function isPointInPolygon(Point $targetPoint): bool
23
    {
24
        $targetLatitude = $targetPoint->getLatitude();
25
        $longitudeHits = [];
26
        $lastPoint = $this->points[array_key_last($this->points)];
27
        foreach ($this->points as $point) {
28
            if ($this->doLinesCrossLatitude($lastPoint, $point, $targetLatitude)) {
29
                $longitudeHits[] = $this->findInterceptLongitude($lastPoint, $point, $targetLatitude);
30
            }
31
            $lastPoint = $point;
32
        }
33
34
        $longitudeHits = array_unique($longitudeHits);
35
        sort($longitudeHits);
36
37
        $hitsBefore = 0;
38
        $hitsAfter = 0;
39
40
        foreach ($longitudeHits as $hit) {
41
            if ($hit <= $targetPoint->getLongitude()) {
42
                $hitsBefore++;
43
            } elseif ($hit >= $targetPoint->getLongitude()) {
44
                $hitsAfter++;
45
            }
46
        }
47
48
        if (($hitsBefore % 2 == 1) && ($hitsAfter % 2 == 1)) {
49
            return true;
50
        } elseif (($hitsBefore % 2 == 0) && ($hitsAfter % 2 == 0)) {
51
            return false;
52
        }
53
54
        throw new \Exception("Something weird happened.  Fixme");
55
    }
56
57
    private function doLinesCrossLatitude(Point $point1, Point $point2, float $latitude): bool
58
    {
59
        if ($point1->getLatitude() >= $latitude && $point2->getLatitude() <= $latitude) {
60
            return true;
61
        } elseif ($point2->getLatitude() >= $latitude && $point1->getLatitude() <= $latitude) {
62
            return true;
63
        } else {
64
            return false;
65
        }
66
    }
67
68
    private function findInterceptLongitude(Point $point1, Point $point2, float $latitude): float
69
    {
70
        if ($point2->getLongitude() - $point1->getLongitude() == 0) {
71
            return $point2->getLongitude();
72
        }
73
74
        $slope = ($point2->getLatitude() - $point1->getLatitude()) / ($point2->getLongitude() - $point1->getLongitude());
75
        $b = $point1->getLatitude() - ($slope * $point1->getLongitude());
76
77
        return ($latitude + $b) / $slope;
78
    }
79
80
    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...
81
    {
82
        $pointStrings = [];
83
        foreach ($this->points as $point) {
84
            $pointStrings[] = '[' . $point->getX() . ', ' . $point->getY() . ']';
85
        }
86
87
        return "Points: " . implode(', ', $pointStrings);
88
    }
89
}