-3

What is the best algorithm to solve point in polygon in programming contests?

royhowie
  • 11,075
  • 14
  • 50
  • 67
user1284064
  • 175
  • 2
  • 9
  • 3
    There are optimized algorithms for most of the common shapes (namely triangles and quadrilaterals), but a simple approach is outlined by Wikipedia that should work for any arbitrary polygon as long as you choose the ray properly: http://en.wikipedia.org/wiki/Point_in_polygon – Blender Apr 07 '12 at 07:12
  • -1 You want to enter a computational geometry contest - but are asking the community solve it for you!? – cmannett85 Apr 08 '12 at 07:33
  • How did you come to that conclusion cmannett85??!! – user1284064 Aug 31 '12 at 21:13

2 Answers2

1

Shoot a ray(in arbitrary direction) from the point and check the number of times it has crossed the edges of polygon if it is even then the point is outside the polygon otherwise the point is inside the polygon.

If you need to do it for lots of query points, you can triangulate the polygon (actually triangulate both inside and the region between the polygon the convex containing it) so that you could do ray shooting in O(log n)

Ehsan Shoja
  • 443
  • 3
  • 6
-1

if you have a convex polygon you can use this :

http://e-maxx.ru/algo/pt_in_polygon

Babak
  • 176
  • 1
  • 4
  • 18