bresenham fix dx and dy equal 0

This commit is contained in:
jonathan santis 2024-10-25 13:21:51 +02:00
parent 60ae492b0f
commit 45f75ea757

View File

@ -140,7 +140,7 @@ fn fillBuffer() !void {
defer allocator.free(buffer);
var vec = Vec3{
.a = Point{ .x = 20, .y = 20, .z = 0, .direction = .{ .x = 1, .y = 1, .z = 1 } },
.a = Point{ .x = 20, .y = 30, .z = 0, .direction = .{ .x = 1, .y = 1, .z = 1 } },
.b = Point{ .x = 50, .y = 30, .z = 0, .direction = .{ .x = 1, .y = 1, .z = 1 } },
.c = Point{ .x = 30, .y = 1, .z = 0, .direction = .{ .x = 1, .y = 1, .z = 1 } },
};
@ -165,9 +165,8 @@ fn fillBuffer() !void {
try triangle(buffer, w, vec);
//_ = try bresenham(buffer, w, Point{ .x = 10, .y = 10, .z = 0 }, Point{ .x = 30, .y = 12, .z = 0 });
sleep(20000000);
try out.print("\x1B[2J", .{});
//sleep(1000000);
try triangle(buffer, w, vec);
bounce(w, &vec.a, 'x');
@ -185,7 +184,7 @@ fn fillBuffer() !void {
bounce(w, &vec2.c, 'x');
bounce(w, &vec2.c, 'y');
try out.print("{s}", .{buffer});
sleep(10000000);
sleep(30000000);
//_ = try stdin.readUntilDelimiterOrEof(&buf, '\n');
}
}
@ -253,7 +252,7 @@ fn bresenham(buf: []u8, w: Winsize, p1: Point, p2: Point) !void {
er -= dx;
}
}
} else if (dx < dy and dy > 0 and dx > 0) {
} else if (dx <= dy and dy >= 0 and dx >= 0) {
while (y <= p2.y) : (y += 1) {
_ = try pixel(buf, w, x, y, '█');
er += dx;
@ -262,7 +261,7 @@ fn bresenham(buf: []u8, w: Winsize, p1: Point, p2: Point) !void {
er -= dy;
}
}
} else if (@abs(dy) < dx and dy < 0 and dx > 0) {
} else if (@abs(dy) <= dx and dy <= 0 and dx >= 0) {
while (x <= p2.x) : (x += 1) {
_ = try pixel(buf, w, x, y, '█');
er -= dy;
@ -271,7 +270,7 @@ fn bresenham(buf: []u8, w: Winsize, p1: Point, p2: Point) !void {
er -= dx;
}
}
} else if (@abs(dy) > dx and dy < 0 and dx > 0) {
} else if (@abs(dy) >= dx and dy <= 0 and dx >= 0) {
while (y >= p2.y) : (y -= 1) {
_ = try pixel(buf, w, x, y, '█');
er += dx;
@ -280,7 +279,7 @@ fn bresenham(buf: []u8, w: Winsize, p1: Point, p2: Point) !void {
er += dy;
}
}
} else if (@abs(dx) <= dy and dx < 0 and dy > 0) {
} else if (@abs(dx) <= dy and dx <= 0 and dy >= 0) {
while (y <= p2.y) : (y += 1) {
_ = try pixel(buf, w, x, y, '█');
er += @abs(dx);
@ -289,7 +288,7 @@ fn bresenham(buf: []u8, w: Winsize, p1: Point, p2: Point) !void {
er -= dy;
}
}
} else if (@abs(dx) > dy and dy > 0 and dx < 0) {
} else if (@abs(dx) >= dy and dy >= 0 and dx <= 0) {
while (x >= p2.x) : (x -= 1) {
_ = try pixel(buf, w, x, y, '█');
er += dy;
@ -298,7 +297,7 @@ fn bresenham(buf: []u8, w: Winsize, p1: Point, p2: Point) !void {
er -= @abs(dx);
}
}
} else if (dx <= dy and dx < 0 and dy < 0) {
} else if (dx <= dy and dx <= 0 and dy <= 0) {
while (x >= p2.x) : (x -= 1) {
_ = try pixel(buf, w, x, y, '█');
er += @abs(dy);
@ -307,7 +306,7 @@ fn bresenham(buf: []u8, w: Winsize, p1: Point, p2: Point) !void {
er -= @abs(dx);
}
}
} else if (dx > dy and dy < 0 and dx < 0) {
} else if (dx >= dy and dy <= 0 and dx <= 0) {
while (y >= p2.y) : (y -= 1) {
//print("x:{},y:{},er:{},dx:{},dy:{}\n", .{ x, y, er, dx, dy });
_ = try pixel(buf, w, x, y, '█');