From 9fd81c0b38b2b843c24fb61bf8cb5b7873deaa72 Mon Sep 17 00:00:00 2001 From: Nathan Reiner Date: Wed, 12 Feb 2025 12:48:11 +0100 Subject: graphics: add line --- src/estd/graphics/curve.zig | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 src/estd/graphics/curve.zig (limited to 'src/estd/graphics/curve.zig') diff --git a/src/estd/graphics/curve.zig b/src/estd/graphics/curve.zig new file mode 100644 index 0000000..d4be1d7 --- /dev/null +++ b/src/estd/graphics/curve.zig @@ -0,0 +1,36 @@ +const std = @import("std"); +const graphics = @import("root.zig"); + +pub const Curve = struct { + const Self = @This(); + const Point = struct { + x: u32, + y: u32, + }; + + start: Point, + middle: Point, + end: Point, + + color: graphics.Color, + + pub fn render(self: *const Self, canvas: *const graphics.Canvas) void { + const start_x: f64 = @floatFromInt(self.start.x); + const start_y: f64 = @floatFromInt(self.start.y); + const middle_x: f64 = @floatFromInt(self.middle.x); + const middle_y: f64 = @floatFromInt(self.middle.y); + const end_x: f64 = @floatFromInt(self.end.x); + const end_y: f64 = @floatFromInt(self.end.y); + + for (0..100) |index| { + const t: f64 = @as(f64, @floatFromInt(index)) / 100; + const x: u32 = @intFromFloat(@round( + (1 - t) * ((1 - t) * start_x + t * middle_x) + t * ((1 - t) * middle_x + t * end_x) + )); + const y: u32 = @intFromFloat(@round( + (1 - t) * ((1 - t) * start_y + t * middle_y) + t * ((1 - t) * middle_y + t * end_y) + )); + canvas.buffer[x + canvas.width * y] = self.color; + } + } +}; -- cgit v1.2.3-70-g09d2