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