summaryrefslogtreecommitdiff
path: root/examples/noc/src/koch.zig
diff options
context:
space:
mode:
authorGabriel Schneider <[email protected]>2026-11-16 21:28:20 -0300
committerGabriel Schneider <[email protected]>2026-11-16 21:28:20 -0300
commit6933f99ddb2d7103515d26838496309a807bba26 (patch)
tree991c2fe7d4a57957222876e190fa4d16a0fb889c /examples/noc/src/koch.zig
parent82fca5c9f29124f37f6dcd08e73e6999ce44a69d (diff)
downloadsketchbook-6933f99ddb2d7103515d26838496309a807bba26.tar.gz
sketchbook-6933f99ddb2d7103515d26838496309a807bba26.zip
Move standalone sketches into sketchbook examples
Diffstat (limited to 'examples/noc/src/koch.zig')
-rw-r--r--examples/noc/src/koch.zig79
1 files changed, 79 insertions, 0 deletions
diff --git a/examples/noc/src/koch.zig b/examples/noc/src/koch.zig
new file mode 100644
index 0000000..2dfa8ef
--- /dev/null
+++ b/examples/noc/src/koch.zig
@@ -0,0 +1,79 @@
+//! Nature of Code, Example 8.5: the Koch Curve. Each click applies the
+//! rules once more: four segments for every one (after 8, back to 0).
+const std = @import("std");
+const sk = @import("sketch");
+const vec = sk.math;
+
+pub const name = "koch";
+pub const width = 640;
+pub const height = 360;
+
+const max_generation = 8;
+
+/// The whole curve is a function of how often the rules ran, so that is
+/// all State keeps: one byte, however many segments it means (65536 at
+/// generation 8, 1 MiB as points). Snapshots, replays and seeks stay cheap.
+pub const State = struct {
+ generation: u8 = 5,
+};
+
+/// The segments, and their picture, derived from the generation in
+/// `draw` and kept here between frames. `Derived` rebuilds them when the
+/// generation they were made for is not State's (a click, a seek, a
+/// replay) and after a reload.
+pub const Session = struct {
+ curve: sk.Derived(Curve) = .{ .value = .{} },
+};
+
+const Segment = struct { start: vec.Vec, end: vec.Vec };
+
+const Curve = struct {
+ segments: std.ArrayList(Segment) = .empty,
+ picture: sk.draw.Tessellation = .empty,
+};
+
+pub fn update(s: *State, in: *const sk.Input) void {
+ if (in.mousePressed(.left) != null) s.generation = if (s.generation == max_generation) 0 else s.generation + 1;
+}
+
+/// The book's `generate()`: every segment becomes four, the middle two
+/// raised into a peak.
+fn generate(gpa: std.mem.Allocator, segments: *std.ArrayList(Segment)) !void {
+ var next: std.ArrayList(Segment) = try .initCapacity(gpa, segments.items.len * 4);
+ for (segments.items) |line| {
+ const v = vec.scale(line.end - line.start, 1.0 / 3.0);
+ const b = line.start + v;
+ const d = b + v;
+ // Rotated by -60°, so the peak points up.
+ const c = b + vec.Affine.rotate(-std.math.pi / 3.0).applyVector(v);
+ for ([_]vec.Vec{ line.start, b, c, d }, [_]vec.Vec{ b, c, d, line.end }) |from, to| next.appendAssumeCapacity(.{ .start = from, .end = to });
+ }
+ segments.deinit(gpa);
+ segments.* = next;
+}
+
+fn build(curve: *Curve, gpa: std.mem.Allocator, generation: u8) !void {
+ curve.segments.clearRetainingCapacity();
+ try curve.segments.append(gpa, .{ .start = .{ 0, 300 }, .end = .{ width, 300 } });
+ for (0..generation) |_| try generate(gpa, &curve.segments);
+ // The segments join end to start: one line through their points, 2 px
+ // wide with round joins and ends, looks like the book's separate
+ // round-capped lines and draws each pixel once.
+ const points = try gpa.alloc(vec.Vec, curve.segments.items.len + 1);
+ defer gpa.free(points);
+ for (curve.segments.items, points[0..curve.segments.items.len]) |line, *p| p.* = line.start;
+ points[points.len - 1] = curve.segments.items[curve.segments.items.len - 1].end;
+ curve.picture.clearRetainingCapacity();
+ try curve.picture.strokePath(gpa, points, .{ .width = 2, .join = .round, .cap = .round }, .identity);
+}
+
+pub fn draw(s: *const State, ss: *Session, f: *sk.Frame) void {
+ f.clear(sk.Color.white);
+ if (ss.curve.stale(s.generation, f.in)) build(&ss.curve.value, f.session, s.generation) catch {
+ ss.curve.forget();
+ f.host.log("out of session memory: curve dropped");
+ return;
+ };
+ f.shape(&ss.curve.value.picture, .black);
+ f.print(8, 8, 2, sk.Color.gray(100), "generation {d}: {d} segments", .{ s.generation, ss.curve.value.segments.items.len });
+}