summaryrefslogtreecommitdiff
path: root/src/estd/graphics
diff options
context:
space:
mode:
authorNathan Reiner <nathan@nathanreiner.xyz>2025-02-12 20:25:35 +0100
committerNathan Reiner <nathan@nathanreiner.xyz>2025-02-13 10:02:02 +0100
commit7ca9831f85db448641141ac40770af6baa8b512f (patch)
treeb4c54a8acb866134e1bfc1a4df7b27cad0d9bc7a /src/estd/graphics
parentf0aa5d3fafdbfde016f6ebadc96d9fa8cd693d45 (diff)
graphics: fix line offset
Diffstat (limited to 'src/estd/graphics')
-rw-r--r--src/estd/graphics/curve.zig28
-rw-r--r--src/estd/graphics/line.zig19
2 files changed, 34 insertions, 13 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 };
}
}
};
diff --git a/src/estd/graphics/line.zig b/src/estd/graphics/line.zig
index 93f17ff..faf801a 100644
--- a/src/estd/graphics/line.zig
+++ b/src/estd/graphics/line.zig
@@ -42,32 +42,31 @@ pub const Line = struct {
gradient = 1;
}
-
var intersect_y: f64 = @floatFromInt(start.y);
if (steep) {
for (start.x..end.x + 1) |x| {
- const y: usize = @intFromFloat(intersect_y);
+ const y: u64 = @intFromFloat(intersect_y);
const factor = intersect_y - @as(f64, @floatFromInt(y));
- var previous_color = canvas.buffer[canvas.width * x + y];
- canvas.buffer[x * canvas.width + y] = previous_color.mix(&self.color, factor);
+ var previous_color = canvas.buffer[x * canvas.width + y];
+ canvas.buffer[x * canvas.width + y] = self.color.mix(&previous_color, factor);
- previous_color = canvas.buffer[x * canvas.width + y - 1];
- canvas.buffer[x * canvas.width + y - 1] = self.color.mix(&previous_color, factor);
+ previous_color = canvas.buffer[canvas.width * x + y + 1];
+ canvas.buffer[x * canvas.width + y + 1] = previous_color.mix(&self.color, factor);
intersect_y += gradient;
}
} else {
for (start.x..end.x + 1) |x| {
- const y: u32 = @intFromFloat(intersect_y);
+ const y: u64 = @intFromFloat(intersect_y);
const factor = intersect_y - @as(f64, @floatFromInt(y));
var previous_color = canvas.buffer[x + canvas.width * y];
- canvas.buffer[x + canvas.width * y] = previous_color.mix(&self.color, factor);
+ canvas.buffer[x + canvas.width * y] = self.color.mix(&previous_color, factor);
- previous_color = canvas.buffer[x + canvas.width * (y - 1)];
- canvas.buffer[x + canvas.width * (y - 1)] = self.color.mix(&previous_color, factor);
+ previous_color = canvas.buffer[x + canvas.width * (y + 1)];
+ canvas.buffer[x + canvas.width * (y + 1)] = previous_color.mix(&self.color, factor);
intersect_y += gradient;
}