avr/animation/bresenham.c

33 lines
610 B
C

#include <stdlib.h>
void bresenham(int x1, int y1, int x2, int y2) {
int dx = x2 - x1;
int dy = y2 - y1;
int m = 2 * dy;
int err = 0;
if (m > 0) {
err = m - dx;
} else if (m < 0) {
err = m + dx;
}
for (int x = x1, y = y1; x <= x2; x++) {
printf("(%d, %d) [%d]\n", x, y, err);
err += m;
if (m > 0) {
if (err >= 0) {
y++;
err -= 2 * dx;
}
} else if (m < 0) {
if (err <= 0) {
y--;
err += 2 * dx;
}
}
}
}