Add bresenham variants
This commit is contained in:
parent
4987faed56
commit
f834d31d46
61
main.zig
61
main.zig
@ -136,7 +136,11 @@ fn fillBuffer() !void {
|
|||||||
};
|
};
|
||||||
_ = vec;
|
_ = vec;
|
||||||
//triangle(vec);
|
//triangle(vec);
|
||||||
_ = try bresenham(buffer, w, Point{ .x = 10, .y = 40, .z = 0 }, Point{ .x = 30, .y = 10, .z = 0 });
|
_ = 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 = 40, .z = 0 });
|
||||||
|
_ = try bresenham(buffer, w, Point{ .x = 30, .y = 30, .z = 0 }, Point{ .x = 60, .y = 10, .z = 0 });
|
||||||
try file.writeAll(buffer);
|
try file.writeAll(buffer);
|
||||||
}
|
}
|
||||||
fn pixel(buffer: []u8, w: Winsize, x: i64, y: i64, symbol: u21) !void {
|
fn pixel(buffer: []u8, w: Winsize, x: i64, y: i64, symbol: u21) !void {
|
||||||
@ -152,33 +156,44 @@ fn pixel(buffer: []u8, w: Winsize, x: i64, y: i64, symbol: u21) !void {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
fn bresenham(buf: []u8, w: Winsize, p1: Point, p2: Point) !void {
|
fn bresenham(buf: []u8, w: Winsize, p1: Point, p2: Point) !void {
|
||||||
const x1: i32 = p1.x;
|
var x: i32 = p1.x;
|
||||||
const y1: i32 = p1.y;
|
var y: i32 = p1.y;
|
||||||
var x: i32 = 0;
|
//var x: i32 = 0;
|
||||||
var y: i32 = 0;
|
//var y: i32 = 0;
|
||||||
const dx = (p2.x - p1.x);
|
const dx = (p2.x - p1.x);
|
||||||
print("p2: {} - {},{}\n", .{ p2.y, p1.y, p2.y - p1.y });
|
print("p2: {} - {},{}\n", .{ p2.y, p1.y, p2.y - p1.y });
|
||||||
const dy: i64 = (p2.y - p1.y);
|
const dy: i64 = (p2.y - p1.y);
|
||||||
//const slope = dx/dy;
|
//const slope = dx/dy;
|
||||||
var er: i64 = 0;
|
var er: i64 = 0;
|
||||||
|
if (dx >= dy and dy > 0) {
|
||||||
if (dy > 0 and dy <= dx) {
|
while (x <= p2.x) : (x += 1) {
|
||||||
x = x1;
|
print("[bresenham]x:{},y:{},er:{},dx:{},dy:{}\n", .{ x, y, er, dx, dy });
|
||||||
y = y1;
|
_ = try pixel(buf, w, x, y, '.');
|
||||||
}
|
er += dy;
|
||||||
if (dy < 0 and @abs(dy) > dx) {
|
if (2 * er >= dx) {
|
||||||
print("octant2\n", .{});
|
y += 1;
|
||||||
x = y1;
|
er -= dx;
|
||||||
y = x1;
|
}
|
||||||
}
|
}
|
||||||
|
} else if (dx < dy and dy > 0) {
|
||||||
while (x <= p2.x) : (x += 1) {
|
while (y <= p2.y) : (y += 1) {
|
||||||
print("[bresenham]x:{},y:{},er:{},dx:{},dy:{}\n", .{ x, y, er, dx, dy });
|
print("[bresenham2]x:{},y:{},er:{},dx:{},dy:{}\n", .{ x, y, er, dx, dy });
|
||||||
_ = try pixel(buf, w, x, y, '.');
|
_ = try pixel(buf, w, x, y, ',');
|
||||||
er += dy;
|
er += dx;
|
||||||
if (2 * er >= dx) {
|
if (2 * er >= dy) {
|
||||||
y += 1;
|
x += 1;
|
||||||
er -= dx;
|
er -= dy;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else if (@abs(dy) < dx and dy < 0) {
|
||||||
|
while (x <= p2.x) : (x += 1) {
|
||||||
|
print("[bresenham3]x:{},y:{},er:{},dx:{},dy:{}\n", .{ x, y, er, dx, dy });
|
||||||
|
_ = try pixel(buf, w, x, y, 'x');
|
||||||
|
er -= dy;
|
||||||
|
if (2 * er >= dx) {
|
||||||
|
y -= 1;
|
||||||
|
er -= dx;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user