My first GitHub project! – A MKPolygon Category – Polygon Intersection!

I am happy to announce my very first open source project that is now on GitHub! You can check out (no pun intended) the repo here and run it in XCode!

I have been playing around the MKMapKit framework for a while and I have solved lots of different types of geo-spatial problems before (in different programming languages) but thought that the geo-spatial functionality, surrounding MKPolygons (which conform to the MKOverlay protocol) was a bit lacking.

If you check out the MKOverlay Protocol Reference you will see that you do have a a method called ‘intersectsMapRect:(MKMapRect) ‘ but then it only tells you whether or not your shape intersects with a rectangular area or not. That could be useful but what if I wanted to test whether two non-rectangular MKPolygons intersected? What if I wanted to do something REALLY crazy and create a brand spanking new MKPolygon that was the result of the union of two non-rectangular MKPolygons? Well, if you have ever wanted to do such a thing, then you are in luck because now there is a MKPolygon category that does just that!

I accomplished this task by using some good old fashioned slope-intercept math (and had to add special test cases) and writing my own custom algorithm to figure out a lot of the rest once I calculated all of the main intersection points.

Below are screenshots from a test, using view controller code. Basically just a MapView and the view controller to handle all of the GUI stuff. When i am done drawing 2 polygons on the map, I then call the category class method to return to me a new MKPolygon and I use that new polygon as a new overlay (colored orange) on the mapview. (see the view controller code for more details).

Screenshot 2013.06.25 13.46.46

Map View

Screenshot 2013.06.25 13.48.32

Drawing Polygon 1 on the map

Screenshot 2013.06.25 13.48.50

Finished Drawing Polygon 1 – Area Filled In

Screenshot 2013.06.25 13.49.28

Drawing Polygon 2

Screenshot 2013.06.25 13.49.43

Finished Drawing Polygon 2 and the intersection of both polygons is calculated and updated on the map!

Let me know what you think!


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