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