Add lines

This commit is contained in:
2024-02-25 22:23:43 -08:00
parent be9c443c58
commit 3440669227
5 changed files with 58 additions and 10 deletions

View File

@ -6,4 +6,15 @@ sys_i32 sys_max_i32(sys_i32 x, sys_i32 y) {
sys_i32 sys_min_i32(sys_i32 x, sys_i32 y) {
return x < y ? x : y;
}
sys_i32 sys_abs_i32(sys_i32 x) {
if (x < 0) { return -x; }
return x;
}
sys_i32 sys_sgn_i32(sys_i32 x) {
if (x < 0) { return -1; }
if (x > 0) { return 1; }
return 0;
}

View File

@ -11,6 +11,8 @@ sys_screen_color sys_make_screen_color(uint8_t r, uint8_t g, uint8_t b);
sys_i32 sys_max_i32(sys_i32 x, sys_i32 y);
sys_i32 sys_min_i32(sys_i32 x, sys_i32 y);
sys_i32 sys_abs_i32(sys_i32 x);
sys_i32 sys_sgn_i32(sys_i32 x);
#define SYS_COLOR_TRANSPARENT 255
#define SYS_COLOR_N 256

View File

@ -121,9 +121,12 @@ void sys_oval_fill(
void sys_oval_draw_ext(
sys_i32 x0, sys_i32 y0, sys_i32 x1, sys_i32 y1, sys_color c, bool fill
) {
assert(sys_get_initialized());
if (x0 == x1 || y0 == y1) { return; }
if (x0 > x1) { sys_i32 tmp = x0; x0 = x1; x1 = tmp; }
if (y0 > y1) { sys_i32 tmp = y0; y0 = y1; y1 = tmp; }
// alois' algorithm for this implies the bounds are inclusive
x1 -= 1; y1 -= 1;
@ -162,6 +165,28 @@ void sys_oval_draw_ext(
}
}
void sys_line_draw(
sys_i32 x0, sys_i32 y0, sys_i32 x1, sys_i32 y1, sys_color c
) {
assert(sys_get_initialized());
if (x0 == x1 || y0 == y1) { return; }
sys_i32 dx = sys_abs_i32(x1 - x0);
sys_i32 sx = sys_sgn_i32(x1 - x0);
sys_i32 dy = -sys_abs_i32(y1 - y0);
sys_i32 sy = sys_sgn_i32(y1 - y0);
sys_i32 err = dx + dy;
while (true) {
if (x0 == x1 || y0 == y1) { return; }
sys_pixel_internal_set(x0, y0, c);
sys_i32 e2 = 2 * err;
if (e2 >= dy) { err += dy; x0 += sx; }
if (e2 <= dx) { err += dx; y0 += sy; }
}
}
void sys_spal_set(sys_color c0, sys_screen_color rc1) {
assert(sys_get_initialized());

View File

@ -97,6 +97,8 @@ void sys_oval_draw_ext(
/**
* Draw a line from x0 to y0.
*
* The point `(x1, y1)` is not drawn. (because the line is a half-open interval)
*/
void sys_line_draw(sys_i32 x0, sys_i32 y0, sys_i32 x1, sys_i32 y1, sys_color c);