﻿ Processing « Coding, Sounds and Colors | A blog about algorithmic experiments in music and visual art. Sort of.

## Floating

posted by on 2013.05.11, under Processing
11:

Here’s a code which uses the idea of a “particle system”, i.e. a system which consists of a number of objects (which may vary with time) interacting among themselves, and with their environment. In this case, I have just used 3 particles which are subject to a centripetal force, and that repulse each other. Once you set up a basic interaction system, you can think about how to represent at each time the data produced by the system: in this case I have choose to draw for each particle a rotating line, whose properties, like its transparency, depend on some other parameters (distance from the center).
The interesting thing about using particle systems to make visual art is that in principle one is dealing with a “parameter space”, which doesn’t need to be mapped via the intuition which it was built in the first place with: in other words, there’s no need to represent the objects as actual particles (with positions, velocity, etc.), but one can consider the system as a machine that produces data, to be then represented in the way one artistically feels like. The advantage of using a particle system is that all these data will evolve according to some dynamical evolution which is (in general) not totally random, giving rise to patterns in the final artistic outcome.

int d=3;
Agent[] point=new Agent[d];
void setup(){
randomSeed(90);
size(500,500);
background(255);
for (int i=0;i<d;i++)
{
point[i]= new Agent(random(10,width),random(10,height),random(-1.5,1.5),random(-1.5,1.5));
};
};

void draw(){
for (int i=0;i<d;i++)
{
point[i].resetrep();
for (int j=0;j<d;j++)
{
if (i!=j) point[i].interact(point[j]);
};
};
for (int i=0;i<d;i++)
{
point[i].applyForce();
point[i].move();
point[i].paint();
};
};

class Agent{
PVector loc;
PVector vel;
PVector rep;
float alpha;
int tran;
float l;
float mag=0;

Agent(float _locx,float _locy, float _velx,float _vely){
loc=new PVector(_locx,_locy);
vel=new PVector(_velx,_vely);
rep=new PVector(0,0);
alpha=0;
tran=10;
l=random(20,50);
};

void resetrep(){
rep.x=0;
rep.y=0;
}

void interact(Agent other){
PVector reps=PVector.sub(loc,other.loc);
float m=reps.mag();
reps.normalize();
reps.mult(3/(m+0.0001));
};

void applyForce(){
PVector force=new PVector(random(-0.02,0.02),random(-0.02,0.02));
force.limit(1);
vel.limit(6);
};

void move(){
alpha+=2;
};

void paint(){
pushMatrix();
translate(loc.x,loc.y);
popMatrix();
};
};

Here’s a screenshot of the animation after a few seconds.

## On Cellular Automata

posted by on 2013.04.27, under Processing
27:

The last few days for me have been all about Cellular Automata, due mostly to a long term obsession of mine: complex behaviours of systems at large scale which do not require hierarchy or centralized organization to exist, but “emerge” from local behaviours of the system’s constituents. (Here‘s a TEdTalk by Steven Strogatz on large scale complexity in birds flocks and also inanimate objects.)
What is then a cellular automaton? You can see it as a conceptual building block: it’s an entity capable of being in a given (finite) number of states, and of interacting with its close “neighbours” according to certain very simple rules. The result of the interaction will in general cause a change in the state of the given automaton, and consequently an (discrete time) evolution of the system. The neighbours, once defined, don’t change during the evolution of the system, so, in other words, a cellular automaton is not capable of moving, but it is fixed to a grid, the most common being 1 and 2 dimensional. Moreover, the grid is actually positioned on a circle or torus, so to avoid having to impose boundary conditions, i.e. a modification of the rules for the automata living on the edge of the grid.
Some systems of rules, like Conway’s Game Of Life, which is the one I have used in the code below, and probably the most famous one, exhibit some incredibly interesting global behaviours and patterns.
The code below tries to use Cellular Automata for artistic purposes: some of the parameters of the automaton, as for instance its state, the number of “alive” neighbours, and the number of frames it has been alive, have been used to draw geometric shapes (squares, in this case) of different dimensions, colors,etc. .
Notice that the set of rules is slightly perturbed, so, given an initial state, its evolution is not completely deterministic.

int dim;
Cell[][] cells;
int cellsize=20;
int r=0;
int acc=0;

void setup(){
size(600,600);
background(0);
dim=floor(width/(cellsize/2));
cells=new Cell[dim][dim];
for (int i=0;i<dim;i++)
{
for (int j=0;j<dim;j++)
{
if(random(0,1)<0.7) {r=0;} else {r=1;};
cells[i][j]=new Cell(cellsize/2+i*cellsize/2,cellsize/2+j*cellsize/2,r);
};
};

for (int i=0;i<dim;i++)
{
for (int j=0;j<dim;j++)
{
cells[i][j].paint();
};
};

};

void draw(){
background(0);
nextstateCalc();
for (int i=0;i<dim;i++)
{
for (int j=0;j<dim;j++)
cells[i][j].paint();
};
perturb();
};
;

void nextstateCalc(){
for (int i=0;i<dim;i++)
{
for (int j=0;j<dim;j++)
{
int liveCount=0;

liveCount=cells[(i+dim) % dim][(j+1+dim) % dim].state+cells[(i+1+dim) % dim][(j+1+dim) % dim].state+cells[(i+1+dim) % dim][(j+dim) % dim].state+cells[(i+1+dim) % dim][(j-1+dim) % dim].state+
cells[(i+dim) % dim][(j-1+dim) % dim].state+cells[(i-1+dim) % dim][(j-1+dim) % dim].state+cells[(i-1+dim) % dim][(j+dim) % dim].state+cells[(i-1+dim) % dim][(j+1+dim) % dim].state;

if (cells[i][j].state==1){
cells[i][j].frameAlive++;
if (liveCount == 2 || liveCount == 3) {
cells[i][j].nextstate=1;
}
else {
cells[i][j].nextstate=0;
}
};

if (cells[i][j].state==0){
cells[i][j].frameAlive=0;
if (liveCount == 3) {
cells[i][j].nextstate=1;
};
cells[i][j].neigh=liveCount;
};
};
};

};

void perturb(){

if (random(0,1)<0.1+acc){
int a=int(random(0,dim));
int b=int(random(0,dim));
cells[a][b].state=0;
};
if (acc<=0.9) {
acc+=0.01;
}
else {
acc=0;
}
};

/* Define the cellular automaton class
*/
class Cell{
float x;
float y;
int state;
int nextstate;
int neigh=1;
int frameAlive=0;

Cell (float _x,float _y,int _alive){
x=_x;
y=_y;
state=_alive;
nextstate=_alive;
};

void paint(){
state=nextstate;
noStroke();
fill(150*(neigh),160*(neigh),110*(neigh),4*state);
pushMatrix();
translate(x,y);
rectMode(CENTER);
rect(0,0,3*(1+neigh*(frameAlive % 100)),3*(1+neigh*(frameAlive % 100)));
popMatrix();
};
};

Since the code needs to perform quite a certain load of computations, I have done a render of it, rather than having it running in real time. The music is not strictly speaking related to the animation, but I was inspired by the video to play some piano. 😉
Finally, the use of Cellular Automata was inspired by the reading of Matt Pearson’s very nice Generative Art: A Practical Guide (see also Daniel Shiffmann’s The Nature of Code). I will come back to this topic in a following post, talking about boids and autonomous agents, a somehow related concept. Also, I would like to apply Cellular Automata techniques to sound: I have some ideas, so stay tuned! 😉

## Databending in Processing

posted by on 2013.04.02, under Processing
02:

Here’s a code in Processing that explores a simple databending technique for images. You can read about Glitch Art , and then start endelessy debate with your friends if this is art or not.
Also, here you can find some interesting series of videos exploring a bit also the philosophy behind databending, glitching and malfunctions as a form of awareness of the technology we are sorrounded by. (A simple example would be how the spelling mistakes in a chat messaging system make you indeed aware of the chat medium itself).
Here’s the code

PImage img;

int width=600;
int height=600;
int x1;
int y1;
int x2;
int y2;
int sx=10;
int sy=100;
int iter=100;

void setup(){
size(width,height);

image(img,0,0);

for (int h=0;h<iter;h++)
{
sx=int(random(5,30));
sy=int(random(50,130));

x1=int(random(0,width-sx-1));
y1=int(random(0,height-sy-1));
x2=int(random(0,width-sx-1));
y2=int(random(0,height-sy-1));

for (int i=0; i<sx ;i++)
{
for (int j=0; j<sy;j++)
{
color temp=pixels[(x1+i)*width + (y1+j)];
pixels[(x2+i)*width + (y2+j)]=pixels[(x1+int(random(0,i)))*width+(y1+int(random(0,j)))];
pixels[(x1+i)*width + (y1+j)]=temp;
}}
updatePixels();
}
};

The idea is very simple: we “swap” pixel strips of random width and height from the starting image, starting from two random points, and add some jitter to it.
We then iterate the process starting from the last modified image: the parameter iter controls the number of iterations.
Starting from this image (it’s a small piece from an image taken from the web)

I have got this

Of course, you can do a lot more interesting things by accessing directly to the pixels of an image: you can rotate areas, alter colors, etc..
And in particular, you can make animations in Processing using the same principle.
I’ll try to explores this more in future posts, trying maybe also to clear my mind about deconstructivism in art and communications…

## Periodicity

posted by on 2013.03.01, under Processing
01:

Here’s a little code in Processing exploring periodic motion. The single balls have been assigned an angular velocity consisting of “even harmonics”, while the amplitude of the single oscillation is constant up to some tiny random factor, to make the whole animation look more “natural”.

int p=50;
float[] y= new float[p];
float[] r= new float[p+1];
float[] omega= new float[p+1];
float[] amp= new float[p+1];
float t=0;
float s=0;
float b=0;

void setup() {
for (int i=0;i<y.length;i++){
y[i]=i;
};
omega[0]=0;
for (int i=1;i<omega.length;i++){
omega[i]=i*2;
amp[i]=20+random(-1.0,1.0);
};
size(400, 400);
background(255);
}

void draw() {
background(255);

for (int i=0;i<r.length;i++){
r[i]=amp[i]*sin(omega[i]*t/2);
};
for (int i=0;i<y.length;i++){
stroke(50);
strokeWeight(3);
line(width/2+r[i],y[i]*600/p,width/2+r[i+1],y[i]*600/p+600/p);
fill(int(abs(map(sin(i*t/2),-1,1,-255,255))));
strokeWeight(1);
ellipse(width/2+r[i],y[i]*600/p,10,10);
} ;
t +=0.008;
b++;
};

Click below to start/stop the animation.

posted by on 2013.02.19, under Processing
19:

The “triadic” discrete time dynamical system, i.e. the one obtained by iterating the function f(x)=3*x mod 1, is a very interesting one, showing a chaotic behaviour for given values of the starting point. I have used a bidimensional version of this in the following code, done with Processing: the starting point is given by (0.12,2.13), and at each iteration you have a line connecting the point at step n with the one at step n-1.
The number of points per frame is dictated by the x-position of the mouse, while the y-position controls the transparency of the lines. The x-position also controls their thickness.

float x;
float y;
float a=0.12;
float b=2.13;
float points=500;
float s;
boolean paused=true;

void setup(){
size(400,400);
background(255);
}

void draw(){
if (!paused){
points=map(mouseX,0,width,0,500);
s=map(mouseY,0,height,0,100);
background(255);
translate(width/2,height/2);
stroke(0,int(s));
strokeWeight(int(points/100));
for (int i=0;i<points;i++){
x=(3*a) % 1;
y=(3*b) % 1;
line(map(a,0,1,-width/2,width/2),map(b,0,1,-height/2,height/2),map(x,0,1,-width/2,width/2),map(y,0,1,-height/2,height/2));
a=x;
b=y;
};
};
};

void mousePressed() {
// if paused == true make it false
if(paused) {
paused = false;
}
// otherwise make it true
else {
paused = true;
}
};

Click on the white square to start/stop the animation. 😉

## Hello World!

posted by on 2013.02.11, under Processing, Supercollider
11:

Here’s some cheers from the main programming environments I like to experiment with, namely Supercollider and Processing

“Hello World! Let’s make some noise”.postln

and

println(“Hello Word! Coloring pixels, anyone?”)