Subject:

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 , computer vision , 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 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.

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 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.

## Point in polygon algorithm complexity pdf

This method won't work if the point is on the edge of the polygon. This algorithm is sometimes also known as the crossing number algorithm or the even—odd rule algorithm , and was known as early as As a result, after every two "border crossings" the moving point goes outside.

This observation may be mathematically proved using 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.

## Article Metrics

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 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.

## Subscribe to RSS

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.

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. 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. This algorithm is sometimes also known as the nonzero-rule algorithm.

## Geometric Algorithms

One way to compute the winding number is to sum up the angles subtended by each side of the polygon. Luckily, these inverse trigonometric functions do not need to be computed. There is a significant speed-up known since of the winding number algorithm. It uses signed crossings, based on whether each crossing is left-to-right or right-to-left.

## Your Answer

Angles are not used, and no trigonometry is involved. The code is as fast as the simple boundary crossing algorithm.

Further, it gives the correct answer for nonsimple polygons, whereas the boundary crossing algorithm fails in this case. Both methods are used in SVG , where the value of attribute 'fill-rule' is either " nonzero " or " evenodd ". For simple polygons , the algorithms will give the same result.

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.

One solution using the even-odd rule is to transform complex polygons into simpler ones that are even-odd-equivalent before the intersection check. It is less expensive to use the fast non-zero winding number algorithm, which gives the correct result even when the polygon overlaps itself.

## Point in polygon

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.

Simpler algorithms are possible for monotone polygons , star-shaped polygons , convex polygons and triangles. The triangle case can be solved easily by use of a barycentric coordinate system , parametric equation or dot product. From Wikipedia, the free encyclopedia. This section needs expansion.

## Space and Time Complexity of an Algorithm

You can help by adding to it. August Computational Geometry. Categories : Geometric algorithms. Hidden categories: Articles to be expanded from August All articles to be expanded Articles using small message boxes.

## Navigation menu

Namespaces Article Talk. Views Read Edit View history. In other projects Wikimedia Commons. By using this site, you agree to the Terms of Use and Privacy Policy. Wikibooks has a book on the topic of: Category:Point-in-polygon problem.