Just watch as it grows! Will take longer on low-end devices. Press space to switch pattern
A fork of Koch-Like Fractal with Turmites by 16384
xxxxxxxxxx
let timeSteps = 0;
let colorNum = 10;
let stateNum = 10;
let type = 0;
let antX, antY;
let antDir;
let antState = 0;
let stt; //State Transition Table array
let states;
function setup() {
createCanvas(windowWidth, windowHeight);
noFill();
colorMode(HSB);
antDir = PI;
antX = width / 2;
antY = width / 2;
reset();
}
function draw() {
for (let t = 0; t < 1000; t++) turmite();
}
function reset() {
background(0, 0, 0);
antX = width / 2;
antY = height / 2;
states = [];
for (let x = 0; x < width; x++) {
states[x] = [];
for (let y = 0; y < height; y++) {
states[x][y] = 0;
}
}
stt = [];
for (let x = 0; x < stateNum; x++) {
stt[x] = [];
for (let y = 0; y < colorNum; y++) {
let turn = floor(random(5));
if (turn != 0) turn = pow(2, turn - 1);
stt[x][y] = createVector(floor(random(colorNum)), turn, floor(random(stateNum)));
}
}
manual();
}
function turmite() {
let currentColor = states[round(antX)][round(antY)];
let writeColor = stt[antState][currentColor].x;
let turn = stt[antState][currentColor].y;
antState = stt[antState][currentColor].z;
states[round(antX)][round(antY)] = writeColor;
stroke(map(writeColor, 0, colorNum - 1, 0, 255), 255, 255);
point(antX, antY);
switch (turn) {
case 1:
break;
case 2:
antDir += HALF_PI;
break;
case 4:
antDir += PI;
break;
case 8:
antDir -= HALF_PI;
break;
}
antX = round(antX + cos(antDir));
antY = round(antY + sin(antDir));
antX = (antX + width) % width;
antY = (antY + height) % height;
timeSteps++;
}
function manual() {
//Use for 3-State Manual turmites:
// stt[0][0] = createVector(1, 8, 1);
// stt[0][1] = createVector(1, 2, 0);
// stt[1][0] = createVector(1, 4, 1);
// stt[1][1] = createVector(1, 4, 2);
// stt[2][0] = createVector(0, 0, 0);
// stt[2][1] = createVector(0, 4, 0);
//Use for 2-State Manual Turmites
// stt[0][0] = createVector(1, 2, 1);
// stt[0][1] = createVector(0, 2, 1);
// stt[1][0] = createVector(1, 1, 0);
// stt[1][1] = createVector(1, 1, 1);
// presets();
}
function presets() {
switch (type) {
case 0:
colorNum = 2;
stateNum = 2;
createEmpty();
stt[0][0] = createVector(1, 1, 1);
stt[0][1] = createVector(1, 8, 0);
stt[1][0] = createVector(1, 2, 1);
stt[1][1] = createVector(0, 1, 0);
break;
case 1:
colorNum = 2;
stateNum = 2;
createEmpty();
stt[0][0] = createVector(1, 2, 1);
stt[0][1] = createVector(0, 2, 1);
stt[1][0] = createVector(1, 1, 0);
stt[1][1] = createVector(1, 1, 1);
break;
case 2:
colorNum = 2;
stateNum = 2;
createEmpty();
stt[0][0] = createVector(1, 2, 1);
stt[0][1] = createVector(1, 8, 1);
stt[1][0] = createVector(1, 2, 1);
stt[1][1] = createVector(0, 2, 0);
break;
case 3:
colorNum = 2;
stateNum = 2;
createEmpty();
stt[0][0] = createVector(1, 8, 0);
stt[0][1] = createVector(1, 2, 1);
stt[1][0] = createVector(0, 2, 0);
stt[1][1] = createVector(0, 8, 1);
break;
case 4:
colorNum = 2;
stateNum = 2;
createEmpty();
stt[0][0] = createVector(1, 8, 1);
stt[0][1] = createVector(1, 8, 1);
stt[1][0] = createVector(1, 2, 1);
stt[1][1] = createVector(0, 1, 0);
break;
case 5:
colorNum = 2;
stateNum = 3;
createEmpty();
stt[0][0] = createVector(1, 8, 1);
stt[0][1] = createVector(1, 2, 0);
stt[1][0] = createVector(1, 4, 1);
stt[1][1] = createVector(1, 4, 2);
stt[2][0] = createVector(0, 0, 0);
stt[2][1] = createVector(0, 4, 0);
break;
}
}
function createEmpty() {
stt = [];
for (let x = 0; x < stateNum; x++) {
stt[x] = [];
for (let y = 0; y < colorNum; y++) {
stt[x][y] = 0;
}
}
}
function keyPressed() {
if (key == '`') {
save("pic.png");
} else if (keyCode == 32) {
type = (type + 1) % 6;
reset();
}
}
function mousePressed() {
// print("-");
// for (let x = 0; x < stateNum; x++) {
// for (let y = 0; y < colorNum; y++) {
// print("State:" + x + "-Color:" + y + "-{" + stt[x][y].x + ", " + stt[x][y].y + ", " + stt[x][y].z + "}");
// }
// }
}