From 4987faed561b9593885f7a86561995a197a6a3e2 Mon Sep 17 00:00:00 2001 From: jonathan santis Date: Tue, 22 Oct 2024 14:57:57 +0200 Subject: [PATCH] sdf --- main.zig | 56 +++++++++++++++++++++++++++----------------------------- 1 file changed, 27 insertions(+), 29 deletions(-) diff --git a/main.zig b/main.zig index 2c80c58..63513ac 100644 --- a/main.zig +++ b/main.zig @@ -17,9 +17,9 @@ const Winsize = struct { ws_ypixel: c_ushort = 0, }; const Point = struct { - x: u32, - y: u32, - z: u32, + x: i32, + y: i32, + z: i32, }; const Vec3 = struct { a: Point, @@ -136,11 +136,11 @@ fn fillBuffer() !void { }; _ = vec; //triangle(vec); - _ = try bresenham(buffer, w, Point{ .x = 10, .y = 40, .z = 0 }, Point{ .x = 100, .y = 20, .z = 0 }); + _ = try bresenham(buffer, w, Point{ .x = 10, .y = 40, .z = 0 }, Point{ .x = 30, .y = 10, .z = 0 }); try file.writeAll(buffer); } -fn pixel(buffer: []u8, w: Winsize, x: u64, y: u64, symbol: u21) !void { - const i = 4 * (x + y * w.ws_col); +fn pixel(buffer: []u8, w: Winsize, x: i64, y: i64, symbol: u21) !void { + const i: usize = @intCast(4 * (x + y * w.ws_col)); if (i < buffer.len) { const slice = buffer[i .. i + 4]; print("slice:{u}\n", .{slice}); @@ -152,37 +152,35 @@ fn pixel(buffer: []u8, w: Winsize, x: u64, y: u64, symbol: u21) !void { } } fn bresenham(buf: []u8, w: Winsize, p1: Point, p2: Point) !void { - var x: u64 = p1.x; - var y: u64 = p1.y; + const x1: i32 = p1.x; + const y1: i32 = p1.y; + var x: i32 = 0; + var y: i32 = 0; const dx = (p2.x - p1.x); - const a: u32 = 20; - const b: u32 = 40; - var c1: i64 = 0; - c1 = a - b; - print("c:{}\n", .{c1}); - print("p2: {} - {},{}", .{ p2.y, p1.y, p2.y - p1.y }); - var dy: i64 = ((p2.y) - (p1.y)); + print("p2: {} - {},{}\n", .{ p2.y, p1.y, p2.y - p1.y }); + const dy: i64 = (p2.y - p1.y); //const slope = dx/dy; var er: i64 = 0; + + if (dy > 0 and dy <= dx) { + x = x1; + y = y1; + } + if (dy < 0 and @abs(dy) > dx) { + print("octant2\n", .{}); + x = y1; + y = x1; + } + while (x <= p2.x) : (x += 1) { print("[bresenham]x:{},y:{},er:{},dx:{},dy:{}\n", .{ x, y, er, dx, dy }); _ = try pixel(buf, w, x, y, '.'); - - if (dy > 0) { - er += dy; - if (2 * er >= dx) { - y += 1; - er -= dx; - } - } else if (dy < 0) { - er -= dy; - if (2 * er >= dx) { - y -= 1; - er += dx; - } + er += dy; + if (2 * er >= dx) { + y += 1; + er -= dx; } } - dy = 0; } //fn triangle(vec: Vec3) void {