Point in Polygon Algorithm
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
The Point in Polygon (PIP) algorithm determines whether a given point lies inside, outside, or on the boundary of a polygon. This algorithm is fundamental in computational geometry and has numerous applications, including computer graphics, geographical information systems (GIS), robotics, and collision detection.
Basics of Polygons
Before diving into the Point in Polygon algorithm, it is important to understand some basic concepts about polygons:
• Polygon: A polygon is a closed shape with straight sides. It can be defined by a sequence of vertices. • Convex Polygon: A polygon where all interior angles are less than 180 degrees, and no vertices point inward. • Concave Polygon: A polygon where at least one angle is greater than 180 degrees, indicating vertices that point inward.
PIP Algorithm Overview
The most common Point in Polygon algorithm is the Ray Casting method. It works by drawing an imaginary line (ray) from the point in question to infinity and counting how many times the line intersects with the polygon's edges. The basic principle is:
• If the number of intersections is odd, the point is inside the polygon. • If the number of intersections is even, the point is outside the polygon.
Technical Explanation
Ray Casting Method
- Input: A point and a polygon defined by its vertices .
- Initialization: Initialize a counter for intersections, `intersections = 0`.
- Iterate over each line segment in the polygon formed by consecutive vertices .
- Check for Intersection: Formulate the ray from horizontally to the right, represented as . For each edge, check: • Whether the edge crosses the ray line. • Calculate the point of intersection. • Use the intersection formula to check whether it's a valid intersection.
- Count Valid Intersections: If the ray crosses the polygon edge, increment the `intersections` counter.
- Determine Point Location: • If `intersections` is odd, is inside the polygon. • If `intersections` is even, is outside the polygon.
Special Cases
• Point on a Vertex: If lies directly on a vertex of the polygon, it is considered on the boundary. • Point on an Edge: Similarly, if lies precisely on an edge, it is also on the boundary.
Example
Consider a triangle with vertices at points , , and . To determine if the point is inside this triangle, apply the Ray Casting method:
- Draw a horizontal ray from to the right.
- Check intersections with each edge , , and .
- Count the number of valid intersections. In this case, the ray intersects the triangle's edges at one point, thus confirming that is inside the triangle.
Efficiency Considerations
The Ray Casting method typically runs in time, where is the number of vertices of the polygon. It's efficient for simple use cases but can be comprehensive for polygons with a high number of vertices. Other methods, like the Winding Number algorithm, may offer alternative solutions with varying degrees of complexity.
Applications
• Geographic Information Systems (GIS): Determines if a point (e.g., a GPS coordinate) lies within a geographic boundary. • Computer Graphics: Used in rendering to detect visibility and occlusion within modeled scenes. • Robotics and Path Planning: Helps in collision detection and path optimization within defined environments.
Summary Table
| Criteria | Description |
| Polygon Type | Convex or Concave |
| Input | Point coordinates and polygon vertices |
| Mathematical Method | Ray Casting method (drawing a ray from point to infinity) |
| Complexity | where is the number of polygon vertices |
| Output | Boolean determination (inside/outside/on the edge) |
| Special Cases Handling | Accounts for points on vertices and edges |
| Applications | GIS, Computer Graphics, Robotics, Collision Detection, etc. |
Conclusion
The Point in Polygon algorithm is a vital tool for many technological fields requiring spatial determination. Understanding the algorithm's underlying principles and its efficiency can significantly aid in applications requiring spatial analysis. Despite its simplicity, the method is robust and adaptable, cementing its place as a fundamental algorithm in computational geometry.
Related reading
- Point in polygon on Earth globe
- Policy Iteration vs Value Iteration
- Polygon infill algorithm
- polygon union without holes
- Polygon enclosing a set of points
- Position N circles of different radii inside a larger circle without overlapping
- Polynomial time and exponential time
- Polynomial time solution for Tetris Puzzle

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.