determine if a point sits inside an arbitrary shape?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Determining whether a point is inside an arbitrary shape is a fundamental problem in computational geometry with numerous practical applications in computer graphics, geographic information systems (GIS), collision detection in physics engines, and game development. The solution depends on the complexity of the shape, which could range from simple convex polygons to complex concave polygons with holes.
Problem Definition
Given a point and an arbitrary polygon defined by its vertices, the goal is to determine if lies inside the polygon, outside it, or on its boundary. For most practical purposes, we focus on the inside-vs-outside distinction.
Geometric Definitions
- Polygon: A plane figure described by a finite number of straight line segments connected to form a closed shape.
- Convex Polygon: A polygon where any line segment connecting two interior points lies entirely inside or on the polygon. All interior angles are less than .
- Concave Polygon: A polygon that is not convex, meaning at least one interior angle exceeds . A line segment between two interior points may pass outside the polygon.
Ray Casting Algorithm
The ray casting method (also called the crossing number algorithm) is one of the most common techniques for determining whether a point is inside a polygon. The principle is straightforward:
- Cast a ray from the point in an arbitrary direction (typically along the positive x-axis).
- Count how many times the ray intersects with the edges of the polygon.
- If the number of intersections is odd, the point is inside. If even, the point is outside.
This works because a ray starting inside a closed polygon must cross an odd number of edges to escape, while a ray starting outside crosses an even number (including zero).
Algorithm Steps
- Initialize an intersection counter to zero.
- For each edge of the polygon defined by consecutive vertices and :
- Check if the ray from (going in the positive x-direction) could intersect this edge. The edge must straddle the y-coordinate of , meaning one endpoint is above and one is below .
- Compute the x-coordinate of the intersection point using linear interpolation: .
- If , the intersection is to the right of , so increment the counter.
- If the counter is odd, is inside the polygon. If even, is outside.
Implementation in Python
The time complexity is where is the number of edges. This is optimal since every edge must be checked at least once.
Winding Number Algorithm
The winding number algorithm is another approach that counts how many times the polygon winds around the point. A non-zero winding number means the point is inside.
- For each edge, determine whether it crosses upward or downward relative to .
- For upward crossings where is to the left of the edge, increment the winding number.
- For downward crossings where is to the right of the edge, decrement the winding number.
- If the final winding number is non-zero, the point is inside.
The winding number approach handles complex polygons (including self-intersecting ones) more robustly than ray casting.
Handling Special Cases
Several edge cases need careful treatment:
- Point on an edge: Both algorithms may give inconsistent results when the point lies exactly on a polygon edge. Most implementations treat boundary points as either inside or outside consistently.
- Ray passing through a vertex: The ray casting algorithm can double-count or miss intersections at vertices. The standard fix is to treat edges as closed on one endpoint and open on the other (for example, include the lower vertex but exclude the upper vertex).
- Degenerate edges: Zero-length edges (duplicate consecutive vertices) should be skipped.
Extending to Complex Shapes
Polygons with Holes
For shapes with holes, test the point against the outer boundary and each hole boundary separately. The point is inside the shape if it is inside the outer boundary and outside all holes.
Curved Boundaries
For shapes with curved edges (circles, ellipses, Bezier curves), the ray casting approach still works but intersection calculations become more involved. For a circle centered at with radius , the point is inside if .
Comparison Table
| Algorithm | Time | Handles Concave | Handles Self-intersecting |
| Ray Casting | Yes | Partial | |
| Winding Number | Yes | Yes | |
| Convex Test | No (convex only) | No |
Summary
The ray casting and winding number algorithms are the two primary methods for determining point-in-polygon containment. Both run in time and handle convex and concave polygons. Ray casting is simpler to implement, while the winding number provides more robust results for complex shapes. For convex polygons specifically, binary search methods can achieve time. Choosing the right algorithm depends on the shape complexity, the need for boundary handling, and whether the polygon may self-intersect.

