jeudi 17 février 2011

Array wonders

We start with the ARRAYS from the section learning of the Processing tutorials.
We slightly modify the example:

// Example: 2D Array
size(400,400);
int cols = 5;
int rows = 5;
// Declare 2D array
int[][] myArray = new int[cols][rows];
int cellWidthHeight = 80;

int showMode = 0;//see later on
//
void setup(){
// Initialize 2D array values RANDOM
for (int i = 0; i < cols; i++) 

{
for (int j = 0; j < rows; j++)
{
myArray[i][j] = int(random(255));
}
}
}
//
void draw(){
// Draw points
for (int i = 0; i < cols; i++)
{
for (int j = 0; j < rows; j++)
{
fill(myArray[i][j]);
rect(i*cellWidthHeight, j*cellWidthHeight,cellWidthHeight,cellWidthHeight); point(i,j);
}
}
}


But we see only one representation, 2D, we would like to see more perspectives, for instance on a line, or on a circle.
For this we make showModes, and depending on a showmode the view should change.
We make the keys of the keyboard interactive and use keypressed to choose a mode:

void keyPressed() {
 if ( key == 1 ) showMode = 1;
 if ( key == 2 ) showMode = 2;
 
if ( key == 3 ) showMode = 3;
}

like that we can swith between "perspectives" of the same data.

void draw(){
if ( showMode == 1 ){
// Draw points
for (int i = 0; i < cols; i++)
{
for (int j = 0; j < rows; j++)
{
fill(myArray[i][j]);
rect(i*cellWidthHeight, j*cellWidthHeight,cellWidthHeight,cellWidthHeight); point(i,j);
}

}
if ( showMode == 2 ){
 //do something else
}

if ( showMode == 3 ){
 //do something else
}
}

Now we can play around in diffeent primitive visualisations of the data.
source code: http://www.contrechoc.com/crosslab/array.pde

Aucun commentaire:

Enregistrer un commentaire