Archived

This topic is now archived and is closed to further replies.

Point in polygon

This topic is 6169 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

Once I had a pice of code that checked if the given point 2d was inside an polygon(4 verts).... I lost it.... Could somebody give me an fast and easy pointinpoly function....

Share on other sites
take the point in question, and project a ray from it (any direction will do)

check to see if that ray crosses each of the lines in the poly. if you get an even number of crossings (0,2,etc), its not in the poly.

Share on other sites
You can get the equation of the 4 lines bounding the poly by solving the standard y=mx+b equation of a line. Now plug in the value of your point. If you get a value that either below or above an opposite pair of lines, then the point is out side the poly. If it is between the lines (above one, below the other), then check the other pair. A point is in the poly if and only if it is between both pairs of oppsite sides.

See crude Diagram:

O1

-----y1------

I

------y2-----

O2

y1,y2 are lines on opposite side of poly.
I, O1, and O2 are points.

O1 is outside the poly because y1(O1.x) and y2 (O1.x) are both positive
O2 is outside the poly because y1(O2.x) and y2 (O2.x) are both negative

I is between the lines because y1(I.x) is negative, but y2(I.x) is positive

Share on other sites
Invective: TANSTAAFL''s method is the standard inside test by counting intersections. It''ll run circles around solving for four given lines (and it works better for arbitrarily oriented lines, where the point in question may be a vertex - intersecting both lines exactly).

Share on other sites
Just some clarification on tans'' method. There is at least one special case that I can see that must be taken into account. Assuming your polygon is limited to 4 verts than this works great however, there is a case where extending a line in one direction might run through a single vertex giving you the result of being inside the poly when in fact its not. Also dont forget the case where your point is ON a poly''s vertex

1. 1
2. 2
Rutin
22
3. 3
4. 4
frob
18
5. 5

• 33
• 13
• 10
• 11
• 9
• Forum Statistics

• Total Topics
632566
• Total Posts
3007107

×