Determine if Two Polygons overlap

In order to determine if two polygons overlap we need to iterate through the line segments that make up each polygon and determine if any of the line segments intersect. Once we find a single point where the lines intersect we can then stop the iterations. The following code can be used to determine if two polygons overlap:

//poly1 and poly2 are arrays of VELatlongs that represent polygons
function ArePolygonsOverlapped(poly1, poly2)
if(poly1.length >= 3 && poly2.length >= 3)
//close polygons poly1.push(poly1[0]); poly2.push(poly2[0]); for(var i = 0; i < poly1.length-1;i++)
for(var k = 0; k < poly2.length-1; k++)
return true;

return false;

return null;

The code for the function "SimplePolylineIntersection" can be found here:!E7DBA9A4BFD458C5!474.entry


6 thoughts on “Determine if Two Polygons overlap

  1. Thanks Ricky, I was wondering if this handles polygon A contains polygon B. Or containing is not consider as overlap?

    • The algorithm will determine if a polygon intersects (overlaps) or is complete contained. However it won’t just tell you if is complete contains and not overlapping. The algorithm could be easily changed though to determine that.

  2. This is for Google Maps v2 version I guess(which is depreciated and removed). It would be great if you can update for Google Maps v3. If I end up. I will let you know.

    Praveen Jegan

    • Nope. This is Bing Maps v6.3, not Google Maps v2. I don’t use Google Maps, note the title of my blog “Ricky’s Bing Maps Blog”. If you want to use this with the latest version of Bing Maps (v7) you can easily modify this code to work by:

      * Give all Latitude and Longitude property names small “L”s.
      * Change VELatLong to Microsoft.Maps.Location.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s