Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Point in polygon
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
==Ray casting algorithm== {{See also|Jordan curve theorem}} [[Image:RecursiveEvenPolygon.svg|right|thumb|The number of intersections for a ray passing from the exterior of the polygon to any point: If odd, it shows that the point lies inside the polygon; if even, the point lies outside the polygon. This test also works in three dimensions.]] One simple way of finding whether the point is inside or outside a [[simple polygon]] is to test how many times a [[ray (mathematics)|ray]], starting from the point and going in any fixed direction, intersects the edges of the polygon. If the point is on the outside of the polygon the ray will intersect its edge an [[even number]] of times. If the point is on the inside of the polygon then it will intersect the edge an [[odd number]] of times. The status of a point on the edge of the polygon depends on the details of the ray intersection algorithm. This algorithm is sometimes also known as the '''crossing number algorithm''' or the [[even–odd rule]] '''algorithm''', and was known as early as 1962.<ref>Shimrat, M., "Algorithm 112: Position of point relative to polygon" 1962, ''[[Communications of the ACM]]'' Volume 5 Issue 8, Aug. 1962. https://dl.acm.org/doi/10.1145/368637.368653</ref> The algorithm is based on a simple observation that if a point moves along a ray from infinity to the probe point and if it crosses the boundary of a polygon, possibly several times, then it alternately goes from the outside to inside, then from the inside to the outside, etc. As a result, after every two "border crossings" the moving point goes outside. This observation may be mathematically proved using the [[Jordan curve theorem]]. === Limited precision === If implemented on a computer with [[finite precision arithmetics]], the results may be incorrect if the point lies very close to that boundary, because of rounding errors. For some applications, like video games or other entertainment products, this is not a large concern since they often favor speed over precision. However, for a [[Formal verification|formally correct]] [[computer program]], one would have to introduce a [[Numerical analysis|numerical]] [[Tolerance (engineering)|tolerance]] ε and test in line whether ''P'' (the point) lies within ε of ''L'' (the Line), in which case the algorithm should stop and report "''P'' lies very close to the boundary." Most implementations of the ray casting algorithm consecutively check intersections of a ray with all sides of the polygon in turn. In this case the following problem must be addressed. If the ray passes exactly through a [[vertex (geometry)|vertex]] of a polygon, then it will intersect 2 segments at their endpoints. While it is OK for the case of the topmost vertex in the example or the vertex between crossing 4 and 5, the case of the rightmost vertex (in the example) requires that we count one intersection for the algorithm to work correctly. A similar problem arises with horizontal segments that happen to fall on the ray. The issue is solved as follows: If the intersection point is a vertex of a tested polygon side, then the intersection counts only if the other vertex of the side lies below the ray. This is effectively equivalent to considering vertices ''on'' the ray as lying slightly ''above'' the ray. Once again, the case of the ray passing through a vertex may pose numerical problems in [[finite precision arithmetics]]: for two sides adjacent to the same vertex the straightforward computation of the intersection with a ray may not give the vertex in both cases. If the polygon is specified by its vertices, then this problem is eliminated by checking the y-coordinates of the ray and the ends of the tested polygon side before actual computation of the intersection. In other cases, when polygon sides are computed from other types of data, other tricks must be applied for the [[numerical robustness]] of the algorithm.
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)