i have a typical question with the Geometric datatype of mysql, polygon.
I have the polygon data, in the form of an array of latitudes and longitudes, ex:
[["x":37.628134, "y":-77.458334],
["x":37.629867, "y":-77.449021],
["x":37.62324, "y":-77.445416],
["x":37.622424, "y":-77.457819]]
And i have a point (Vertex) with coordinates of latitude and longitude, ex:
$location = new vertex($_GET["longitude"], $_GET["latitude"]);
Now i want to find whether this vertex (point) is inside the polygon. How can i do this in php ?
If your polygons are self-closing, that is to say that it's final vertex is the line between it's last point and it's first point then you need to add a variable and a condition to your loop to deal with the final vertex. You also need to pass the number of vertices as being equal to the number of points.
Here is the accepted answer modified to deal with self-closing polygons:
Thank you! I found this page and it's accepted answer very helpful and I am proud to offer this variation.
The popular answer above contains typos. Elsewhere, this code has been cleaned up. The corrected code is as follows:
Here's a possible algorithm.
Writing the code is left as an exercise. :)
This is a function i converted from another language into PHP:
Additional: For more functions i advise you to use the polygon.php class available here. Create the Class using your vertices and call the function
isInside
with your testpoint as input to have another function solving your problem.