summaryrefslogtreecommitdiff
path: root/src/sketch/shape.zig
diff options
context:
space:
mode:
authorGabriel Schneider <[email protected]>2026-09-25 21:47:58 -0300
committerGabriel Schneider <[email protected]>2026-09-25 21:48:16 -0300
commit9c33714591a2147f7e5879e77212ca9886d0c967 (patch)
treed250be9d9bc365d7894d49455c0f6376966b58be /src/sketch/shape.zig
downloadsketchbook-9c33714591a2147f7e5879e77212ca9886d0c967.tar.gz
sketchbook-9c33714591a2147f7e5879e77212ca9886d0c967.zip
Put the game first: lib/sketchbook, sketches/vecanim
The root is the game. The platform library moves to lib/sketchbook (with serve_web.py, which serves any sketch's web build, as a tool of it), and vecanim to sketches/vecanim, still its own package on the same library.
Diffstat (limited to 'src/sketch/shape.zig')
-rw-r--r--src/sketch/shape.zig209
1 files changed, 209 insertions, 0 deletions
diff --git a/src/sketch/shape.zig b/src/sketch/shape.zig
new file mode 100644
index 0000000..9bd4cee
--- /dev/null
+++ b/src/sketch/shape.zig
@@ -0,0 +1,209 @@
+//! Duck-typed geometry. A point is anything with `x` and `y`, a rect anything
+//! that also has `w` and `h` (or `width`/`height`), in any int or float type;
+//! `@Vector`s and arrays of two or four elements work too. Functions return
+//! the caller's own type where they build one, so sketches keep their data in
+//! whatever shape suits them and never convert to library types.
+const std = @import("std");
+const abi = @import("abi");
+
+/// Anything with an `x` and a `y`: fields, or a 2+ element vector or array.
+pub fn isPoint(comptime T: type) bool {
+ return switch (@typeInfo(T)) {
+ .vector => |v| v.len >= 2 and isNumber(v.child),
+ .array => |a| a.len >= 2 and isNumber(a.child),
+ .@"struct" => @hasField(T, "x") and @hasField(T, "y"),
+ .pointer => |p| p.size == .one and isPoint(p.child),
+ else => false,
+ };
+}
+
+/// A point that also has a size: `w`/`h` or `width`/`height`, or 4 elements.
+pub fn isRect(comptime T: type) bool {
+ return isPoint(T) and switch (@typeInfo(T)) {
+ .vector => |v| v.len >= 4,
+ .array => |a| a.len >= 4,
+ .@"struct" => (@hasField(T, "w") and @hasField(T, "h")) or (@hasField(T, "width") and @hasField(T, "height")),
+ .pointer => |p| isRect(p.child),
+ else => false,
+ };
+}
+
+/// `r`, `g`, `b` (optionally `a`) fields, or a `0xRRGGBB` integer.
+pub fn isColor(comptime T: type) bool {
+ return switch (@typeInfo(T)) {
+ .int, .comptime_int => true,
+ .@"struct" => @hasField(T, "r") and @hasField(T, "g") and @hasField(T, "b"),
+ .pointer => |p| p.size == .one and isColor(p.child),
+ else => false,
+ };
+}
+
+fn isNumber(comptime T: type) bool {
+ return switch (@typeInfo(T)) {
+ .int, .float, .comptime_int, .comptime_float => true,
+ else => false,
+ };
+}
+
+pub const Point = struct { x: f32 = 0, y: f32 = 0 };
+
+/// The type to build results in for inputs shaped like `T`: `T` itself, or
+/// for anonymous literals (whose fields are comptime-only) an f32 `Point` or
+/// `abi.Rect`.
+pub fn Out(comptime T: type) type {
+ if (@typeInfo(T) != .@"struct") return T;
+ for (@typeInfo(T).@"struct".fields) |field| switch (@typeInfo(field.type)) {
+ .comptime_int, .comptime_float => return if (isRect(T)) abi.Rect else Point,
+ else => {},
+ };
+ return T;
+}
+
+/// Component `name` of a point- or rect-like value, as f32.
+pub fn get(value: anytype, comptime name: []const u8) f32 {
+ const T = @TypeOf(value);
+ return switch (@typeInfo(T)) {
+ .vector, .array => num(value[comptime index(name)]),
+ .@"struct" => if (@hasField(T, name))
+ num(@field(value, name))
+ else if (comptime std.mem.eql(u8, name, "w") and @hasField(T, "width"))
+ num(value.width)
+ else if (comptime std.mem.eql(u8, name, "h") and @hasField(T, "height"))
+ num(value.height)
+ else
+ @compileError(@typeName(T) ++ " has no field '" ++ name ++ "'"),
+ .pointer => get(value.*, name),
+ else => @compileError("expected a point or rect, found " ++ @typeName(T)),
+ };
+}
+
+/// A value of type `T` with the given components, converted to `T`'s fields.
+pub fn make(comptime T: type, x: f32, y: f32, w: f32, h: f32) T {
+ switch (@typeInfo(T)) {
+ .vector => |v| return if (v.len == 2) .{ cast(v.child, x), cast(v.child, y) } else .{ cast(v.child, x), cast(v.child, y), cast(v.child, w), cast(v.child, h) },
+ .array => |a| return if (a.len == 2) .{ cast(a.child, x), cast(a.child, y) } else .{ cast(a.child, x), cast(a.child, y), cast(a.child, w), cast(a.child, h) },
+ .@"struct" => {
+ var result: T = undefined;
+ inline for (@typeInfo(T).@"struct".fields) |field| {
+ @field(result, field.name) = if (comptime eql(field.name, "x"))
+ cast(field.type, x)
+ else if (comptime eql(field.name, "y"))
+ cast(field.type, y)
+ else if (comptime eql(field.name, "w") or eql(field.name, "width"))
+ cast(field.type, w)
+ else if (comptime eql(field.name, "h") or eql(field.name, "height"))
+ cast(field.type, h)
+ else if (field.defaultValue()) |default|
+ default
+ else
+ @compileError(@typeName(T) ++ "." ++ field.name ++ " has no default");
+ }
+ return result;
+ },
+ else => @compileError("cannot build a point or rect of type " ++ @typeName(T)),
+ }
+}
+
+pub fn point(comptime T: type, x: f32, y: f32) T {
+ return make(T, x, y, 0, 0);
+}
+
+pub fn rect(value: anytype) abi.Rect {
+ comptime if (!isRect(@TypeOf(value))) @compileError("expected a rect (x, y, w, h), found " ++ @typeName(@TypeOf(value)));
+ return .{ .x = get(value, "x"), .y = get(value, "y"), .w = get(value, "w"), .h = get(value, "h") };
+}
+
+pub fn contains(r: anytype, p: anytype) bool {
+ const x = get(p, "x");
+ const y = get(p, "y");
+ const rx = get(r, "x");
+ const ry = get(r, "y");
+ return x >= rx and y >= ry and x < rx + get(r, "w") and y < ry + get(r, "h");
+}
+
+/// `r` shrunk by `d` on every side, in `r`'s own type.
+pub fn inset(r: anytype, d: f32) Out(@TypeOf(r)) {
+ return make(Out(@TypeOf(r)), get(r, "x") + d, get(r, "y") + d, get(r, "w") - 2 * d, get(r, "h") - 2 * d);
+}
+
+/// `r` moved by the point `by`, in `r`'s own type.
+pub fn offset(r: anytype, by: anytype) Out(@TypeOf(r)) {
+ const has_size = comptime isRect(@TypeOf(r));
+ return make(Out(@TypeOf(r)), get(r, "x") + get(by, "x"), get(r, "y") + get(by, "y"), if (has_size) get(r, "w") else 0, if (has_size) get(r, "h") else 0);
+}
+
+/// Anything with `r`, `g`, `b` (and optionally `a`) in 0..255 or 0..1 floats,
+/// or a `0xRRGGBB` integer.
+pub fn color(value: anytype) abi.Color {
+ const T = @TypeOf(value);
+ comptime if (!isColor(T)) @compileError("expected a color (r, g, b[, a]) or 0xRRGGBB, found " ++ @typeName(T));
+ if (T == abi.Color) return value;
+ return switch (@typeInfo(T)) {
+ .int, .comptime_int => abi.Color.hex(@intCast(value)),
+ .@"struct" => .{
+ .r = channel(value.r),
+ .g = channel(value.g),
+ .b = channel(value.b),
+ .a = if (@hasField(T, "a")) channel(value.a) else 255,
+ },
+ .pointer => color(value.*),
+ else => @compileError("expected a color, found " ++ @typeName(T)),
+ };
+}
+
+fn channel(v: anytype) u8 {
+ return switch (@typeInfo(@TypeOf(v))) {
+ .float, .comptime_float => @intFromFloat(std.math.clamp(v, 0, 1) * 255 + 0.5),
+ else => @intCast(std.math.clamp(v, 0, 255)),
+ };
+}
+
+fn index(comptime name: []const u8) usize {
+ if (eql(name, "x")) return 0;
+ if (eql(name, "y")) return 1;
+ if (eql(name, "w")) return 2;
+ if (eql(name, "h")) return 3;
+ @compileError("no component " ++ name);
+}
+
+fn num(v: anytype) f32 {
+ return switch (@typeInfo(@TypeOf(v))) {
+ .int, .comptime_int => @floatFromInt(v),
+ .float, .comptime_float => @floatCast(v),
+ else => @compileError("not a number: " ++ @typeName(@TypeOf(v))),
+ };
+}
+
+fn cast(comptime T: type, v: f32) T {
+ return switch (@typeInfo(T)) {
+ .int => @intFromFloat(@round(v)),
+ .float => @floatCast(v),
+ else => @compileError("not a number type: " ++ @typeName(T)),
+ };
+}
+
+fn eql(a: []const u8, b: []const u8) bool {
+ return std.mem.eql(u8, a, b);
+}
+
+test "shapes are recognized by their data" {
+ try std.testing.expect(isPoint(struct { x: u8, y: u8 }));
+ try std.testing.expect(!isRect(struct { x: u8, y: u8 }));
+ try std.testing.expect(!isRect(struct { w: u8, h: u8 }));
+ try std.testing.expect(isRect(struct { x: f32, y: f32, width: f32, height: f32 }));
+ try std.testing.expect(isRect(@Vector(4, i16)));
+ try std.testing.expect(!isPoint([2][]const u8));
+ try std.testing.expect(isColor(struct { r: f32, g: f32, b: f32 }));
+}
+
+test "any shape with the right fields works and keeps its type" {
+ const P = struct { x: i32, y: i32, label: u8 = 7 };
+ const R = struct { x: f64, y: f64, width: f64, height: f64 };
+ try std.testing.expect(contains(R{ .x = 0, .y = 0, .width = 10, .height = 5 }, P{ .x = 3, .y = 4 }));
+ try std.testing.expect(!contains(@Vector(4, f32){ 0, 0, 10, 5 }, [2]f32{ 3, 6 }));
+ const moved = offset(P{ .x = 1, .y = 2 }, @Vector(2, f32){ 2, 3 });
+ try std.testing.expectEqual(P{ .x = 3, .y = 5 }, moved);
+ try std.testing.expectEqual(abi.Rect{ .x = 1, .y = 1, .w = 8, .h = 3 }, rect(inset(R{ .x = 0, .y = 0, .width = 10, .height = 5 }, 1)));
+ try std.testing.expectEqual(abi.Color{ .r = 255, .g = 0, .b = 128 }, color(0xff0080));
+ try std.testing.expectEqual(abi.Color{ .r = 255, .g = 128, .b = 0, .a = 255 }, color(.{ .r = 1.0, .g = 0.5, .b = 0.0 }));
+}