Skip to content

Instantly share code, notes, and snippets.

@tsprates
Last active August 29, 2015 14:08
Show Gist options
  • Save tsprates/7574c3822bb32c6dd2a2 to your computer and use it in GitHub Desktop.
Save tsprates/7574c3822bb32c6dd2a2 to your computer and use it in GitHub Desktop.
Checks if the google.maps.Polygon.Path is inside the polygon, to determine if exists "holes" inside the google.maps.Polygon.
/**
* Checks if the google.maps.Polygon.Path is inside the polygon.
*
* @see {@link https://developers.google.com/maps/documentation/javascript/overlays#Polygons}
* @see {@link http://gmaps-samples-v3.googlecode.com/svn/trunk/poly/pentagon.html}
* @see {@link http://debian.fmi.uni-sofia.bg/~sergei/cgsr/docs/clockwise.htm}
* @see {@link http://en.wikipedia.org/wiki/Curve_orientation}
* @param {google.maps.Polygon.Path} path
* @returns {boolean}
*/
google.maps.prototype.isCounterClokwise = function(path) {
var j, k, z,
p = path,
count = 0;
for (var i = 0, n = p.length; i < n; i++) {
j = (i + 1) % n;
k = (i + 2) % n;
z = (p[j].lat() - p[i].lat()) * (p[k].lng() - p[j].lng());
z -= (p[j].lng() - p[i].lng()) * (p[k].lat() - p[j].lat());
if (z < 0) {
count--;
} else if (z > 0) {
count++;
}
}
return (count > 0);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment