Test Failed
Push — master ( 820428...529f7c )
by Tom
07:52
created

Polygon::findInterceptLongitude()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 3
dl 0
loc 10
ccs 0
cts 6
cp 0
crap 6
rs 10
c 0
b 0
f 0
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
}