From d712a7d8d4e48a8791097be2aa4959950a5de84a Mon Sep 17 00:00:00 2001 From: jonathan santis Date: Thu, 24 Oct 2024 08:57:37 +0200 Subject: [PATCH] oct 5 and 6 --- main.zig | 39 +++++++++++++++++++++++++++++---------- 1 file changed, 29 insertions(+), 10 deletions(-) diff --git a/main.zig b/main.zig index 705a949..69775b7 100644 --- a/main.zig +++ b/main.zig @@ -139,19 +139,18 @@ fn fillBuffer() !void { _ = try bresenham(buffer, w, Point{ .x = 10, .y = 10, .z = 0 }, Point{ .x = 30, .y = 12, .z = 0 }); _ = try bresenham(buffer, w, Point{ .x = 10, .y = 10, .z = 0 }, Point{ .x = 30, .y = 28, .z = 0 }); _ = try bresenham(buffer, w, Point{ .x = 10, .y = 10, .z = 0 }, Point{ .x = 30, .y = 30, .z = 0 }); - _ = try bresenham(buffer, w, Point{ .x = 10, .y = 10, .z = 0 }, Point{ .x = 30, .y = 40, .z = 0 }); + _ = try bresenham(buffer, w, Point{ .x = 10, .y = 10, .z = 0 }, Point{ .x = 30, .y = 39, .z = 0 }); _ = try bresenham(buffer, w, Point{ .x = 30, .y = 30, .z = 0 }, Point{ .x = 60, .y = 10, .z = 0 }); _ = try bresenham(buffer, w, Point{ .x = 60, .y = 30, .z = 0 }, Point{ .x = 70, .y = 5, .z = 0 }); + _ = try bresenham(buffer, w, Point{ .x = 100, .y = 20, .z = 0 }, Point{ .x = 30, .y = 30, .z = 0 }); + _ = try bresenham(buffer, w, Point{ .x = 100, .y = 10, .z = 0 }, Point{ .x = 90, .y = 40, .z = 0 }); try file.writeAll(buffer); } 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}); _ = try std.unicode.utf8Encode(symbol, slice); - print("slice after:{u}\n", .{slice}); - print("cols{}\ni: {}\n", .{ w.ws_col, i }); } else { print("Error illegal memory access\n", .{}); } @@ -166,7 +165,7 @@ fn bresenham(buf: []u8, w: Winsize, p1: Point, p2: Point) !void { const dy: i64 = (p2.y - p1.y); //const slope = dx/dy; var er: i64 = 0; - if (dx >= dy and dy > 0) { + if (dx >= dy and dy > 0 and dx > 0) { 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, '.'); @@ -176,7 +175,7 @@ fn bresenham(buf: []u8, w: Winsize, p1: Point, p2: Point) !void { er -= dx; } } - } else if (dx < dy and dy > 0) { + } else if (dx < dy and dy > 0 and dx > 0) { while (y <= p2.y) : (y += 1) { print("[bresenham2]x:{},y:{},er:{},dx:{},dy:{}\n", .{ x, y, er, dx, dy }); _ = try pixel(buf, w, x, y, ','); @@ -186,26 +185,46 @@ fn bresenham(buf: []u8, w: Winsize, p1: Point, p2: Point) !void { er -= dy; } } - } else if (@abs(dy) < dx and dy < 0) { + } else if (@abs(dy) < dx and dy < 0 and dx > 0) { while (x <= p2.x) : (x += 1) { print("[bresenham3]x:{},y:{},er:{},dx:{},dy:{}\n", .{ x, y, er, dx, dy }); - _ = try pixel(buf, w, x, y, 'x'); + _ = try pixel(buf, w, x, y, '🡇'); er -= dy; if (2 * er >= dx) { y -= 1; er -= dx; } } - } else if (@abs(dy) > dx and dy < 0) { + } else if (@abs(dy) > dx and dy < 0 and dx > 0) { while (y >= p2.y) : (y -= 1) { print("[bresenham4]x:{},y:{},er:{},dx:{},dy:{}\n", .{ x, y, er, dx, dy }); - _ = try pixel(buf, w, x, y, '4'); + _ = try pixel(buf, w, x, y, '='); er += dx; if (2 * er >= dy) { x += 1; er += dy; } } + } else if (@abs(dx) <= dy and dx < 0 and dy > 0) { + while (y <= p2.y) : (y += 1) { + print("[bresenham5]x:{},y:{},er:{},dx:{},dy:{}\n", .{ x, y, er, dx, dy }); + _ = try pixel(buf, w, x, y, '5'); + er += @abs(dx); + if (2 * er >= dy) { + x -= 1; + er -= dy; + } + } + } else if (@abs(dx) > dy and dy > 0 and dx < 0) { + while (x >= p2.x) : (x -= 1) { + print("[bresenham6]x:{},y:{},er:{},dx:{},dy:{}\n", .{ x, y, er, dx, dy }); + _ = try pixel(buf, w, x, y, '6'); + er += dy; + if (2 * er >= @abs(dx)) { + y += 1; + er -= @abs(dx); + } + } } }