diff options
| author | Nathan Reiner <nathan@nathanreiner.xyz> | 2025-02-12 20:25:35 +0100 |
|---|---|---|
| committer | Nathan Reiner <nathan@nathanreiner.xyz> | 2025-02-13 10:02:02 +0100 |
| commit | 7ca9831f85db448641141ac40770af6baa8b512f (patch) | |
| tree | b4c54a8acb866134e1bfc1a4df7b27cad0d9bc7a /src/estd/graphics/curve.zig | |
| parent | f0aa5d3fafdbfde016f6ebadc96d9fa8cd693d45 (diff) | |
graphics: fix line offset
Diffstat (limited to 'src/estd/graphics/curve.zig')
| -rw-r--r-- | src/estd/graphics/curve.zig | 28 |
1 files changed, 25 insertions, 3 deletions
diff --git a/src/estd/graphics/curve.zig b/src/estd/graphics/curve.zig index d4be1d7..70569a3 100644 --- a/src/estd/graphics/curve.zig +++ b/src/estd/graphics/curve.zig @@ -14,6 +14,14 @@ pub const Curve = struct { color: graphics.Color, + fn distance(a: u32, b: u32) u32 { + if (a > b) { + return a - b; + } else { + return b - a; + } + } + 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); @@ -21,16 +29,30 @@ pub const Curve = struct { const middle_y: f64 = @floatFromInt(self.middle.y); const end_x: f64 = @floatFromInt(self.end.x); const end_y: f64 = @floatFromInt(self.end.y); + const resolution = 2 + ( + distance(self.start.x, self.end.x) + distance(self.start.y, self.end.y) + ) / 8; + + var last = self.start; - for (0..100) |index| { - const t: f64 = @as(f64, @floatFromInt(index)) / 100; + canvas.buffer[last.x + last.y * canvas.width] = .{ .red = 0xff, .green = 0xff, .blue = 0xff }; + + for (1..resolution) |index| { + const t: f64 = @as(f64, @floatFromInt(index)) / @as(f64, @floatFromInt(resolution)); 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; + + (graphics.Line { + .start = .{ .x = last.x, .y = last.y }, + .end = .{ .x = x, .y = y }, + .color = self.color, + }).render(canvas); + + last = .{ .x = x, .y = y }; } } }; |