fixed langton ant for resoltuions with width!=height (my fault), also

removed those annoying 45° vectors
This commit is contained in:
Christian Kroll 2014-05-15 19:28:25 +02:00
parent 22f14abb32
commit dc0402251c
1 changed files with 16 additions and 30 deletions

View File

@ -35,27 +35,20 @@
#define NX (UNUM_COLS - 1u) #define NX (UNUM_COLS - 1u)
#define NY (UNUM_ROWS - 1u) #define NY (UNUM_ROWS - 1u)
#if UNUM_ROWS == UNUM_COLS static coord_t const xdcomp[] = {0, P, 0, NX};
static coord_t const dcomp[] = {0, P, NX}; static coord_t const ydcomp[] = {P, 0, NY, 0};
#define xdcomp dcomp
#define ydcomp dcomp
#else
static coord_t const xdcomp[] = {0, P, NX};
static coord_t const ydcomp[] = {0, P, NY};
#endif
typedef struct ant_s {
coord_t x, y;
coord_t ox, oy; /* Used to set old pixels to brightness 2 */
unsigned char vector_index;
} ant_t;
void ltn_ant() { void ltn_ant() {
clear_screen(0); clear_screen(0);
struct { ant_t ant;
coord_t x, y;
coord_t ox, oy; /* Used to set old pixels to brightness 2 */
coord_t dx, dy; /* Vector can only be (0,1),(1,0),(0,-1),(-1,0) */
} ant;
unsigned char temp;
unsigned int cycles = 500; unsigned int cycles = 500;
/* Random start position and direction */ /* Random start position and direction */
@ -63,10 +56,7 @@ void ltn_ant() {
ant.y = random8() % UNUM_ROWS; ant.y = random8() % UNUM_ROWS;
/* Make sure we do have a valid vector */ /* Make sure we do have a valid vector */
ant.dx = xdcomp[random8() % 3]; ant.vector_index = random8() % 4u;
do {
ant.dy = ydcomp[random8() % 3];
} while(ant.dx == ant.dy);
ant.ox = ant.x; ant.ox = ant.x;
ant.oy = ant.y; ant.oy = ant.y;
@ -76,9 +66,8 @@ void ltn_ant() {
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}, 3);
temp = ant.dx; // turn right
ant.dx = ant.dy; ant.vector_index = (ant.vector_index + 1u) % 8u;
ant.dy = -temp; /* Turn 90 degrees to the right */
/* Lets the last pixel be darker than the latest */ /* Lets the last pixel be darker than the latest */
if((ant.ox != ant.x) || (ant.oy != ant.y)) if((ant.ox != ant.x) || (ant.oy != ant.y))
@ -86,20 +75,17 @@ void ltn_ant() {
ant.ox = ant.x; ant.ox = ant.x;
ant.oy = ant.y; ant.oy = ant.y;
} else { } else {
setpixel((pixel) {ant.x, ant.y}, 0); setpixel((pixel) {ant.x, ant.y}, 0);
// turn left
temp = ant.dy; ant.vector_index = (ant.vector_index + 3u) % 8u;
ant.dy = ant.dx;
ant.dx = -temp; /* Turn 90 degrees to the left */
} }
wait(100); wait(100);
/* Playing field is modeled after a torus */ /* Playing field is modeled after a torus */
ant.x = (coord_t)(ant.x + ant.dx) % UNUM_COLS; ant.x = (coord_t)(ant.x + xdcomp[ant.vector_index]) % UNUM_COLS;
ant.y = (coord_t)(ant.y + ant.dy) % UNUM_ROWS; ant.y = (coord_t)(ant.y + ydcomp[ant.vector_index]) % UNUM_ROWS;
cycles--; cycles--;
} }