7.25.2010

Processing Sketches - Spew

So hit this link first........http://processing.org/learning/........and work through every tutorial if you want to get up to speed quickly with the Processing programming language. Each one is very concisely written and each has well developed example code to help through each lesson.

So here is my example code for the sketch I have called Spew. It is basically a mouse input sketch that creates 'fireworks' from your cursor. It was my test for understanding mouse cursor following as well as simple class writing (you tend to forget those things when you dont use them on a daily basis).





/**************************************************
Spewing Cursor Test v. 3
Kyle Kuepker
July 21, 2010

The idea is to have the mouse move a cursor and have
it spew pixels in different directions like a fountain

**************************************************/
int array_max = 60;
int firework_count = 0;
int spew_length_max = 200;
int spew_length_min = 50;
int fade_speed = 7;
float angle;
int MouseX;
int MouseY;
int spew_length;
PImage reticle;
Firework f_array[] = new Firework[array_max];

void setup()
{
size(400, 400);
background(25);
reticle = createImage(15, 15, ALPHA);
formReticleSquare();
noCursor();
smooth();
stroke(255, 150);
strokeWeight(3);
strokeCap(ROUND);
fill(255, 150);
for(int i = 0; i < array_max; i++)
{
f_array[i] = new Firework(0,0,0,0);
}

}

void draw()
{
background(25);
image(reticle, mouseX-8, mouseY-8);

if(mousePressed)
{
spew_length = int(random(spew_length_min, spew_length_max));
angle = random(0, 2*PI);
f_array[firework_count] = new Firework(mouseX, mouseY, spew_length, angle);
f_array[firework_count].f_draw();
firework_count++;
println(firework_count);
if(firework_count > 59)
{
firework_count = 0;
println("RESET");
}
}

for(int i = 0; i < f_array.length; i++)
{
f_array[i].update();
}
}

/*****************************
A simple Firework class
******************************/

class Firework
{
int startX, startY, f_length, endX, endY;
float f_angle;

Firework(int X1, int Y1, int LENGTH, float ANGLE)
{
startX = X1;
startY = Y1;
f_length = LENGTH;
f_angle = ANGLE;
endX = X1 + int(LENGTH * cos(ANGLE));
endY = Y1 + int(LENGTH * sin(ANGLE));
}

void f_draw()
{
line(startX, startY, endX, endY);
}

void update()
{
f_length = f_length - fade_speed;

if(f_length < 1)
noStroke();
else
stroke(255, 150);

startX = endX - int((f_length) * cos(f_angle));
startY = endY - int((f_length) * sin(f_angle));
line(startX, startY, endX, endY);
}
}

/*****************************
A simple reticle to replace the mouse cursor
This creates a square cursor....boring....i know
******************************/

void formReticleSquare()
{
for(int i=0; i < reticle.pixels.length; i++)
{
reticle.pixels[i] = color(255, 200);
}
}

No comments:

Post a Comment