|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869 |
- <?php
-
- namespace Lc\ShopBundle\Services ;
-
- class PointLocationUtils {
-
- var $pointOnVertex = true;
-
- function pointLocation() {
- }
-
- function pointInPolygon($point, $polygon, $pointOnVertex = true) {
- $this->pointOnVertex = $pointOnVertex;
-
-
- $point = $this->pointStringToCoordinates($point);
- $vertices = array();
- foreach ($polygon as $vertex) {
- $vertices[] = $this->pointStringToCoordinates($vertex);
- }
-
-
- if ($this->pointOnVertex == true and $this->pointOnVertex($point, $vertices) == true) {
- return "vertex";
- }
-
-
- $intersections = 0;
- $vertices_count = count($vertices);
-
- for ($i=1; $i < $vertices_count; $i++) {
- $vertex1 = $vertices[$i-1];
- $vertex2 = $vertices[$i];
- if ($vertex1['y'] == $vertex2['y'] and $vertex1['y'] == $point['y'] and $point['x'] > min($vertex1['x'], $vertex2['x']) and $point['x'] < max($vertex1['x'], $vertex2['x'])) {
- return "boundary";
- }
- if ($point['y'] > min($vertex1['y'], $vertex2['y']) and $point['y'] <= max($vertex1['y'], $vertex2['y']) and $point['x'] <= max($vertex1['x'], $vertex2['x']) and $vertex1['y'] != $vertex2['y']) {
- $xinters = ($point['y'] - $vertex1['y']) * ($vertex2['x'] - $vertex1['x']) / ($vertex2['y'] - $vertex1['y']) + $vertex1['x'];
- if ($xinters == $point['x']) {
- return "boundary";
- }
- if ($vertex1['x'] == $vertex2['x'] || $point['x'] <= $xinters) {
- $intersections++;
- }
- }
- }
-
- if ($intersections % 2 != 0) {
- return "inside";
- } else {
- return "outside";
- }
- }
-
- function pointOnVertex($point, $vertices) {
- foreach($vertices as $vertex) {
- if ($point == $vertex) {
- return true;
- }
- }
-
- }
-
- function pointStringToCoordinates($pointString) {
- $coordinates = explode(" ", $pointString);
- return array("x" => $coordinates[0], "y" => $coordinates[1]);
- }
-
- }
|