spacer1
spacer2 1_1 1_2
2_1
 Subscribe
 The MP2K Update!
 
 
 
 Magazine
Front Cover
What's New
Articles
News
Sample Data
Gallery
Advertise
About
 Features
MapPoint 2009
Press Releases
MapPoint Forums
Companies
Link to MP2Kmag
Wish List
MapPoint Trial
Authors
 Earlier Content
Past News Items
Past What's New Announcements
 Sponsors
 Order

MapPoint 2009

Programming MapPoint in .NET

MapPoint Book

  Spatial Community
ViaVirtualEarth
SVG Tutorials
MapPoint

Map Visitors

 Direct Media

Travel Insurance Compare over 450 travel insurance policies (inc. annual multi-trip, single trip & winter sports)

  ARTICLES  


Determing Whether A Point Is Located Inside Polygon

Wilfried Mestdagh wrote this article on determing whether a vehicle is located inside or outside a convex polygon. "Simplify the polygon by make triangles from it one by one, until we have a triangle where the point is inside. At the end we only have to verify if the point is in the last triangle."

Download code for this article.

This article explains a method to calculate if a position is in or outside a convex polygon. It does not make use of floating point numbers to optimize for speed as much as possible. To transform Latitude / Longitude pairs into X/Y coordiantes, just multiply them with a given factor, for example 100000. 1/100000 of a degree gives precision of around 1 meter which is already better than any GPS receiver.
If you just want to download the complete project, included with demo executable and source code then click here.
To make the calculations easy we will reduce the polygon to triangles by replacing each time two successive sides by a new one, provided that the triangle formed by the two polygon sides and the new side does not contain the point.
Simplify the polygon by make triangles from it one by one, until we have a triangle where the point is inside.
At the end we only have to verify if the point is in the last triangle.
The last triangle.
So we reduce the whole problem to a simple problem if a certain triangle a certain point has in itself. Now, to move the triangle and also the point in such a way that the point is on the origin of the coordinate axis (0,0) we again simplify the calculations.
Moving the polygon to the origin 0,0 of coordinates.
We search the sides intercepting the Y axis. If we don't find them then the zero point is certainly not in the triangle. The 2 intercept points have to each be on another side of the X axis or on the X axis itself. The calculation of a straight line going through the points X1, Y1 and X2, Y2 is:
z (x) = (y2 - y1) / (x2 - x1) x - (y2 - y1) / (x2 - x1) + y1
Now we have to see if the side is intercepting the Y axis. If x1 = x2 then this cannot be, so we can drop the case. To find the intercept point of a straight line with the Y axis we find to compute z(0):
z(0) = (y1 - y2) / (x2 - x1) + y1
This intercept point is on the side and not on the straight line past the side if sng(x1 <> sng(x2).
Finally we have to find out if the side is intercepting the Y axis above or below the X axis. We call the intercept point z(0) (see above). So we have to find the sign of x(0). So z(0) >= 0.
(y1 - y2) / (x2 - x1) * x1 + y1 >= 0
If x2 < x2, then x2 - x1 > 0 and then:
(y1 - y2) * x1 >= y1( - y2)  [E1]
If x1 > x2, then x2 - x1 < 0, and then:
(y1 - y2) * x1 <= y1(x1 - x2) [E2]
So the intercept point is above the X axis if E1 or E2 is true.
Below is the complete code written in Delphi. You can translate it into the programming language of your choice and if you do so please post in the discussion thread for this article.

Discuss this story in the forum.

Author: Wilfried Mestdagh
Email: wilfried(AT)mestdagh.biz
Wilfried Mestdagh works as software engineer at the company Sonal in Mortsel, Belgium. His main work is writing software for fleet management and onboard computers. Fleet management is mainly written in Delphi and C# while the onboard computers are mostly programmed in C. His department started years ago for specializing in tracking and tracing security and dangerous transport vehicles, but it is grown to satisfy a very wide of vehicle / truck fleet customers.



Google
 
MP2Kmag Internet


 Recent Discussion
-BingMapsDev: From today you can now gaze up and explore the stars from Bing Maps Silverlight thx to WWT http://is.gd/aMtvs
-BingMapsDev: @jont87 I was thinking that as I virtually looked up at the stars from the Vegas strip, with all this neon I'm not sure what you'd see.
-bingmaps: BingMapApps pull local blogs, tweets, embedded videos, historic photos, worldwide telescope imagery and more into maps! http://bit.ly/bCPnlb
-bingmaps: @AllTheWiser Nice!You won the BingMaps contest! DM your address and we'll mail your t-shirt. http://bit.ly/adOWAO
-bingmaps: @Buitel Nice!You won the BingMaps contest!DM your address and we'll mail your t-shirt.Few hours left for everyone else http://bit.ly/adOWAO
-bingmaps: @cdlongo Nice!You won the BingMaps contest!DM your address and we'll mail your t-shirt.A few hours left for other folks http://bit.ly/adOWAO
-bingmaps: @ddavidch Nice! You won the BingMaps contest! DM your address and we'll mail your t-shirt. http://bit.ly/adOWAO
-bingmaps: @Gilly2468 Nice!You won the BingMaps contest!DM your address and we'll mail your shirt. Few hours left for other folks http://bit.ly/adOWAO
-bingmaps: @markmcc19 Nice! You won the BingMaps contest! DM your address and we'll mail your t-shirt. http://bit.ly/adOWAO
-bingmaps: @mp2kmag Nice! You won the BingMaps contest! DM your address and we'll mail your t-shirt. http://bit.ly/adOWAO
-bingmaps: @NicolasBoonaert Nice! You won the BingMaps contest! DM your address and we'll mail your t-shirt. A few more hours left for everyone else
-bingmaps: @virus84 Nice! You won the BingMaps contest! DM your address and we'll mail your t-shirt. A few more hours left for other folks
-bingmaps: @WebJives2 Nice! You won the BingMaps contest! DM your address and we'll mail your t-shirt. http://bit.ly/adOWAO
-bingmaps: RT @ChrisPendleton: Watching "Opening up opportunity with Twitter" @rsarver @raffi #Mix10
-bingmaps: RT @ChrisPendleton: Windows Phone 7 Series Developer Training Kit: http://bit.ly/aHiBPW. Now, we just need to add some Bing Maps!
-bingmaps: RT @ChrisPendleton: New Bing Maps Application - WorldWide Telescope: http://bit.ly/bEwrEE.
-Importing Data into Mappoint
-deleting the number associated with pushpins.
-New Bing Maps Application: WorldWide Telescope
-bingmaps: Don't miss out! One more day to tweet to win a Bing t-shirt. Details here: http://bit.ly/adOWAO
-bingmaps: RT @ChrisPendleton: SxSW Musings and Where the World Is: http://bit.ly/9OBAU6. @BingMaps #SxSWi
-Problems with FindResults
-Restrict routes based on country of origin/destination
-How to Retrive location name form latitude and Longitude in J2ME?
-SxSW Musings and Where the World Is
 Resources
Browse GIS books and periodicals
Best Car Contract Hire Lease Save
Find a MapPoint Partner or Consultant
Real Estate Columbia Homes for Sale by Owner


Want Your Site To Appear Here?

   © 1999-2009 MP2K. Questions and comments to: website@mp2kmag.com
  Microsoft and MapPoint 2002/2004/2006/2009 are either trademarks or registered trademarks of Microsoft.

• Replacement Cordless Phone Batteries from DSMiller
Improve your career with a computer degree LocalEDU.com
• Tim Cohn's Marketing Strategy Consultancy
• Buy Toner Cartridges at Marquee Office Solutions
• Looking for Dry Erase Boards? See Dry Erase Boards • The Search Starts Here Used Cars
• 5339 - We Know Bikes: Road Bikes / Triathlon Bikes / Mountain Bikes / BMX Bikes
• Buy Your Car Used Cars UK, Car Lease and Contract Hire