|
7 | 7 | class Polygon extends Collection |
8 | 8 | { |
9 | 9 | protected $geom_type = 'Polygon'; |
10 | | - |
| 10 | + |
11 | 11 | public function area($exterior_only = FALSE, $signed = FALSE) { |
12 | 12 | if ($this->isEmpty()) return 0; |
13 | 13 |
|
@@ -135,6 +135,73 @@ public function isSimple() { |
135 | 135 | return TRUE; |
136 | 136 | } |
137 | 137 |
|
| 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 | + |
138 | 205 | // Not valid for this geometry type |
139 | 206 | // -------------------------------- |
140 | 207 | public function length() { return NULL; } |
|
0 commit comments