fix bug in oct8

This commit is contained in:
jonathan santis 2024-10-24 10:15:32 +02:00
parent 93ce68f527
commit e1f5730a01

View File

@ -130,27 +130,26 @@ fn fillBuffer() !void {
}
try pixel(buffer, w, 5, 10, '\u{1F702}');
const vec = Vec3{
.a = Point{ .x = 0, .y = 1, .z = 2 },
.b = Point{ .x = 0, .y = 1, .z = 20 },
.c = Point{ .x = 0, .y = 1, .z = 2 },
.a = Point{ .x = 20, .y = 20, .z = 0 },
.b = Point{ .x = 50, .y = 30, .z = 0 },
.c = Point{ .x = 30, .y = 1, .z = 0 },
};
_ = vec;
//triangle(vec);
_ = 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 = 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 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 triangle(buffer, w, vec);
//_ = 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 = 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 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 {
const i: usize = @intCast(4 * (x + y * w.ws_col));
if (i < buffer.len) {
if (i < buffer.len) { //-1 ??
const slice = buffer[i .. i + 4];
_ = try std.unicode.utf8Encode(symbol, slice);
} else {
@ -160,12 +159,8 @@ fn pixel(buffer: []u8, w: Winsize, x: i64, y: i64, symbol: u21) !void {
fn bresenham(buf: []u8, w: Winsize, p1: Point, p2: Point) !void {
var x: i32 = p1.x;
var y: i32 = p1.y;
//var x: i32 = 0;
//var y: i32 = 0;
const dx = (p2.x - p1.x);
print("p2: {} - {},{}\n", .{ p2.y, p1.y, 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) {
@ -232,16 +227,19 @@ 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("x:{},y:{},er:{},dx:{},dy:{}\n", .{ x, y, er, dx, dy });
_ = try pixel(buf, w, x, y, '8');
er += @abs(dx);
if (2 * er >= @abs(dx)) {
x -= 1;
er -= @abs(dx);
er -= @abs(dy);
}
}
}
}
//fn triangle(vec: Vec3) void {
//}
fn triangle(buffer: []u8, w: Winsize, vec: Vec3) !void {
_ = try bresenham(buffer, w, vec.a, vec.b);
_ = try bresenham(buffer, w, vec.b, vec.c);
_ = try bresenham(buffer, w, vec.c, vec.a);
}