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;
}
}