简体   繁体   中英

Is Point inside a polygon?

Given a list of points that represent the polygon in 2D, how can I determine if the point is inside the polygon.

Please note that the polygon can be either concave or convex. You can also make any assumptions about the order of the points.

The best approach to doing this is to draw a line in any direction from your point, and count the number of times that you cross the boundary of the object. If you hit the boundary an even number of times, you are outside the object, if an odd, you are inside. It is usually easiest to go along one of the axis to make this determination.

Essentially, you just have to find a way to determine if you cross a point. Use the slope of a line equation( m=(y1-y2)/(x1-x2) , y=m*x(x-x1)+y1 , and see if you cross within the boundaries that the point is valid. Given this equation for the line between the points, determine where your line crosses this line, and figure out if it is within the range of the line.

Incidentally, the same method works for any arbitrary dimension, just determining if you hit the face becomes more difficult.

To show a couple of examples, I've drawn a simple illustration, showing both what happens inside and outside, even with a weird shape.

Incidentally, if you hit a corner, it counts for the number of times you make a transition from inside to outside.

在此输入图像描述

Select a point outside of the polygon. Draw a line between your´s and the point outside. If the line intersects the polygon an odd number of times, its inside, otherwise its outside. Zero intersections are also outside. However, this only workes for non overlapping polygons

It's a known problem with many solutions, just google it. And read this : http://en.wikipedia.org/wiki/Point_in_polygon

Take an arbitrary line from your point, to any point definitely outside your polygon (ie outside of the bounds). Check how many times that line intersects with one of the edges of your polygon. If the value is odd, the point is inside.

When checking, be careful of lines that go through a vertex, where your test may indicate two edge crossings.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM