Skip to content

Commit 40303d4

Browse files
committed
Merge pull request #67 from aaronbauman/master
feature request: pointInPolygon() and pointOnVertex() in Polygon.class.php
2 parents 1d1c0c4 + a9294c8 commit 40303d4

1 file changed

Lines changed: 68 additions & 1 deletion

File tree

lib/geometry/Polygon.class.php

Lines changed: 68 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
class Polygon extends Collection
88
{
99
protected $geom_type = 'Polygon';
10-
10+
1111
public function area($exterior_only = FALSE, $signed = FALSE) {
1212
if ($this->isEmpty()) return 0;
1313

@@ -135,6 +135,73 @@ public function isSimple() {
135135
return TRUE;
136136
}
137137

138+
/**
139+
* For a given point, determine whether it's bounded by the given polygon.
140+
* Adapted from http://www.assemblysys.com/dataServices/php_pointinpolygon.php
141+
* @see http://en.wikipedia.org/wiki/Point%5Fin%5Fpolygon
142+
*
143+
* @param Point $point
144+
* @param boolean $pointOnBoundary - whether a boundary should be considered "in" or not
145+
* @param boolean $pointOnVertex - whether a vertex should be considered "in" or not
146+
* @return boolean
147+
*/
148+
public function pointInPolygon($point, $pointOnBoundary = true, $pointOnVertex = true) {
149+
$vertices = $this->getPoints();
150+
151+
// Check if the point sits exactly on a vertex
152+
if (pointOnVertex($point, $vertices)) {
153+
return $pointOnVertex ? TRUE : FALSE;
154+
}
155+
156+
// Check if the point is inside the polygon or on the boundary
157+
$intersections = 0;
158+
$vertices_count = count($vertices);
159+
160+
for ($i=1; $i < $vertices_count; $i++) {
161+
$vertex1 = $vertices[$i-1];
162+
$vertex2 = $vertices[$i];
163+
if ($vertex1->y() == $vertex2->y()
164+
&& $vertex1->y() == $point->y()
165+
&& $point->x() > min($vertex1->x(), $vertex2->x())
166+
&& $point->x() < max($vertex1->x(), $vertex2->x())) {
167+
// Check if point is on an horizontal polygon boundary
168+
return $pointOnBoundary ? TRUE : FALSE;
169+
}
170+
if ($point->y() > min($vertex1->y(), $vertex2->y())
171+
&& $point->y() <= max($vertex1->y(), $vertex2->y())
172+
&& $point->x() <= max($vertex1->x(), $vertex2->x())
173+
&& $vertex1->y() != $vertex2->y()) {
174+
$xinters =
175+
($point->y() - $vertex1->y()) * ($vertex2->x() - $vertex1->x())
176+
/ ($vertex2->y() - $vertex1->y())
177+
+ $vertex1->x();
178+
if ($xinters == $point->x()) {
179+
// Check if point is on the polygon boundary (other than horizontal)
180+
return $pointOnBoundary ? TRUE : FALSE;
181+
}
182+
if ($vertex1->x() == $vertex2->x() || $point->x() <= $xinters) {
183+
$intersections++;
184+
}
185+
}
186+
}
187+
// If the number of edges we passed through is even, then it's in the polygon.
188+
if ($intersections % 2 != 0) {
189+
return TRUE;
190+
}
191+
else {
192+
return FALSE;
193+
}
194+
}
195+
196+
public function pointOnVertex($point) {
197+
foreach($this->getPoints() as $vertex) {
198+
if ($point->equals($vertex)) {
199+
return true;
200+
}
201+
}
202+
}
203+
204+
138205
// Not valid for this geometry type
139206
// --------------------------------
140207
public function length() { return NULL; }

0 commit comments

Comments
 (0)