Hubbry Logo
Point in polygonPoint in polygonMain
Open search
Point in polygon
Community hub
Point in polygon
logo
8 pages, 0 posts
0 subscribers
Be the first to start a discussion here.
Be the first to start a discussion here.
Point in polygon
Point in polygon
from Wikipedia
An example of a simple 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, computer vision, geographic information systems (GIS), motion planning, and computer-aided design (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.[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.[2]

Ray casting algorithm

[edit]
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, 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.[3] 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

[edit]

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 formally correct computer program, one would have to introduce a numerical 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 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.

Winding number algorithm

[edit]

Another technique used to check if a point is inside a polygon 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.

One way to compute the winding number is to sum up the angles subtended by each side of the polygon.[4] However, this involves costly inverse trigonometric functions, which generally makes this algorithm performance-inefficient (slower) compared to 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 (or multiples of ) only, it is sufficient to track through which quadrants the polygon winds,[5] as it turns around the test point, which makes the winding number algorithm comparable in speed to counting the boundary crossings.

Visualization of Dan Sunday's winding number algorithm. A winding number of 0 means the point is outside the polygon; other values indicate the point is inside the polygon

An improved algorithm to calculate the winding number was developed by Dan Sunday in 2001.[6] It does not use angles in calculations, nor any trigonometry, and functions exactly the same as the ray casting algorithms described above. Sunday's algorithm works by considering an infinite horizontal ray cast from the point being checked. Whenever that ray crosses an edge of the polygon, Juan Pineda's edge crossing algorithm (1988)[7] is used to determine how the crossing will affect the winding number. As Sunday describes it, if the edge crosses the ray going "upwards", the winding number is incremented; if it crosses the ray "downwards", the number is decremented. Sunday's algorithm gives the correct answer for nonsimple polygons, whereas the boundary crossing algorithm fails in this case.[6]

Implementations

[edit]

SVG

[edit]

Similar methods are used in SVG for defining a way of filling with color various shapes (such as path, polyline, polygon, text etc.).[8] The algorithm of filling is influenced by 'fill-rule' attribute. The value may be either nonzero or evenodd. For example, in a pentagram, there is a central "hole" (visible background) with evenodd, and none with nonzero attribute.[9]

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.[10] This, however, is computationally expensive. 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 queries

[edit]

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.

Special cases

[edit]

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.[11] The dot product method extends naturally to any convex polygon.

References

[edit]

See also

[edit]
Revisions and contributorsEdit on WikipediaRead on Wikipedia
from Grokipedia
In , the point-in-polygon (PIP) problem is the task of determining whether a given point in the plane lies inside, outside, or on the boundary of a specified . This problem is fundamental to the field, with solutions dating back to early algorithms for testing point location relative to polygonal boundaries. Key methods include the ray-casting algorithm, also known as the even-odd rule, which counts the number of intersections between a ray from the point and the 's edges to determine parity; an odd count indicates the point is inside. Another approach is the algorithm, which computes the total angle subtended by the around the point, with a nonzero value signifying enclosure. These techniques apply to arbitrary simple and are based on the topological principle that a closed divides the plane into distinct interior and exterior regions. For convex polygons, more efficient strategies exist, such as preprocessing the vertices to enable binary search for O(log n) query time, where n is the number of vertices. The PIP problem has broad applications in for rendering and clipping. It also has applications in geographic information systems (GIS) for spatial queries and spatial databases for join operations. Variations address challenges like handling self-intersecting polygons, multiple components, or large-scale data sets requiring acceleration via grids or parallel processing.

Fundamentals

Problem Definition

The point-in-polygon (PIP) problem is a fundamental in that involves determining whether a given point in the plane lies in the interior, exterior, or on the boundary of a . This problem arises frequently in applications requiring spatial queries, such as identifying regions enclosed by boundaries defined by vertex coordinates. The is typically represented as a closed of line segments connecting an ordered sequence of vertices, and the task is to classify the point's location relative to this boundary. Polygons in the PIP problem are often assumed to be simple, meaning their edges do not intersect except at vertices, forming a single closed loop without self-intersections. The vertices are commonly listed in counterclockwise order for the outer boundary to ensure consistent orientation, though some algorithms accommodate ordering or require preprocessing to standardize it. The problem can extend to polygons with holes, where interior holes are represented as additional simple with opposite orientation (e.g., ), and a point is considered inside the overall region if it lies within the outer but outside all holes. Self-intersecting , while more complex, can also be handled by certain methods that account for multiple boundary crossings. The classification of the point's location includes three cases: interior (strictly inside the , not on any edge), exterior (strictly outside), and boundary (lying exactly on an edge or at a vertex). Points on the boundary require special handling in algorithms to avoid ambiguity, often using criteria like whether the point coincides with a vertex or lies midway on an edge segment. For with holes, boundary classification applies similarly to both outer and inner boundaries. To illustrate, consider a simple triangular polygon with vertices at (0,0), (4,0), and (2,3). A test point at (2,1) lies in the interior, as it is enclosed by the three edges, while a point at (5,1) is exterior. Algorithms like address this classification by analyzing intersections between a ray from the test point and the polygon edges.

Mathematical Prerequisites

The Jordan curve theorem provides the foundational topological basis for point-in-polygon problems, asserting that every simple closed curve in the plane divides the into exactly two regions: an interior (bounded) region and an exterior (unbounded) region, with the curve itself forming the boundary between them. This theorem guarantees that for a simple closed polygonal chain, points can be unambiguously classified as interior or exterior, excluding the boundary, and underpins the binary nature of tests in . A simple polygon is defined as a closed polygonal chain in the plane that does not intersect itself, forming a Jordan curve whose vertices connect via straight line segments. Convex polygons represent a special case where the line segment joining any two points in the polygon lies entirely within it, equivalent to all interior angles being less than or equal to 180 degrees and the polygon containing its convex hull. Polygon orientation refers to the direction of traversal along the boundary, typically counterclockwise for the exterior boundary (positive orientation) or clockwise for interior boundaries, which can be determined by the sign of the signed area computed via the shoelace formula. Complex polygons, such as those with holes, consist of an outer simple boundary enclosing one or more disjoint inner simple boundaries (holes), where the region between the outer and inner boundaries defines the polygon's interior, requiring consistent orientation conventions (counterclockwise for outer, clockwise for holes) to maintain topological integrity. In coordinate geometry, determining point-line segment relations is essential, particularly tests for whether a point lies on a . To check of a point CC with segment endpoints AA and BB, compute the cross product of vectors AB\overrightarrow{AB}
Add your contribution
Related Hubs
User Avatar
No comments yet.