A bug in the stroke algorithm

When we redesigned the stroke algorithms we added connections using line crossing calculations, but we always worked with open paths. But what happens if the path is closed, or if it intersects itself?

Javascript Editor

The situation is worse, if there ar multiple subpaths, we even get a javascript error, because a subpath can be empty.

Javascript Editor

If we want stroke letters, many paths will be composite.

First we need to accept in stroke that a subpath may be empty and we ignore it.

Second we need to adress the ovoerload. There are algorithms that create the union of vector paths, implicating crossing each line with each other, but we do not need them. At this stage, we are already at the raster image, so we just can stroke each segment individually. If we set a pixel black twice, it will be the same black. So this is what we do. We convert the path in two line segments for each line. We scanFill each segment individually. Then we scanFill the connecting polygon.

Javascript Editor

Javascript Editor

Javascript Editor

However, we still have rounding error as you can see with at lower left corner of the square. We never round, but we use trigonometric functions. In the beginning of stroke, I defined pi as 3.1415926536, the digits yi know by heart since school. Ten digits is not exact enough. We replace it with the Javascript constant Math.PI. We also must escape lineIntersection if the lines are parallel.

Javascript Editor

Javascript Editor

The current version of the code has 1845 lines.
ps20240821.js

My Journey to PostScript