Quantize each vertex only once.

This commit is contained in:
treeform 2022-06-30 08:50:02 -07:00
parent 67f48bc576
commit dffad18641

View file

@ -1054,8 +1054,21 @@ proc commandsToShapes(
proc shapesToSegments(shapes: seq[Polygon]): seq[(Segment, int16)] =
## Converts the shapes into a set of filtered segments with winding value.
for shape in shapes:
for segment in shape.segments:
# Quantize the segment to prevent leaks
template quantize(v: Vec2, q: float32): Vec2 =
vec2(v.x.quantize(q), v.y.quantize(q))
for poly in shapes:
var
vec1 = poly[^1].quantize(1 / 256)
vec2 = poly[0].quantize(1 / 256)
segment: Segment
for i in 0 ..< poly.len:
if i != 0:
vec2 = vec1
vec1 = poly[i].quantize(1 / 256)
segment = segment(vec1, vec2)
if segment.at.y == segment.to.y: # Skip horizontal
continue
var
@ -1065,12 +1078,6 @@ proc shapesToSegments(shapes: seq[Polygon]): seq[(Segment, int16)] =
swap(segment.at, segment.to)
winding = -1
# Quantize the segment to prevent leaks
segment = segment(
vec2(segment.at.x.quantize(1 / 256), segment.at.y.quantize(1 / 256)),
vec2(segment.to.x.quantize(1 / 256), segment.to.y.quantize(1 / 256))
)
result.add((segment, winding))
proc transform(shapes: var seq[Polygon], transform: Mat3) =