From 93ce68f527d1d90d60ee5b49fa78ef17f672a9e4 Mon Sep 17 00:00:00 2001 From: jonathan santis Date: Thu, 24 Oct 2024 09:28:12 +0200 Subject: [PATCH] bresenham done --- main.zig | 28 +++++++++++++++++++++------- 1 file changed, 21 insertions(+), 7 deletions(-) diff --git a/main.zig b/main.zig index 69775b7..9f0698d 100644 --- a/main.zig +++ b/main.zig @@ -144,6 +144,8 @@ fn fillBuffer() !void { _ = 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 bresenham(buffer, w, Point{ .x = 100, .y = 30, .z = 0 }, Point{ .x = 90, .y = 10, .z = 0 }); + _ = try bresenham(buffer, w, Point{ .x = 100, .y = 30, .z = 0 }, Point{ .x = 60, .y = 10, .z = 0 }); try file.writeAll(buffer); } fn pixel(buffer: []u8, w: Winsize, x: i64, y: i64, symbol: u21) !void { @@ -162,12 +164,11 @@ fn bresenham(buf: []u8, w: Winsize, p1: Point, p2: Point) !void { //var y: i32 = 0; const dx = (p2.x - p1.x); print("p2: {} - {},{}\n", .{ p2.y, p1.y, p2.y - p1.y }); - const dy: i64 = (p2.y - p1.y); + const dy = (p2.y - p1.y); //const slope = dx/dy; var er: i64 = 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, '.'); er += dy; if (2 * er >= dx) { @@ -177,7 +178,6 @@ fn bresenham(buf: []u8, w: Winsize, p1: Point, p2: Point) !void { } } 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, ','); er += dx; if (2 * er >= dy) { @@ -187,7 +187,6 @@ fn bresenham(buf: []u8, w: Winsize, p1: Point, p2: Point) !void { } } 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, '🡇'); er -= dy; if (2 * er >= dx) { @@ -197,7 +196,6 @@ fn bresenham(buf: []u8, w: Winsize, p1: Point, p2: Point) !void { } } 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, '='); er += dx; if (2 * er >= dy) { @@ -207,7 +205,6 @@ fn bresenham(buf: []u8, w: Winsize, p1: Point, p2: Point) !void { } } 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) { @@ -217,7 +214,6 @@ fn bresenham(buf: []u8, w: Winsize, p1: Point, p2: Point) !void { } } 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)) { @@ -225,6 +221,24 @@ fn bresenham(buf: []u8, w: Winsize, p1: Point, p2: Point) !void { er -= @abs(dx); } } + } else if (dx <= dy and dx < 0 and dy < 0) { + while (x >= p2.x) : (x -= 1) { + _ = try pixel(buf, w, x, y, '7'); + er += @abs(dy); + if (2 * er >= @abs(dx)) { + y -= 1; + er -= @abs(dx); + } + } + } else if (dx > dy and dy < 0 and dx < 0) { + while (y >= p2.y) : (y -= 1) { + _ = try pixel(buf, w, x, y, '8'); + er += @abs(dx); + if (2 * er >= @abs(dx)) { + x -= 1; + er -= @abs(dx); + } + } } }