avr/animation/animation.c

44 lines
932 B
C

#include "animation.h"
line_t line_new(uint8_t x0, uint8_t y0, uint8_t x1, uint8_t y1) {
uint8_t dx = x1 - x0;
int8_t dy = y1 - y0;
int8_t inc = dy > 0 ? 1 : dy == 0 ? 0 : -1;
int8_t m = 2 * dy;
int8_t error = 0;
if (m > 0) {
error = m - dx;
} else {
error = m + dx;
}
return (line_t){ .y = y0, .m = m, .error = error, .inc = inc, .dx = dx };
}
uint8_t line_next(line_t *slope) {
slope->error += slope->m;
if (slope->m > 0) {
if (slope->error >= 0) {
slope->y += slope->inc;
slope->error -= 2 * slope->dx;
}
} else {
if (slope->error <= 0) {
slope->y += slope->inc;
slope->error += 2 * slope->dx;
}
}
return slope->y;
}
/*
void line_print(line_t *line) {
printf("y: %d\tm: %d\terror: %d\tinc: %d\t:dx: %d\n", line->y, line->m, line->error, line->inc, line->dx);
}
*/