From 42068ebcb63969b7339736b2806d949c3071a351 Mon Sep 17 00:00:00 2001 From: Simon Forman Date: Sun, 26 Feb 2023 20:02:14 -0800 Subject: [PATCH] Is this fun? Kinda. I should break out the graph papaer and Abrash's Black Book and figure out WTF I'm doing rather than just noodling around, eh? --- implementations/uvm-ncc/graphics.h | 25 ++++++++++++++++++++----- implementations/uvm-ncc/xerblin.c | 5 ++++- 2 files changed, 24 insertions(+), 6 deletions(-) diff --git a/implementations/uvm-ncc/graphics.h b/implementations/uvm-ncc/graphics.h index a74cad8..1c27b46 100644 --- a/implementations/uvm-ncc/graphics.h +++ b/implementations/uvm-ncc/graphics.h @@ -96,23 +96,38 @@ plot_pixel(u32* dest, size_t dest_stride, u64 x, u64 y, u32 color, u8 alpha) void -carefree_wu_line(u32* dest, size_t dest_stride, u64 x, u64 y, int w, int h, u32 color) +carefree_wu_line(u32* dest, size_t dest_stride, u64 x, u64 y, u64 w, u64 h, u32 color) { // Yeah... crunchy fun for the whole family. // https://dl.acm.org/doi/pdf/10.1145/127719.122734 // > Without loss of generality only lines in the first octant are considered. - assert(w > 0 && h > 0 && h >= w); + assert(w > 0 && h > 0 && w >= h); // > We translate the point (x0, y0) to the origin, // so y = kx where k = h/w with k <= 1 + u16 k = 0xFFFF * h / w; + //print_i64(w); print_str(" x "); print_i64(h); print_endl(); + print_i64(k>>8); print_endl(); u64 x1 = x + w; u64 y1 = y + h; - int d = 0; + u16 d = 0; while (x1 > x) { - plot_pixel(dest, dest_stride, x, y, color, 0xFF); - plot_pixel(dest, dest_stride, x1, y1, color, 0xFF); + //print_i64(d); print_endl(); + if (d) { + plot_pixel(dest, dest_stride, x, y, color, d >> 8); + plot_pixel(dest, dest_stride, x1, y1, color, d >> 8); + } + plot_pixel(dest, dest_stride, x, y - 1, color, 0xFF - (d >> 8)); + plot_pixel(dest, dest_stride, x1, y1 - 1, color, 0xFF - (d >> 8)); ++x; --x1; + d = d + k; + if (d > 0xFFFF) { + d = 0; + // opposite (de-)increment because computer y is negative cartesian y + ++y; + --y1; + } } } diff --git a/implementations/uvm-ncc/xerblin.c b/implementations/uvm-ncc/xerblin.c index 820ca9f..19a6529 100644 --- a/implementations/uvm-ncc/xerblin.c +++ b/implementations/uvm-ncc/xerblin.c @@ -104,7 +104,10 @@ main() u64 w = 3 + 26 * font_Inconsolata_22_width; u64 h = 4 + 4 * font_Inconsolata_22_height; carefree_draw_box(frame_buffer, FRAME_WIDTH, 126, 126, w, h, WHITE); - carefree_wu_line(frame_buffer, FRAME_WIDTH, 10, 256, 100, 200, WHITE); + w = 200; + for (w = 101; w < (FRAME_WIDTH - 10); w = w + 3) { + carefree_wu_line(frame_buffer, FRAME_WIDTH, 10, 256, w, 100, WHITE); + } window_draw_frame(wid, frame_buffer); window_on_keydown(wid, keydown); window_on_mousedown(wid, mousedown);