Zero winding vs. even odd rule

For our scanfill algorithm, we have used the even odd rule which is appropriate for most polygons and easy to implement. However, PostScript and also SVG use the zero winding rule.

What is the difference. On a scan line, each crossing switches on and off with the even odd rule. With the zero winding rule, the direction of the crossing matters. If it the line goes up, 1 is added, if it goes down, 1 is substracted. Painting is on whenever the sum is not zero.

Both rules behave the same for a single convex polygon. If a path goes up, it must go down later when the path is closed.

However, with complex and multiple polygons, the paths are more expressive. With the zero-winding you can both paint over or cut out, depending on the direction. For a letter O, you make an outside circle in one direction and an inside circle in the other direction. We probably can also simplify the stroke algorithm which had problem with the even odd rule.

Can we modify the scanfill algorithm to enable the zero-winding rule? The algorithm uses an array of crossings for each line which is sorted numerically. Adding direction would create a second dimension to each crossing, which makes it more complicated to sort. But we can do a trick. Currently, we use real numbers for the crossings. We do not need the fractiona for the pixel. We can round down the values and then add 0.5 if it for crossing that go up. This will not interfere with sort and allow us to sort and both detect the direction.

Javascript Editor

We use here a global variable as switch to test the algorithm

Javascript Editor

Javascript Editor

And paint a pentagrams as well as two pairs of overlapping squares both in same and opposite direction.

Javascript Editor

This needed only minimal intervention. How are we going to implement it?
PostScript proposes eofill as alternate operator so we can switch both ways. SVG has an option for even odd, as well as Canvas. We can use the zerowind rule also to do the stroke algorithm the old way. We do this in the next chapter.

My Journey to PostScript