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

This commit is contained in:
Christian Kroll 2014-05-17 19:28:57 +02:00
parent 4fd59d8edf
commit d4dbc53e5a
1 changed files with 23 additions and 31 deletions

View File

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