dif fixes

This commit is contained in:
jonathan santis 2024-10-24 13:52:21 +02:00
parent b8a9788d65
commit 034c2c8b66

View File

@ -18,10 +18,16 @@ const Winsize = struct {
ws_xpixel: c_ushort = 0,
ws_ypixel: c_ushort = 0,
};
const Direction = struct {
x: i8 = 1,
y: i8 = 1,
z: i8 = 1,
};
const Point = struct {
x: i32,
y: i32,
z: i32,
direction: Direction, //for movement state
};
const Vec3 = struct {
a: Point,
@ -119,10 +125,6 @@ fn fillBuffer() !void {
var w = Winsize{};
var i: u64 = 0;
var buf = std.mem.zeroes([2]u8);
var d: i8 = 1;
var d2: i8 = 1;
var d3: i8 = -1;
var d4: i8 = -1;
buf[0] = 0;
w = try getTermDimension();
@ -132,9 +134,9 @@ fn fillBuffer() !void {
var buffer = try allocator.alloc(u8, w.ws_row * w.ws_col * 4); //utf-8 can be 4 bytes long
defer allocator.free(buffer);
var vec = Vec3{
.a = Point{ .x = 20, .y = 20, .z = 0 },
.b = Point{ .x = 50, .y = 30, .z = 0 },
.c = Point{ .x = 30, .y = 1, .z = 0 },
.a = Point{ .x = 20, .y = 20, .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 } },
};
while (true) {
@ -148,46 +150,61 @@ fn fillBuffer() !void {
try pixel(buffer, w, 5, 10, '\u{1F702}');
try triangle(buffer, w, vec);
//_ = try bresenham(buffer, w, Point{ .x = 10, .y = 10, .z = 0 }, Point{ .x = 30, .y = 12, .z = 0 });
// try file.writeAll(buffer);
if (vec.c.x < w.ws_col) {
vec.c.x += d;
}
if (vec.c.x == w.ws_col or vec.c.x == 0) {
d *= -1;
vec.c.x += d;
}
if (vec.a.y < w.ws_row) {
vec.a.y += d2;
}
if (vec.a.y == w.ws_row or vec.a.y == 0) {
d2 *= -1;
vec.a.y += d2;
}
if (vec.b.y < w.ws_row) {
vec.b.y += d3;
}
if (vec.b.y == w.ws_row or vec.b.y == 0) {
d3 *= -1;
vec.b.y += d3;
}
if (vec.b.x < w.ws_col) {
vec.b.x += d4;
}
if (vec.b.x == w.ws_col or vec.b.x == 0) {
d4 *= -1;
vec.b.x += d4;
}
try triangle(buffer, w, vec);
try out.print("\x1B[2J", .{});
bounce(w, &vec.a, 'x');
bounce(w, &vec.a, 'y');
bounce(w, &vec.b, 'x');
bounce(w, &vec.b, 'y');
bounce(w, &vec.c, 'x');
bounce(w, &vec.c, 'y');
print("\x1b[45\x1b[37", .{});
try out.print("{s}", .{buffer});
_ = try stdin.readUntilDelimiterOrEof(&buf, '\n');
}
}
fn bounce(w: Winsize, point: *Point, axis: u8) void {
var cor = &point.x;
var dir = &point.direction.x;
var upper = w.ws_col;
switch (axis) {
'x' => {
cor = &point.x;
upper = w.ws_col;
dir = &point.direction.x;
},
'y' => {
cor = &point.y;
upper = w.ws_row;
dir = &point.direction.y;
},
'X' => {
cor = &point.x;
upper = w.ws_col;
dir = &point.direction.y;
dir.* = -1;
},
'Y' => {
cor = &point.y;
upper = w.ws_row;
dir = &point.direction.y;
dir.* = -1;
},
else => {},
}
if (cor.* < upper) {
cor.* += dir.*;
}
if (cor.* >= upper or cor.* <= 0) {
dir.* *= -1;
cor.* += dir.*;
}
}
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) { //-1 ??