# Point in polygon

Point in polygon

In computational geometry, the point-in-polygon (PIP) problem asks whether a given point in the plane lies inside, outside, or on the boundary of a polygon. It is a special case of point location problems and finds applications in areas that deal with processing geometrical data, such as computer graphics, geographical information systems (GIS), motion planning, and CAD.

An early description of the problem in computer graphics shows two common approaches (ray casting and angle summation) in use as early as 1974. [Ivan Sutherland et al,"A Characterization of Ten Hidden-Surface Algorithms" 1974, "ACM Computing Surveys" vol. 6 no. 1.]

An attempt of computer graphics veterans to trace the history of the problem and some tricks for its solution can be found in an issue of the "Ray Tracing News". [ [http://jedi.ks.uiuc.edu/~johns/raytracer/rtn/rtnv3n4.html "Point in Polygon, One More Time..."] , "Ray Tracing News", vol. 3 no. 4, October 1, 1990. ]

Ray casting algorithm

One simple way of finding whether the point is inside or outside a simple polygon is to test how many times a ray starting from the point intersects the edges of the polygon. If the point in question is not on the boundary of the polygon, the number of intersections is an even number if the point is outside, and it is odd if inside. This algorithm is sometimes also known as the crossing number algorithm or the even-odd rule algorithm. 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 basing on the Jordan curve theorem.

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. This is not normally a concern, as speed is much more important than complete accuracy in most applications of computer graphics. However, for a formally correct computer program, one would have to introduce a numerical tolerance ε and test in line whether "P" lies within ε of "L", 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, say, horizontal 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 of a polygon, then it will intersect 2 segments at their endpoints. While it is OK for the case of, say, the topmost vertex in the example, the case of, say, 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 second vertex of the side lies below 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.

Winding number algorithm

Another algorithm is to compute the given point's winding number with respect to the polygon. If the winding number is non-zero, the point lies inside the polygon. One way to compute the winding number is to sum up the angles subtended by each side of the polygon. However, this involves costly inverse trigonometric functions, which generally makes this algorithm slower than the ray casting algorithm. Luckily, these inverse trigonometric functions do not need to be computed. Since the result, the sum of all angles, can add up to 0 or 2*PI (or multiples of 2*PI) only, it is sufficient to track through which quadrants the polygon winds, as it turns around the test point, which makes the winding number algorithm comparable in speed to counting the boundary crossings.

For simple polygons, both algorithms will always give the same results for all points. However, for complex polygons, the algorithms may give different results for points in the regions where the polygon intersects itself, where the polygon does not have a clearly defined inside and outside. In this case, the former algorithm is called the even-odd rule.

Point in polygon queries

The point in polygon problem may be considered in the general repeated geometric query setting: given a single polygon and a sequence of query points, quickly find the answers for each query point. Clearly, any of the general approaches for planar point location may be used. Simpler solutions are available for some special polygons.

pecial cases

Simpler algorithms are possible for monotone polygons, star-shaped polygons and convex polygons.

References

* [http://www.acm.org/pubs/tog/editors/erich/ptinpoly/ Point in polygon strategies] , by Eric Haines
* [http://www.ecse.rpi.edu/Homepages/wrf/research/geom/pnpoly.html C code to determine if a point is in a polygon]
* [http://www.visibone.com/inpoly/ C code for an algorithm using integers and no divides]
* [http://www.geometryalgorithms.com/Archive/algorithm_0103/algorithm_0103.htm Crossing number and winding number algorithms with illustrations and example code]

Wikimedia Foundation. 2010.

### Look at other dictionaries:

• Polygon — For other uses, see Polygon (disambiguation). Some polygons of different kinds In geometry a polygon (   …   Wikipedia

• Point location — The point location problem is a fundamental topic of computational geometry. It finds applications in areas that deal with processing geometrical data: computer graphics, geographic information systems (GIS), motion planning, and computer aided… …   Wikipedia

• Polygon — Pol y*gon, n. [Gr. poly gwnos polygonal; poly s many + gwni a angle: cf. F. polygone.] (Geom.) A plane figure having many angles, and consequently many sides; esp., one whose perimeter consists of more than four sides; any figure having many… …   The Collaborative International Dictionary of English

• Polygon of forces — Polygon Pol y*gon, n. [Gr. poly gwnos polygonal; poly s many + gwni a angle: cf. F. polygone.] (Geom.) A plane figure having many angles, and consequently many sides; esp., one whose perimeter consists of more than four sides; any figure having… …   The Collaborative International Dictionary of English

• Polygon mesh — A polygon mesh or unstructured grid is a collection of vertices, edges and faces that defines the shape of a polyhedral object in 3D computer graphics and solid modeling. The faces usually consist of triangles, quadrilaterals or other simple… …   Wikipedia

• Polygon (computer graphics) — Polygons are used in computer graphics to compose images that are three dimensional in appearance. Usually (but not always) triangular, polygons arise when an object s surface is modeled, vertices are selected, and the object is rendered in a… …   Wikipedia

• Polygon triangulation — In computational geometry, polygon triangulation is the decomposition of a polygon into a set of triangles.A triangulation of a polygon P is its partition into non overlapping triangles whose union is P. In the strictest sense, these triangles… …   Wikipedia

• Point group — The Bauhinia blakeana flower on the Hong Kong flag has C5 symmetry; the star on each petal has D5 symmetry. In geometry, a point group is a group of geometric symmetries (isometries) that keep at least one point fixed. Point groups can exist in a …   Wikipedia

• Polygon Window — Aphex Twin Aphex Twin Aphex Twin au Traffic festival à Turin en 2005 Alias AFX, Blue Calx, Bradley Strider, Martin Tressider, Caustic Window, Gak, Soit P.P., Po …   Wikipédia en Français

• polygon — n. a plane figure with many (usu. a minimum of three) sides and angles. Phrases and idioms: polygon of forces a polygon that represents by the length and direction of its sides all the forces acting on a body or point. Derivatives: polygonal adj …   Useful english dictionary