bresenham done
This commit is contained in:
parent
d712a7d8d4
commit
93ce68f527
28
main.zig
28
main.zig
@ -144,6 +144,8 @@ fn fillBuffer() !void {
|
|||||||
_ = try bresenham(buffer, w, Point{ .x = 60, .y = 30, .z = 0 }, Point{ .x = 70, .y = 5, .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 = 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 = 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);
|
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 {
|
||||||
@ -162,12 +164,11 @@ fn bresenham(buf: []u8, w: Winsize, p1: Point, p2: Point) !void {
|
|||||||
//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 = (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 and dx > 0) {
|
if (dx >= dy and dy > 0 and dx > 0) {
|
||||||
while (x <= p2.x) : (x += 1) {
|
while (x <= p2.x) : (x += 1) {
|
||||||
print("[bresenham]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 += dy;
|
||||||
if (2 * er >= dx) {
|
if (2 * er >= dx) {
|
||||||
@ -177,7 +178,6 @@ fn bresenham(buf: []u8, w: Winsize, p1: Point, p2: Point) !void {
|
|||||||
}
|
}
|
||||||
} else if (dx < dy and dy > 0 and dx > 0) {
|
} else if (dx < dy and dy > 0 and dx > 0) {
|
||||||
while (y <= p2.y) : (y += 1) {
|
while (y <= p2.y) : (y += 1) {
|
||||||
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 += dx;
|
er += dx;
|
||||||
if (2 * er >= dy) {
|
if (2 * er >= dy) {
|
||||||
@ -187,7 +187,6 @@ fn bresenham(buf: []u8, w: Winsize, p1: Point, p2: Point) !void {
|
|||||||
}
|
}
|
||||||
} else if (@abs(dy) < dx and dy < 0 and dx > 0) {
|
} else if (@abs(dy) < dx and dy < 0 and dx > 0) {
|
||||||
while (x <= p2.x) : (x += 1) {
|
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, '🡇');
|
_ = try pixel(buf, w, x, y, '🡇');
|
||||||
er -= dy;
|
er -= dy;
|
||||||
if (2 * er >= dx) {
|
if (2 * er >= dx) {
|
||||||
@ -197,7 +196,6 @@ fn bresenham(buf: []u8, w: Winsize, p1: Point, p2: Point) !void {
|
|||||||
}
|
}
|
||||||
} else if (@abs(dy) > dx and dy < 0 and dx > 0) {
|
} else if (@abs(dy) > dx and dy < 0 and dx > 0) {
|
||||||
while (y >= p2.y) : (y -= 1) {
|
while (y >= p2.y) : (y -= 1) {
|
||||||
print("[bresenham4]x:{},y:{},er:{},dx:{},dy:{}\n", .{ x, y, er, dx, dy });
|
|
||||||
_ = try pixel(buf, w, x, y, '=');
|
_ = try pixel(buf, w, x, y, '=');
|
||||||
er += dx;
|
er += dx;
|
||||||
if (2 * er >= dy) {
|
if (2 * er >= dy) {
|
||||||
@ -207,7 +205,6 @@ fn bresenham(buf: []u8, w: Winsize, p1: Point, p2: Point) !void {
|
|||||||
}
|
}
|
||||||
} else if (@abs(dx) <= dy and dx < 0 and dy > 0) {
|
} else if (@abs(dx) <= dy and dx < 0 and dy > 0) {
|
||||||
while (y <= p2.y) : (y += 1) {
|
while (y <= p2.y) : (y += 1) {
|
||||||
print("[bresenham5]x:{},y:{},er:{},dx:{},dy:{}\n", .{ x, y, er, dx, dy });
|
|
||||||
_ = try pixel(buf, w, x, y, '5');
|
_ = try pixel(buf, w, x, y, '5');
|
||||||
er += @abs(dx);
|
er += @abs(dx);
|
||||||
if (2 * er >= dy) {
|
if (2 * er >= dy) {
|
||||||
@ -217,7 +214,6 @@ fn bresenham(buf: []u8, w: Winsize, p1: Point, p2: Point) !void {
|
|||||||
}
|
}
|
||||||
} else if (@abs(dx) > dy and dy > 0 and dx < 0) {
|
} else if (@abs(dx) > dy and dy > 0 and dx < 0) {
|
||||||
while (x >= p2.x) : (x -= 1) {
|
while (x >= p2.x) : (x -= 1) {
|
||||||
print("[bresenham6]x:{},y:{},er:{},dx:{},dy:{}\n", .{ x, y, er, dx, dy });
|
|
||||||
_ = try pixel(buf, w, x, y, '6');
|
_ = try pixel(buf, w, x, y, '6');
|
||||||
er += dy;
|
er += dy;
|
||||||
if (2 * er >= @abs(dx)) {
|
if (2 * er >= @abs(dx)) {
|
||||||
@ -225,6 +221,24 @@ fn bresenham(buf: []u8, w: Winsize, p1: Point, p2: Point) !void {
|
|||||||
er -= @abs(dx);
|
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, '7');
|
||||||
|
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) {
|
||||||
|
_ = try pixel(buf, w, x, y, '8');
|
||||||
|
er += @abs(dx);
|
||||||
|
if (2 * er >= @abs(dx)) {
|
||||||
|
x -= 1;
|
||||||
|
er -= @abs(dx);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user