Browse Source

ltnant.c: forgot to adjust index cut off, also refined the code a bit

feature/2015
Christian Kroll 11 years ago
parent
commit
d4dbc53e5a
  1. 54
      src/animations/ltn_ant.c

54
src/animations/ltn_ant.c

@ -35,13 +35,13 @@
#define NX (UNUM_COLS - 1u) #define NX (UNUM_COLS - 1u)
#define NY (UNUM_ROWS - 1u) #define NY (UNUM_ROWS - 1u)
static coord_t const xdcomp[] = {0, P, 0, NX}; static coord_t const xdcomp[] = {0, NX, 0, P};
static coord_t const ydcomp[] = {P, 0, NY, 0}; static coord_t const ydcomp[] = {P, 0, NY, 0};
typedef struct ant_s { typedef struct ant_s {
coord_t x, y; coord_t x, y; /* current postion */
coord_t ox, oy; /* Used to set old pixels to brightness 2 */ coord_t ox, oy; /* previous position, used to dim out old pixels */
unsigned char vector_index; unsigned char vector_index; /* index to one of (0,1),(1,0),(0,-1),(-1,0) */
} ant_t; } ant_t;
void ltn_ant() { void ltn_ant() {
@ -51,43 +51,35 @@ void ltn_ant() {
unsigned int cycles = 500; unsigned int cycles = 500;
/* Random start position and direction */ /* random start position and direction */
ant.x = random8() % UNUM_COLS; ant.x = ant.ox = random8() % UNUM_COLS;
ant.y = random8() % UNUM_ROWS; ant.y = ant.oy = random8() % UNUM_ROWS;
/* Make sure we do have a valid vector */ /* make sure we have a valid vector */
ant.vector_index = random8() % 4u; ant.vector_index = random8() % 4u;
ant.ox = ant.x; while(cycles--) {
ant.oy = ant.y; /* if the pixel is turned off turn it on */
while(cycles != 0) {
/* If the pixel is not set turn it on */
if(get_pixel((pixel) {ant.x, ant.y}) == 0) { if(get_pixel((pixel) {ant.x, ant.y}) == 0) {
setpixel((pixel) {ant.x, ant.y}, 3); setpixel((pixel) {ant.x, ant.y}, NUMPLANE);
ant.vector_index = (ant.vector_index + 3u) % 4u; // turn left
// turn right
ant.vector_index = (ant.vector_index + 1u) % 8u; /* dim the previous pixel */
setpixel((pixel){ant.ox, ant.oy}, NUMPLANE - 1);
/* Lets the last pixel be darker than the latest */
if((ant.ox != ant.x) || (ant.oy != ant.y))
setpixel((pixel) {ant.ox, ant.oy}, 2);
/* memorize this position */
ant.ox = ant.x; ant.ox = ant.x;
ant.oy = ant.y; ant.oy = ant.y;
/* if the pixel is turned on turn it off */
} else { } else {
setpixel((pixel) {ant.x, ant.y}, 0); setpixel((pixel) {ant.x, ant.y}, 0);
// turn left ant.vector_index = (ant.vector_index + 1u) % 4u; // turn right
ant.vector_index = (ant.vector_index + 3u) % 8u;
} }
wait(100); /* move to next pixel, playing field is modeled after a torus */
ant.x = (ant.x + xdcomp[ant.vector_index]) % UNUM_COLS;
ant.y = (ant.y + ydcomp[ant.vector_index]) % UNUM_ROWS;
/* Playing field is modeled after a torus */ wait(100);
ant.x = (coord_t)(ant.x + xdcomp[ant.vector_index]) % UNUM_COLS;
ant.y = (coord_t)(ant.y + ydcomp[ant.vector_index]) % UNUM_ROWS;
cycles--;
} }
wait(300);
} }

Loading…
Cancel
Save