PMA\libraries\gis\GISPolygon::isPointInsidePolygon PHP Method

isPointInsidePolygon() public static method

Determines whether a given point is inside a given polygon.
public static isPointInsidePolygon ( array $point, array $polygon ) : boolean
$point array x, y coordinates of the point
$polygon array array of points forming the ring
return boolean whether a given point is inside a given polygon
    public static function isPointInsidePolygon($point, $polygon)
    {
        // If first point is repeated at the end remove it
        $last = count($polygon) - 1;
        if ($polygon[0]['x'] == $polygon[$last]['x'] && $polygon[0]['y'] == $polygon[$last]['y']) {
            $polygon = array_slice($polygon, 0, $last);
        }
        $no_of_points = count($polygon);
        $counter = 0;
        // Use ray casting algorithm
        $p1 = $polygon[0];
        for ($i = 1; $i <= $no_of_points; $i++) {
            $p2 = $polygon[$i % $no_of_points];
            if ($point['y'] <= min(array($p1['y'], $p2['y']))) {
                $p1 = $p2;
                continue;
            }
            if ($point['y'] > max(array($p1['y'], $p2['y']))) {
                $p1 = $p2;
                continue;
            }
            if ($point['x'] > max(array($p1['x'], $p2['x']))) {
                $p1 = $p2;
                continue;
            }
            if ($p1['y'] != $p2['y']) {
                $xinters = ($point['y'] - $p1['y']) * ($p2['x'] - $p1['x']) / ($p2['y'] - $p1['y']) + $p1['x'];
                if ($p1['x'] == $p2['x'] || $point['x'] <= $xinters) {
                    $counter++;
                }
            }
            $p1 = $p2;
        }
        if ($counter % 2 == 0) {
            return false;
        } else {
            return true;
        }
    }

Usage Example

示例#1
0
 /**
  * test for getPointOnSurface
  *
  * @param array $ring array of points forming the ring
  *
  * @dataProvider providerForTestGetPointOnSurface
  * @return void
  */
 public function testGetPointOnSurface($ring)
 {
     $this->assertEquals(
         $this->object->isPointInsidePolygon(
             $this->object->getPointOnSurface($ring),
             $ring
         ),
         true
     );
 }
All Usage Examples Of PMA\libraries\gis\GISPolygon::isPointInsidePolygon