How do I detect if an object is inside or outside a polygon

Started by
2 comments, last by Codespike 22 years, 6 months ago
Imagine an n-sided polygon, does anyone know a general formula I can apply to detect if an object is within the polygon''s boundaries or outside? What I intend to use it for is to detect whether my mouse cursor is within a polygon or not.
Live by the code, die by the code
Advertisement
These things are called "inside tests". I posted on two methods a few moths ago; try searching on that term. If you don''t find anything, I''ll dig up the info and post again.

Hint: the two major methods are winding numbers and intersection count.
A good web site for point-in-polygon tests:

http://www.acm.org/pubs/tog/editors/erich/ptinpoly/

Graham Rhodes
Senior Scientist
Applied Research Associates, Inc.
Graham Rhodes Moderator, Math & Physics forum @ gamedev.net
i know two methods:

1. calculate a plane for each egde of the polygon and test the point against them.

2. imagine a point where you know it IS outside of the polygon. then draw a line from the point you want to check to the point which lies outside the polygon. if this line crossed one edge the point is inside the polygon. if it crossed two edges the point is outside the polygon. if it crossed three edges the point is inside the polygon and so on. this also works for non convex polygons.
i read this at gamasutra.

This topic is closed to new replies.

Advertisement