diff options
author | bptato <nincsnevem662@gmail.com> | 2024-09-01 15:58:45 +0200 |
---|---|---|
committer | bptato <nincsnevem662@gmail.com> | 2024-09-01 15:58:45 +0200 |
commit | e0a9cec50023bc21c443b51433b486cbe8e52e53 (patch) | |
tree | 4389485858d9c9e8a419da37282065e6e3e3ea90 | |
parent | 55cfd29e961488a8c1ed9eb7801d237d27bc86c7 (diff) | |
download | chawan-e0a9cec50023bc21c443b51433b486cbe8e52e53.tar.gz |
path: fix swap bug, refactor lines
-rw-r--r-- | src/html/dom.nim | 4 | ||||
-rw-r--r-- | src/img/path.nim | 34 | ||||
-rw-r--r-- | src/types/line.nim | 2 |
3 files changed, 21 insertions, 19 deletions
diff --git a/src/html/dom.nim b/src/html/dom.nim index c1abd4e3..e46d0078 100644 --- a/src/html/dom.nim +++ b/src/html/dom.nim @@ -588,9 +588,7 @@ proc fillPath(ctx: CanvasRenderingContext2D; path: Path; color: ARGBColor; proc strokePath(ctx: CanvasRenderingContext2D; path: Path; color: ARGBColor) = if ctx.ps != nil: - var lines: seq[Line] = @[] - for line in path.lines: - lines.add(line) + let lines = path.getLines() ctx.ps.withPacketWriter w: w.swrite(pcStrokePath) w.swrite(lines) diff --git a/src/img/path.nim b/src/img/path.nim index 7572ce20..5c1ce680 100644 --- a/src/img/path.nim +++ b/src/img/path.nim @@ -180,47 +180,51 @@ iterator arcLines(p0, p1, o: Vector2D; r: float64; i: bool): Line {.inline.} = p0 = p1 theta -= step -iterator lines(subpath: Subpath; i: int): Line {.inline.} = +proc addLines(lines: var seq[Line]; subpath: Subpath; i: int) = let p0 = subpath.points[i] let p1 = subpath.points[i + 1] case subpath.segments[i].t of pstStraight: - yield Line(p0: p0, p1: p1) + if line.p0 != line.p1: + lines.add(Line(p0: p0, p1: p1)) of pstQuadratic: let c = subpath.segments[i].cp for line in quadraticLines(p0, p1, c): - yield line + if line.p0 != line.p1: + lines.add(line) of pstBezier: let c0 = subpath.segments[i].cp0 let c1 = subpath.segments[i].cp1 for line in bezierLines(p0, p1, c0, c1): - yield line + if line.p0 != line.p1: + lines.add(line) of pstArc: let o = subpath.segments[i].oa let r = subpath.segments[i].r let i = subpath.segments[i].ia for line in arcLines(p0, p1, o, r, i): - yield line + if line.p0 != line.p1: + lines.add(line) of pstEllipse: discard #TODO -iterator lines*(path: Path): Line {.inline.} = +proc getLines*(path: Path): seq[Line] = + var lines: seq[Line] = @[] for subpath in path.subpaths: assert subpath.points.len == subpath.segments.len + 1 for i in 0 ..< subpath.segments.len: - for line in subpath.lines(i): - if line.p0 == line.p1: - continue - yield line + lines.addLines(subpath, i) + return lines proc getLineSegments*(path: Path): PathLines = if path.subpaths.len == 0: return PathLines() var miny = Inf var maxy = -Inf - var segments: seq[LineSegment] - for line in path.lines: - let ls = LineSegment(line) + let lines = path.getLines() + var segments: seq[LineSegment] = @[] + for line in lines: + let ls = line.toLineSegment() miny = min(miny, ls.miny) maxy = max(maxy, ls.maxy) segments.add(ls) @@ -380,8 +384,8 @@ proc ellipse*(path: Path; x, y, radiusX, radiusY, rotation, startAngle, var e = resolveEllipsePoint(o, endAngle, radiusX, radiusY, rotation) if counterclockwise: let tmp = s - e = s - s = tmp + s = e + e = tmp if path.subpaths.len > 0: path.addStraightSegment(s) else: diff --git a/src/types/line.nim b/src/types/line.nim index 31a639f5..1f1d9687 100644 --- a/src/types/line.nim +++ b/src/types/line.nim @@ -56,7 +56,7 @@ proc cmpLineSegmentX*(l1, l2: LineSegment): int = func p0*(ls: LineSegment): Vector2D {.inline.} = ls.line.p0 func p1*(ls: LineSegment): Vector2D {.inline.} = ls.line.p1 -converter toLineSegment*(line: Line): LineSegment = +proc toLineSegment*(line: Line): LineSegment = LineSegment( line: line, miny: line.miny, |