> show canvas only <


/* built with Studio Sketchpad: 
 *   https://sketchpad.cc
 * 
 * observe the evolution of this sketch: 
 *   https://studio.sketchpad.cc/sp/pad/view/ro.ZaOSineHlQ4/rev.1531
 * 
 * authors: 
 *   
 *   
 *   
 *   
 *   
 *   Juergen Kleinowitz
 *   
 *   
 *   
 *   

 * license (unless otherwise specified): 
 *   creative commons attribution-share alike 3.0 license.
 *   https://creativecommons.org/licenses/by-sa/3.0/ 
 */ 



// This sketch builds on a prior work, "Langton's Ant", created by Zach Denton
// http://studio.sketchpad.cc/sp/pad/view/ro.9DuotR8tg4asc/rev.743

//Run it and interesting stuff will happen, eventually ;)
//All i added was the possibility to add more colors, every color lets the ant either turn left or right, according to what was saved in leftRight array

int[][] grid;
int[] ant;
int L = 1;
int R = -1;

//int[] leftRight = {L,R,R,R,R,R,L,L,R}; //Fills space in a square around itself

//Other possible values from wikipedia
//int[] leftRight = {L,R,R,R,R,L};
int[] leftRight = {R,R,L,L,L,R,L,L,L,R,R,R}; //Should create a moving triangel shape
//int[] leftRight = {L,L,R,R}; //Produces a symetrical pattern
//int[] leftRight = {L,R};

Color[] cols;
int cell_width = 4;
int grid_width, grid_height;
float direction = HALF_PI;
int step=0;

void setup() {
    frameRate(120);
    background(0);
    size(800, 600);
    grid_width = width / cell_width;
    grid_height = height / cell_width;
    grid = new int[grid_width][grid_height];
    ant = [round(grid_width / 2), round(grid_height / 2)];
    cols = genColors(leftRight.length);
} 

void draw() {
    int lr = grid[ant[0]][ant[1]];
    direction+=HALF_PI * leftRight[lr];
    grid[ant[0]][ant[1]] = switchColor(lr);
    fill(cols[lr]);
    
    rect(ant[0]*cell_width, ant[1]*cell_width, cell_width, cell_width);
    
    ant[0] += round(cos(direction));
    
    if (ant[0] < 0) {
        ant[0] = grid_width-1;
    } else if (ant[0] > grid_width-1) {
        ant[0] = 0;
    }
    ant[1] += round(sin(direction));
    
    if (ant[1] < 0) {
        ant[1] = grid_height-1;
    } else if (ant[1] > grid_height-1) {
        ant[1] = 0;
    }
}

int switchColor(int lr) {
    if (lr < leftRight.length-1) {
        return lr=lr+1;
    } else {
        return 0;
    }
}
cols[] genColors(int length) {
    Color[] cols = new Color[length];
    for (int i = 0; i<length;i++) {
        cols[i] = color(random(255), random(255), random(255));
    }
    return cols;
}