\nCodon

System in Motion

Posted in examples, tutorials by pierre forissier on February 21, 2012

 

Growing vs Building. The idea of growing inhabitats  instead of building them still belongs to sorcerer’s apprentices but computing can give us glimpses. This is a visualization of previous cell[f] assembly code (based on Conway’s Game of Life).

The animation is coded in processing with bespoke camera angles.

Processing source code below:

import processing.opengl.*;

import toxi.geom.*;

int cols = 400;
int rows = 70;
float density = 0.97;
int MaxGeneration = 300;
int Generation =0;
int z = 0;
int[][] arrPt;

void setup() {
smooth();
size(1200, 800, OPENGL);
camera (50, 150, 300, 70, 30, 100, -1, 1, -1);
background(0);
randomGenesis(cols, rows);
}

void draw() {

Generation = Generation+1;

if ((Generation<MaxGeneration)&&(Generation>=0)) {
arrayCellTwister(arrPt, Generation);
arrPt=mutateArray(arrPt);
}

if (Generation==MaxGeneration) {
delay(500);
Generation = 0;
background(0);
randomGenesis(cols, rows);
}
}

int[][] randomGenesis(int intGridI, int intGridJ) {
arrPt = new int[intGridI+1][intGridJ+1];
for (int i = 0; i<=intGridI; i++) {
int[] arrJ;
arrJ = new int [intGridJ+1];
for (int j = 0; j<=intGridJ; j++) {
float rnd;
rnd=random(1);
if (rnd > density) {
arrJ[j]= 1;
}
else {
arrJ[j]= 0;
}
}
arrPt[i]=arrJ;
}
return arrPt;
}
void arrayCellTwister(int[][] arrPt, int z) {
for (int i =0 ;i<arrPt.length;i++) {
for (int j = 0; j<arrPt[i].length;j++) {
if (arrPt[i][j]==1) {
int[] arrCtr = {
i, j, z
};
float colorFac;
colorFac = map(z, 0, MaxGeneration, 0, 255);
noStroke();
fill(colorFac);
pushMatrix();
float swish = pow(z, 2)/10000;
rotateZ(PI/(z+swish));
translate(i+swish*10, j+swish*10, z);
box(1);
popMatrix();
}
}
}
}

int sumNeighbors(int[][] arrPt, int i, int j) {
int iOneUp = i+1;
int iOneDown = i-1;

int jOneUp=j+1;
int jOneDown=j-1;

if (i==(arrPt.length)-1) {
iOneUp=0;
}
if (i==0) {
iOneDown=(arrPt.length)-1;
}
if (j==(arrPt[i].length)-1) {
jOneUp=0;
}
if (j==0) {
jOneDown=(arrPt[i].length)-1;
}

int cellN1= arrPt[iOneDown][jOneDown];
int cellN2=arrPt[i][jOneDown];
int cellN3=arrPt[iOneUp][jOneDown];
int cellN4=arrPt[iOneUp][j];
int cellN5=arrPt[iOneUp][jOneUp];
int cellN6=arrPt[i][jOneUp];
int cellN7=arrPt[iOneDown][jOneUp];
int cellN8=arrPt[iOneDown][j];

int SumNcells = cellN1+cellN2+cellN3+cellN4+cellN5+cellN6+cellN7+cellN8;

return SumNcells;
}

int [][]mutateArray(int[][] arrPt) {
int newArrPt [][] = arrPt;
for (int i=0; i<arrPt.length;i++) {
for (int j=0; j<arrPt[i].length;j++) {
int dblNumberOfNeighbours = sumNeighbors(arrPt, i, j);
if ((arrPt[i][j]==0) && (dblNumberOfNeighbours==3)) {
newArrPt[i][j]=1;
}
else if ((arrPt[i][j]==1) && (dblNumberOfNeighbours>3)) {
newArrPt[i][j]=0;
}
}
}
return newArrPt;
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 25 other followers