-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.c
53 lines (49 loc) · 1.31 KB
/
utils.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
// This is crap that I don't need anymore but feel bad about throwing away
#include <stdio.h>
#include "main.c"
void drawLine(Color *canvas, Vector2 start, Vector2 end, Color color) {
if (canvas == NULL) {
printf("Canvas is NULL.\n");
return;
}
if (start.x == end.x) {
// Vertical line
int yStart = (int)start.y;
int yEnd = (int)end.y;
int yInc = yStart < yEnd ? 1 : -1;
for (int y = yStart; y != yEnd + yInc; y += yInc) {
setPixel(canvas, (Vector2){start.x, (float)y}, color);
}
return;
}
if (start.y == end.y) {
// Horizontal line
int xStart = (int)start.x;
int xEnd = (int)end.x;
int xInc = xStart < xEnd ? 1 : -1;
for (int x = xStart; x != xEnd + xInc; x += xInc) {
setPixel(canvas, (Vector2){(float)x, start.y}, color);
}
return;
}
// Diagonal line using Bresenham's algorithm
int dx = fabsf(end.x - start.x), sx = start.x < end.x ? 1 : -1;
int dy = -fabsf(end.y - start.y), sy = start.y < end.y ? 1 : -1;
int err = dx + dy, e2;
Vector2 current = start;
while (1) {
setPixel(canvas, current, color);
if (current.x == end.x && current.y == end.y) {
break;
}
e2 = 2 * err;
if (e2 >= dy) {
err += dy;
current.x += sx;
}
if (e2 <= dx) {
err += dx;
current.y += sy;
}
}
}