avr/animation/animation.c

83 lines
1.7 KiB
C

#include "animation.h"
#include <stdio.h>
time_line_t time_line_new(int t0, int y0, int t1, int y1) {
int dt = t1 - t0;
int dy = y1 - y0;
int de = 2 * dy;
int inc = dy > 0 ? 1 : dy == 0 ? 0 : -1;
int error;
if (de > 0) {
error = de - dt;
} else {
error = de + dt;
}
return (time_line_t){ .y = y0, .dt = dt, .de = de, .error = error, .inc = inc, .dy = dy };
}
int time_line_next(time_line_t *self) {
int error = self->error + self->de;
if (self->de > 0) {
while (error >= 0) {
self->y += self->inc;
error -= 2 * self->dt;
}
} else {
while (error < 0) {
self->y -= self->inc;
error += 2 * self->dt;
}
}
self->error = error;
return self->y;
}
/*
time_line_t time_line_new(int t0, int y0, int t1, int y1) {
int dt = t1 - t0;
int dy = y1 - y0;
int inc = dy > 0 ? 1 : dy == 0 ? 0 : -1;
int m = 2 * dy;
int error = 0;
if (m > 0) {
error = m - dt;
} else {
error = m + dt;
}
return (time_line_t){ .y = y0, .m = m, .error = error, .inc = inc, .dt = dt };
}
*/
/*
int time_line_next(time_line_t *slope) {
slope->error += slope->m;
if (slope->m > 0) {
if (slope->error >= 0) {
slope->y += slope->inc;
slope->error -= 2 * slope->dt;
}
} else {
if (slope->error <= 0) {
slope->y += slope->inc;
slope->error += 2 * slope->dt;
}
}
return slope->y;
}
*/
void time_line_print(time_line_t *self) {
printf("y: %d\tde: %d\terror: %d\tinc: %d\t:dt: %d\n", self->y, self->de, self->error, self->inc, self->dt);
}