1

Here is my code which I modified and took from this question Find Point in polygon PHP

And the vertices x and y form a rectangle..I am not still able to make it work even though the point y and x are inside polygon it's not displaying the correct result..What I am doing wrong here..

<?php
$vertices_x = array(45.007243,45.007243,46.3734,46.3734); // x-coordinates of the vertices of the polygon
$vertices_y = array(-75.007095,-72.506332,-72.506332,-75.007095,); // y-coordinates of the vertices of the polygon
$points_polygon = count($vertices_x); 
$latitude_y =  45.5086699;  // x-coordinate of the point to test
$longitude_x = -73.553992; 

if (is_in_polygon($points_polygon, $vertices_x, $vertices_y, $longitude_x, $latitude_y)){
  echo "Is in polygon!";
}
else 
{
echo "Is not in polygon";
}

function is_in_polygon($points_polygon, $vertices_x, $vertices_y, $longitude_x, $latitude_y)
{
  $i = $j = $c = 0;
  for ($i = 0, $j = $points_polygon-1 ; $i < $points_polygon; $j = $i++) {
    if ( (($vertices_y[$i] > $latitude_y != ($vertices_y[$j] > $latitude_y)) &&
    ($longitude_x < ($vertices_x[$j] - $vertices_x[$i]) * ($latitude_y - $vertices_y[$i]) / ($vertices_y[$j] - $vertices_y[$i]) + $vertices_x[$i]) ) ) 
        $c = !$c;
  }
  return $c;
}
?>
Community
  • 1
  • 1
Shan
  • 2,822
  • 9
  • 40
  • 61

1 Answers1

0

One issue is the initialization of the latitude_y and longitude_x variables:

latitude_y appears to contain the 'x' coordinate.
longitude_x appears to contain the 'y' coordinate.
R Dub
  • 678
  • 6
  • 23