mirror of
https://codeberg.org/redict/redict.git
synced 2025-01-23 16:48:27 -05:00
LOLWUT: draw lines using Bresenham algorithm.
This commit is contained in:
parent
7777379814
commit
a974531d1a
28
src/lolwut.c
28
src/lolwut.c
@ -88,7 +88,7 @@ void lwFreeCanvas(lwCanvas *canvas) {
|
|||||||
* dot will be displyed, and 1 means dot will be displayed.
|
* dot will be displyed, and 1 means dot will be displayed.
|
||||||
* Coordinates are arranged so that left-top corner is 0,0. You can write
|
* Coordinates are arranged so that left-top corner is 0,0. You can write
|
||||||
* out of the size of the canvas without issues. */
|
* out of the size of the canvas without issues. */
|
||||||
void lwPutPixel(lwCanvas *canvas, int x, int y, int color) {
|
void lwDrawPixel(lwCanvas *canvas, int x, int y, int color) {
|
||||||
if (x < 0 || x >= canvas->width ||
|
if (x < 0 || x >= canvas->width ||
|
||||||
y < 0 || y >= canvas->height) return;
|
y < 0 || y >= canvas->height) return;
|
||||||
canvas->pixels[x+y*canvas->width] = color;
|
canvas->pixels[x+y*canvas->width] = color;
|
||||||
@ -101,6 +101,29 @@ int lwGetPixel(lwCanvas *canvas, int x, int y) {
|
|||||||
return canvas->pixels[x+y*canvas->width];
|
return canvas->pixels[x+y*canvas->width];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Draw a line from x1,y1 to x2,y2 using the Bresenham algorithm. */
|
||||||
|
void lwDrawLine(lwCanvas *canvas, int x1, int y1, int x2, int y2, int color) {
|
||||||
|
int dx = abs(x2-x1);
|
||||||
|
int dy = abs(y2-y1);
|
||||||
|
int sx = (x1 < x2) ? 1 : -1;
|
||||||
|
int sy = (y1 < y2) ? 1 : -1;
|
||||||
|
int err = dx-dy, e2;
|
||||||
|
|
||||||
|
while(1) {
|
||||||
|
lwDrawPixel(canvas,x1,y1,color);
|
||||||
|
if (x1 == x2 && y1 == y2) break;
|
||||||
|
e2 = err*2;
|
||||||
|
if (e2 > -dy) {
|
||||||
|
err -= dy;
|
||||||
|
x1 += sx;
|
||||||
|
}
|
||||||
|
if (e2 < dx) {
|
||||||
|
err += dx;
|
||||||
|
y1 += sy;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/* Converts the canvas to an SDS string representing the UTF8 characters to
|
/* Converts the canvas to an SDS string representing the UTF8 characters to
|
||||||
* print to the terminal in order to obtain a graphical representaiton of the
|
* print to the terminal in order to obtain a graphical representaiton of the
|
||||||
* logical canvas. The actual returned string will require a terminal that is
|
* logical canvas. The actual returned string will require a terminal that is
|
||||||
@ -133,8 +156,9 @@ sds lwRenderCanvas(lwCanvas *canvas) {
|
|||||||
int main(void) {
|
int main(void) {
|
||||||
lwCanvas *c = lwCreateCanvas(80,80);
|
lwCanvas *c = lwCreateCanvas(80,80);
|
||||||
for (int i = 0; i < 40; i++) {
|
for (int i = 0; i < 40; i++) {
|
||||||
lwPutPixel(c,i,i,1);
|
lwDrawPixel(c,i,i,1);
|
||||||
}
|
}
|
||||||
|
lwDrawLine(c,10,10,60,30,1);
|
||||||
sds rendered = lwRenderCanvas(c);
|
sds rendered = lwRenderCanvas(c);
|
||||||
printf("%s\n", rendered);
|
printf("%s\n", rendered);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user