The guide
Inside a polygon is a counting question, and a star has two honest answers
Counting crossings and counting windings agree for simple shapes and split on a star. Exact fractions settle every corner and every edge.
- -2 and 2 winding number and crossing count at the pentagram centre
- -5.5511e-17 double-precision cross product for a point that is exactly on an edge
- 720 degrees total turning at the corners of a pentagram
- 2001 year of the angle-free winding number test
Read the full guide Fold the guide
Ask whether a point is inside a polygon and the answer feels obvious: look at the picture. A program cannot look at a picture. It has a list of corners and a pair of numbers, and it must turn that into a yes, a no, or a third answer that people forget to allow for. That third answer is “on the boundary”, and it is one of three the problem has always had: inside, outside, or exactly on an edge. Everything that goes wrong in practice happens at the seams between these three. Those seams are where programs fail.
Two ways to count
The oldest method sends a ray from the point in any fixed direction and counts how many edges it crosses. An odd count means inside. An even count means outside. The idea is that every crossing switches the walker between outside and inside, so after two crossings the walker is back where it began. For a polygon that does not cross itself, this is the Jordan curve theorem at work: a simple closed curve divides the plane into one bounded interior and one unbounded exterior, and the theorem is easy to establish for polygons.
This is called the crossing number test, or the even-odd rule. Two ways of deciding insideness, ray casting and angle summation, were already in use in computer graphics by 1974.
The second method counts turns. Walk once around the polygon and ask how many times the walk circles the point. The answer is the winding number: counterclockwise laps count positive, clockwise laps count negative, and a point the walk never circles scores 0. If the winding number is not 0, the point is inside. That is the nonzero rule.
You can compute a winding number by adding up the angle each edge subtends at the point and dividing by a full turn. It works, but it needs inverse trigonometric functions, which is slow and brings rounding of its own. In 2001 Dan Sunday developed a version that uses no angles at all. It casts a horizontal ray and adds one when an edge crosses it going up and subtracts one when an edge crosses it going down. Its bookkeeping is the same as the ray casting test, plus a sign.
Which side of an edge is the point on? The cross product answers that. For an edge from a to b, the two-dimensional cross product of (b minus a) and (p minus a) is zero when the three points are collinear, positive when p lies to the left of the edge, and negative when it lies to the right.
The corner that counts twice
Here is the classic trap. A ray that happens to pass exactly through a corner meets two edges at their shared end. Count both and you may count one crossing twice, which flips the answer.
The repair is a rule about heights, and it is the same repair in every careful implementation. Treat every corner that sits on the ray as if it were a hair below the ray. The mirror convention, a hair above, works equally well, and it is the form the Wikipedia page describes. Put another way, an edge counts only if one end is at or below the height of the point and the other end is strictly above it. A corner at the exact height then belongs to the edge that rises from it and never to the edge that arrives there. Horizontal edges are never counted at all.
Take the diamond with corners at (0, -5), (5, 0), (0, 5) and (-5, 0). The point (-8, 0) sits to its left, and its ray runs straight through the corner (-5, 0), across the diamond, and out through the corner (5, 0). With the half-open rule the ray counts two edges, the count is even, and the point is outside, which is right. The point (6, 0) is outside too, with no crossings at all. The centre (0, 0) crosses one edge and is inside.
Break the rule on purpose and the centre fails: if both edges at the corner (5, 0) are counted, the ray from (0, 0) records two crossings and the centre is called outside. A single wrong comparison turns a correct page into a wrong one, and it does so only for points whose height matches a corner. Nothing in a random test of generic points will ever find it.
The scalable vector graphics specification, which defines both rules for drawing, handles this by passing the buck. It says that its descriptions do not say what to do when a path segment coincides with or is tangent to the ray, and that one may simply choose a different ray. That is fine for a picture. It is no help to a program that needs a single deterministic answer.
Where a star splits the two rules
For a simple polygon the two methods always agree. The surprise comes with a polygon that crosses itself. Take the pentagram with corners (0, 6), (3, -5), (-5, 2), (5, 2) and (-3, -5), traced in that order. Its centre is surrounded by the five arms, and the outline winds around it twice. The total turning at the corners of a pentagram is 720 degrees, two full turns, where a simple polygon turns once.
At the centre (0, 0) the pair (winding number, crossing count) is -2 and 2. The winding number is not 0, so the nonzero rule says inside. The crossing count is even, so the even-odd rule says outside. Both are correct answers to different questions. At the point (0, 5), in one arm, the pair is -1 and 1, and both rules say inside.
This is not a quirk. Each counted edge changes the winding number by exactly one, so the winding number and the crossing count always have the same parity. They can disagree only where the winding number is even and not zero. The first place that can happen is a region walked around twice, which is exactly what a star’s heart is.
Vector graphics settled the argument by offering both. The nonzero rule is the default for filling shapes in the scalable vector graphics standard and in the canvas drawing interface, and the even-odd rule is an option. On a pentagram the nonzero rule fills the middle, and the even-odd rule leaves a hole there. A square traced twice shows the same split in its purest form: the winding number is 2, the crossing count is 2, and the two rules disagree about a shape nobody would call self-crossing.
Neighbouring regions of a curve differ in winding number by exactly one, and the region outside everything has winding number 0. That is why the pentagram’s arms score 1 in size, its heart scores 2, and the space around it scores 0.
Why the edge needs exact numbers
A point on an edge sits at the one place where the sign of a cross product is zero, and zero is the answer floating-point arithmetic is least likely to give. Decimal 0.1 cannot be stored exactly in binary floating point at any finite precision, so a decimal input is already slightly wrong before any test begins.
A concrete case. Take the segment from (0.1, 0.3) to (0.7, 0.9) and the point (0.4, 0.6). On paper the point lies exactly on the segment, and the cross product is exactly 0. In ordinary double-precision arithmetic the same expression gives about -5.5511e-17. A program that tests for exactly 0 reports the point as off the edge. A program that uses a tolerance has to choose one, and every choice is wrong for some input.
Exact rational arithmetic removes the question. Read each decimal as a fraction of integers, so 0.1 is one tenth, and carry the arithmetic in integers that never overflow or round. Browsers provide integers too large for ordinary numbers through BigInt, and Python’s fractions module does the same job with its Fraction type. Then the cross product comes out as exactly zero, the bounding box test confirms that the point is between the ends, and the verdict is “on the edge” with no tolerance to defend.
Exactness also keeps a corner a corner. When two edges share a vertex, the height comparisons are done on the same numbers for both edges, so the vertex lands in the same half-open class for each of them. With rounded values the two edges could disagree about which side of the ray the shared corner is on, which is the double count again by another route.
To reproduce the example, enter the three vertices 0.1, 0.3 then 0.7, 0.9 then 5, -4 and test the point 0.4, 0.6. The page reports it on the edge, with an exact cross product of 0. The pentagram numbers above are the ones this page loads on first view, and the diamond is in its example list. Larger inputs are a matter of time rather than correctness. Exact tests on several hundred corners and a couple of hundred points are quick, and a background worker thread keeps the page responsive if a job is ever large.
What this method does not cover
It treats one closed ring of straight edges. A polygon with a hole, given as a second ring, needs its own treatment, and so do curved edges. The test says where a point is, not how large the polygon is, how to cut it into triangles, or what its convex hull looks like. Those are different problems with different answers.
It also cannot tell you which rule your own application ought to use. The answer for a star depends on whether the star is a filled shape or a ring, and that is a design decision, not a mathematical one. What the test can do is show both answers side by side, with the edges that were counted, so the choice is made with the facts in hand.
Sources
Every source consulted for this guide. Links checked November 10, 2026.
- Point in polygon Wikipedia
- Winding number Wikipedia
- Even-odd rule Wikipedia
- Jordan curve theorem Wikipedia
- Simple polygon Wikipedia
- Complex polygon Wikipedia
- Cross product Wikipedia
- Floating-point arithmetic Wikipedia
- Painting: Filling, Stroking and Marker Symbols W3C, Scalable Vector Graphics (SVG) 2
- fill-rule MDN Web Docs
- CanvasRenderingContext2D: fill() method MDN Web Docs
- BigInt MDN Web Docs
- Web Workers API MDN Web Docs
- fractions - Rational numbers Python Software Foundation, Python 3.13 documentation