Short answer: Ray casting decides whether a point lies inside a simple polygon by shooting a horizontal ray to infinity and counting edge crossings. An odd count means inside. An even count means outside. Vertices that lie on the ray are counted under a rule that treats an edge as crossing only when one endpoint is strictly above the ray and the other is on or below it. The test is O(n) in the number of edges. It assumes a simple polygon, or a well-defined winding for a more complex one.
What the problem actually asks
A polygon is a closed chain. A ray that starts at the query point and never ends crosses the boundary once for every transition from inside to outside or back. Parity is therefore the inside test, provided the ray does not slide along an edge and the polygon does not touch itself.
The vertex problem is the bug. If both endpoints of an edge are counted when they lie on the ray, a vertex that is a local high point is counted twice and the parity flips. The standard fix counts an edge when it straddles the ray’s y in a half-open way. A point exactly on the boundary is a separate early-out if the caller cares about the boundary.
What the algorithm guarantees
For a simple closed polygon and a query not on the boundary, the parity rule returns the correct inside or outside answer. Time is Θ(n) per query. Extra memory is none beyond the polygon. A point on an edge should be detected by an orientation test and reported as on the boundary, because the parity of a ray that starts on an edge is not a useful answer.
Winding number is the alternative that counts signed crossings and handles self-overlapping polygons in a documented way. Parity and winding agree on a simple polygon. They disagree on a bow-tie if the caller expected the nonzero winding rule. The algorithm does not accelerate repeated queries. A trapezoidal map or a BSP does that preprocessing.
The working steps
- If the point lies on any edge, return on-boundary. The orientation test (b−a) × (p−a) is zero and the point is between the endpoints.
- Set inside to false. For each edge from a to b, test whether the edge straddles the horizontal line through the point: one endpoint has y strictly greater than the point’s y, the other has y less than or equal to it.
- If it straddles, compute the x coordinate of the intersection with the horizontal line. If that x is strictly greater than the point’s x, toggle inside.
- After every edge, inside is the parity answer.
- Use a consistent half-open y test on every edge so a vertex on the ray belongs to exactly one of the two edges that meet there.
The linear scan is for one query on a simple polygon. It is not a winding-number test unless the toggle is replaced by a signed increment. Repeated queries deserve a spatial index. A horizontal edge is skipped by the straddle test, which is correct, because a ray that travels along a horizontal edge is the case the half-open rule is designed to ignore.
Approaches that look simpler and fail
| Approach | Why it fails when both endpoints on the ray are counted |
|---|---|
| Count an edge whenever either endpoint has the same y as the point | A vertex on the ray is shared by two edges and is counted twice. The parity flips. The half-open straddle counts that vertex once. |
| Use the test on a self-intersecting polygon and call the result the nonzero winding | Parity and winding answer different questions on a bow-tie. Name the rule that was implemented. |
| Skip the on-boundary test | A point on an edge can report inside or outside depending on rounding of the intersection x. Detect the boundary first. |
| Preprocess nothing and quote a logarithmic query | One ray is linear in the number of edges. A logarithmic query needs a point-location structure. |
Signed crossings are the winding-number test. A preprocessed point-location structure replaces the linear scan for many queries. Ray casting is the parity crossing count.
A working example
This Python fragment counts crossings of a ray that runs to +∞ in x. The y-straddle is half-open, so a vertex on the ray is counted once. The point (1, 1) is inside the unit square and (3, 3) is outside. It prints in out.
square = [(0, 0), (2, 0), (2, 2), (0, 2)]
def inside(x, y, poly):
hits = 0
n = len(poly)
for i in range(n):
x1, y1 = poly[i]
x2, y2 = poly[(i + 1) % n]
if (y1 > y) != (y2 > y):
xint = (x2 - x1) * (y - y1) / (y2 - y1) + x1
if x < xint:
hits += 1
return hits % 2 == 1
assert inside(1, 1, square) and not inside(3, 3, square)
print("in", "out")
Where it is used
Ray casting is the even-odd point-in-polygon test in GIS and the even-odd fill rule in SVG and canvas hit testing. A ray that crosses an odd number of edges is inside. Winding-number fill is the other rule and counts direction.
When you can drop this algorithm
Drop parity when the polygon is self-overlapping and the winding rule is the specification. Count signed crossings. Keep ray casting for a simple polygon, with the half-open straddle and an explicit boundary test.
Why this stays published
We publish this so a search for ray casting returns the odd-crossing rule and the half-open treatment of vertices that sit on the ray. Ray casting reports a point inside a simple polygon when a horizontal ray crosses an odd number of edges, with a half-open rule so a vertex on the ray is counted once.
本文由 HoHo 與 AI 協作整理,最後更新於 2026 年 11 月 13 日。
