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?
This commit is contained in:
Simon Forman 2023-02-26 20:02:14 -08:00
parent 66d687bba6
commit 42068ebcb6
2 changed files with 24 additions and 6 deletions

View File

@ -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;
}
}
}

View File

@ -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);