summaryrefslogtreecommitdiff
path: root/src/estd/graphics/curve.zig
diff options
context:
space:
mode:
authorNathan Reiner <nathan@nathanreiner.xyz>2025-02-12 12:48:11 +0100
committerNathan Reiner <nathan@nathanreiner.xyz>2025-02-12 12:48:11 +0100
commit9fd81c0b38b2b843c24fb61bf8cb5b7873deaa72 (patch)
treea2d4d76a4fcc1334d83c5538e684061913be24d3 /src/estd/graphics/curve.zig
parentdae5bc02b1c934075e95694953b4330676e21611 (diff)
graphics: add line
Diffstat (limited to 'src/estd/graphics/curve.zig')
-rw-r--r--src/estd/graphics/curve.zig36
1 files changed, 36 insertions, 0 deletions
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;
+ }
+ }
+};