Compare commits

...

2 Commits

Author SHA1 Message Date
jonathan santis
ece4540426 implement polygon
a bug in the fill algorythm where it fills every
2024-11-01 13:21:20 +01:00
jonathan santis
e91c744c16 LinkedList implemented 2024-11-01 11:34:24 +01:00
2 changed files with 35 additions and 27 deletions

View File

@ -39,6 +39,14 @@ fn draw() !void {
var slice: []u8 = undefined; var slice: []u8 = undefined;
var bytes_written: u3 = 0; var bytes_written: u3 = 0;
var buffer_size: u32 = 0; var buffer_size: u32 = 0;
var polygon = primitive.LinkedList().new(allocator);
try polygon.add(.{ .x = 5, .y = 5, .z = 0 });
try polygon.add(.{ .x = 67, .y = 13, .z = 0 });
try polygon.add(.{ .x = 77, .y = 33, .z = 0 });
try polygon.add(.{ .x = 48, .y = 21, .z = 0 });
try polygon.add(.{ .x = 21, .y = 39, .z = 0 });
try polygon.add(.{ .x = 24, .y = 23, .z = 0 });
w = try getTermDimension(); w = try getTermDimension();
buffer_size = w.ws_row; buffer_size = w.ws_row;
@ -62,7 +70,7 @@ fn draw() !void {
slice[bytes_written + ii] = 0; slice[bytes_written + ii] = 0;
} }
} }
try triangle(buffer, w, definitions.vec); //try triangle(buffer, w, definitions.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 = 12, .z = 0 });
try out.print("\x1B[2J", .{}); try out.print("\x1B[2J", .{});
@ -75,9 +83,10 @@ fn draw() !void {
// mv_axis_border_bounce(w, &primitive.vec.b, 'y'); // mv_axis_border_bounce(w, &primitive.vec.b, 'y');
// mv_axis_border_bounce(w, &primitive.vec.c, 'x'); // mv_axis_border_bounce(w, &primitive.vec.c, 'x');
// mv_axis_border_bounce(w, &primitive.vec.c, 'y'); // mv_axis_border_bounce(w, &primitive.vec.c, 'y');
try primitive.polygon_draw(buffer, w, &polygon);
try fill_draw(w, buffer); try fill_draw(w, buffer);
try out.print("{s}", .{buffer}); try out.print("{s}", .{buffer});
sleep(30000000); sleep(30000000);
} }
} }

View File

@ -56,17 +56,18 @@ pub fn LinkedList() type {
self.length += 1; self.length += 1;
} }
pub fn next(self: *Self) Point { pub fn next(self: *Self) Point {
var current: ?*Node = undefined; var elem: ?*Node = undefined;
if (self.current != null) {
current = self.current;
self.current = self.current.?.next;
} else {
current = self.head;
self.current = self.head;
}
return current.?.point; if (self.current == null) {
elem = self.head;
self.current = self.head.?.next;
} else {
elem = self.current;
self.current = self.current.?.next;
}
return elem.?.point;
} }
pub fn dump(self: *Self) ![]Point { pub fn dump(self: *Self) ![]Point {
const allocator = self.allocator; const allocator = self.allocator;
var node = self.head; var node = self.head;
@ -101,25 +102,23 @@ test "test linked list" {
const points: []Point = try polygon.dump(); const points: []Point = try polygon.dump();
for (points) |point| { for (points) |point| {
std.debug.print("x:{}\n", .{point.x}); std.debug.print("Point dump:{}\n", .{point});
std.debug.print("y:{}\n", .{point.y});
std.debug.print("z:{}\n", .{point.z});
} }
std.debug.print("next:{}\n", .{polygon.next()}); std.debug.print("next:{}\n", .{polygon.next()});
std.debug.print("next:{}\n", .{polygon.next()}); std.debug.print("next:{}\n", .{polygon.next()});
std.debug.print("next:{}\n", .{polygon.next()}); std.debug.print("typeof:{}\n", .{@TypeOf(polygon)});
std.debug.print("next:{}\n", .{polygon.next()}); }
std.debug.print("next:{}\n", .{polygon.next()});
std.debug.print("next:{}\n", .{polygon.next()}); pub fn polygon_draw(buf: []u8, w: Winsize, poly: *LinkedList()) !void {
std.debug.print("next:{}\n", .{polygon.next()}); var i: u8 = 0;
std.debug.print("next:{}\n", .{polygon.next()}); var previous: Point = poly.next();
std.debug.print("next:{}\n", .{polygon.next()}); var current: Point = undefined;
std.debug.print("next:{}\n", .{polygon.next()});
std.debug.print("next:{}\n", .{polygon.next()}); while (i < poly.length) : (i += 1) {
std.debug.print("next:{}\n", .{polygon.next()}); current = poly.next();
std.debug.print("next:{}\n", .{polygon.next()}); try bresenham(buf, w, previous, current);
std.debug.print("next:{}\n", .{polygon.next()}); previous = current;
std.debug.print("next:{}\n", .{polygon.next()}); }
} }
pub fn pixel(buffer: []u8, w: Winsize, x: i64, y: i64, symbol: u21) !void { pub fn pixel(buffer: []u8, w: Winsize, x: i64, y: i64, symbol: u21) !void {