Created
August 15, 2021 15:16
-
-
Save aolo2/1d71a39b15a00868081a55732562c888 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
static void | |
render_line(struct v2 from, struct v2 to, u32 *pixels, int width, int height, u32 color) | |
{ | |
if (from.x >= width) { | |
from.x = width - 1; | |
} | |
if (to.x >= width) { | |
to.x = width - 1; | |
} | |
if (from.y >= height) { | |
from.y = height - 1; | |
} | |
if (to.y >= height) { | |
to.y = height - 1; | |
} | |
if (from.y == to.y) { | |
if (to.x < from.x) { | |
int tmp = to.x; | |
to.x = from.x; | |
from.x = tmp; | |
} | |
u32 *out = pixels + width * from.y + from.x; | |
// printf("%ld %ld -> %ld %ld\n", from.x, from.y, to.x, to.y); | |
for (int x = from.x; x <= to.x; ++x) { | |
// printf("%d\n", x); | |
*out = color; | |
++out; | |
} | |
} else if (from.x == to.x) { | |
if (to.y < from.y) { | |
int tmp = to.y; | |
to.y = from.y; | |
from.y = tmp; | |
} | |
u32 *out = pixels + width * from.y + from.x; | |
//printf("%ld %ld -> %ld %ld\n", from.x, from.y, to.x, to.y); | |
for (int y = from.y; y <= to.y; ++y) { | |
out = pixels + width * y + from.x; | |
*out = color; | |
} | |
} else { | |
int x0 = from.x; | |
int y0 = from.y; | |
int x1 = to.x; | |
int y1 = to.y; | |
int dx = abs(x1 - x0); | |
int sx = (x0 < x1 ? 1 : -1); | |
int dy = -abs(y1 - y0); | |
int sy = (y0 < y1 ? 1 : -1); | |
int err = dx+dy; | |
while (0 <= x0 && x0 < width && 0 <= y0 && y0 < height) { | |
u32 *out = pixels + width * y0 + x0; | |
*out = color; | |
if (x0 == x1 && y0 == y1) { | |
break; | |
} | |
int e2 = 2 * err; | |
if (e2 >= dy) { | |
err += dy; | |
x0 += sx; | |
} | |
if (e2 <= dx) { | |
err += dx; | |
y0 += sy; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment