diff options
| author | Gabriel Schneider <[email protected]> | 2026-11-16 21:28:20 -0300 |
|---|---|---|
| committer | Gabriel Schneider <[email protected]> | 2026-11-16 21:28:20 -0300 |
| commit | 6933f99ddb2d7103515d26838496309a807bba26 (patch) | |
| tree | 991c2fe7d4a57957222876e190fa4d16a0fb889c /examples/noc/src/the_wave.zig | |
| parent | 82fca5c9f29124f37f6dcd08e73e6999ce44a69d (diff) | |
| download | sketchbook-6933f99ddb2d7103515d26838496309a807bba26.tar.gz sketchbook-6933f99ddb2d7103515d26838496309a807bba26.zip | |
Move standalone sketches into sketchbook examples
Diffstat (limited to 'examples/noc/src/the_wave.zig')
| -rw-r--r-- | examples/noc/src/the_wave.zig | 31 |
1 files changed, 31 insertions, 0 deletions
diff --git a/examples/noc/src/the_wave.zig b/examples/noc/src/the_wave.zig new file mode 100644 index 0000000..86ce10d --- /dev/null +++ b/examples/noc/src/the_wave.zig @@ -0,0 +1,31 @@ +//! Nature of Code, Example 3.9: The Wave. +const std = @import("std"); +const sk = @import("sketch"); + +pub const name = "the_wave"; +pub const width = 640; +pub const height = 360; + +pub const State = struct { + start_angle: f32 = 0, + angle_velocity: f32 = 0.2, + clock: sk.Clock = .{}, +}; + +pub fn update(s: *State, in: *const sk.Input) void { + for (0..s.clock.steps(in)) |_| s.start_angle += 0.02; +} + +const pen: sk.draw.Pen = .{ .fill = sk.Color.gray(127).alpha(127), .stroke = .black, .width = 2 }; + +pub fn draw(s: *const State, f: *sk.Frame) void { + f.clear(sk.Color.white); + // Processing advances startAngle after reading it; State already has. + var angle = s.start_angle - 0.02; + var x: f32 = 0; + while (x <= width) : (x += 24) { + const y = std.math.lerp(0, height, (@sin(angle) + 1) / 2); + sk.draw.circle(f, .{ x, y }, 24, pen); + angle += s.angle_velocity; + } +} |
