if there's a way to iterate everything in the z-axis, it would be the best! ( as that was the first intention )
Below is the code:
Code: Select all
import peasy.*;
import toxi.geom.*;
PeasyCam cam;
int cols = 50; // population
int rows = 50;
int depth = 50;
CA [][][] grid = new CA [cols][rows][depth]; //declare two dimmensional array
void setup () {
size(1280, 720, P3D);
frameRate (12);
cam = new PeasyCam (this, 100);
for (int i = 0; i < cols; i++) {
for (int j = 0; j < rows; j++) {
for ( int k = 0; k < depth; k++) {
Vec3D ptLoc = new Vec3D ( i* 10, j * 10, k*10 );
grid [i][j][k] = new CA (ptLoc, i, j, k);
}
}
}
}
void draw () {
background (0);
stroke (255);
fill (255, 60);
// rect (0,0,600,600);
for (int i = 0; i < cols; i++) {
for (int j = 0; j < rows; j++) {
for ( int k = 0; k < depth; k++) {
grid[i][j][k].run();
}
}
}
for (int i = 0; i < cols; i++) {
for (int j = 0; j < rows; j++) {
for ( int k = 0; k < depth; k++) {
grid[i][j][k].updateType();
grid[i][j][k].loc.z = grid[i][j][k].loc.z ;
}
}
}
}
////////////////////////////////////////////////////CLASS CA
class CA {
Vec3D loc;
int x;
int y;
int z;
//specify your neighbour
int type = 0;
int futType = 100;
CA(Vec3D _loc, int _x, int _y, int _z) {
loc = _loc;
x = _x;
y = _y;
z = _z;
float rnd = random (100);
if (rnd < 50) {
type = 1;
}
}
void run() {
display();
evo1N();
}
void updateType() {
type = futType;
}
void evo1N() {
int count = 0;
if ( grid [(x+cols-1)%cols] [(y+rows-1)%rows ] [(z+depth+1) %depth].type == 1) count ++;
if ( grid [(x+cols)%cols] [(y+rows-1)%rows] [(z+depth-1) %depth].type == 1) count ++;
if ( grid [(x+cols+1)%cols] [(y+rows-1)%rows] [(z+depth) %depth].type == 1) count ++;
if ( grid [(x+cols-1)%cols] [(y+rows)%rows] [(z+depth-1) %depth].type == 1) count ++;
if ( grid [(x+cols+1)%cols] [(y+rows)%rows] [(z+depth) %depth].type == 1) count ++;
if ( grid [(x+cols-1)%cols] [(y+rows+1)%rows] [(z+depth) %depth].type == 1) count ++;
if ( grid [(x+cols)%cols] [(y+rows+1)%rows] [(z+depth+1) %depth].type == 1) count ++;
if ( grid [(x+cols+1)%cols] [(y+rows+1)%rows] [(z+depth+1) %depth].type == 1) count ++;
//if i have too many neighbours around me, i die out of the over population
if (type == 1 && count < 2) {
futType = 0;
}
if (type == 1 && count <=3 && count >=2) {
futType = 1;
}
if (type ==1 && count > 3) {
futType = 0;
}
if (type == 0 && count == 3) {
futType = 1;
}
}
void display () {
if (type == 1) {
stroke (255);
strokeWeight ( 5 );
point (loc.x, loc.y, loc.z);
}
}
}