Restructure into different files
This commit is contained in:
103
primitives.zig
103
primitives.zig
@@ -1,3 +1,7 @@
|
||||
const std = @import("std");
|
||||
const term = @import("term.zig");
|
||||
const Winsize = term.Winsize;
|
||||
|
||||
pub const Direction = struct {
|
||||
x: i8 = 1,
|
||||
y: i8 = 1,
|
||||
@@ -29,4 +33,101 @@ pub var vec2 = Vec3{
|
||||
.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 } },
|
||||
};
|
||||
pub var vec3: [10]Vec3 = undefined;
|
||||
pub var obj: [10]Vec3 = undefined;
|
||||
|
||||
pub 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 ??
|
||||
const slice = buffer[i .. i + 4];
|
||||
_ = try std.unicode.utf8Encode(symbol, slice);
|
||||
//TODO: check length, set not used bytes to zero
|
||||
} else {
|
||||
//print("Error illegal memory access\n", .{});
|
||||
}
|
||||
}
|
||||
pub fn bresenham(buf: []u8, w: Winsize, p1: Point, p2: Point) !void {
|
||||
var x: i32 = p1.x;
|
||||
var y: i32 = p1.y;
|
||||
const dx = (p2.x - p1.x);
|
||||
const dy = (p2.y - p1.y);
|
||||
var er: i64 = 0;
|
||||
if (dx >= dy and dy > 0 and dx > 0) {
|
||||
while (x <= p2.x) : (x += 1) {
|
||||
_ = try pixel(buf, w, x, y, '█');
|
||||
er += dy;
|
||||
if (2 * er >= dx) {
|
||||
y += 1;
|
||||
er -= dx;
|
||||
}
|
||||
}
|
||||
} else if (dx <= dy and dy >= 0 and dx >= 0) {
|
||||
while (y <= p2.y) : (y += 1) {
|
||||
_ = try pixel(buf, w, x, y, '█');
|
||||
er += dx;
|
||||
if (2 * er >= dy) {
|
||||
x += 1;
|
||||
er -= dy;
|
||||
}
|
||||
}
|
||||
} 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;
|
||||
if (2 * er >= dx) {
|
||||
y -= 1;
|
||||
er -= dx;
|
||||
}
|
||||
}
|
||||
} 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;
|
||||
if (2 * er >= dy) {
|
||||
x += 1;
|
||||
er += dy;
|
||||
}
|
||||
}
|
||||
} 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);
|
||||
if (2 * er >= dy) {
|
||||
x -= 1;
|
||||
er -= dy;
|
||||
}
|
||||
}
|
||||
} 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;
|
||||
if (2 * er >= @abs(dx)) {
|
||||
y += 1;
|
||||
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, '█');
|
||||
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) {
|
||||
//print("x:{},y:{},er:{},dx:{},dy:{}\n", .{ x, y, er, dx, dy });
|
||||
_ = try pixel(buf, w, x, y, '█');
|
||||
er += @abs(dx);
|
||||
if (2 * er >= @abs(dx)) {
|
||||
x -= 1;
|
||||
er -= @abs(dy);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
pub fn triangle(buffer: []u8, w: Winsize, vector: Vec3) !void {
|
||||
try bresenham(buffer, w, vector.a, vector.b);
|
||||
try bresenham(buffer, w, vector.b, vector.c);
|
||||
try bresenham(buffer, w, vector.c, vector.a);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user