Tuesday 14 April 2009

Program code - Plasma Ball


float[]x = new float[12];
float[]y = new float[12];
float[][]xy = {x,y};

void setup(){
size(300,300);
}
void draw(){
background(10,3,255);
noStroke();
ellipse(width/2,height/2,width,height);
fill(0);
noStroke();
smooth();
frameRate(30);
follow();

}
//follow the mouse
void follow(){
for( int i =0 ; i<12; i++){
xy[0][0] = (width/2);
xy[1][0] = (height/2) ;
xy[0][i] = mouseX ;
xy[1][i] = mouseY;

//Keep within the boundaries of the ellipse
float inside = ((sqrt( ((mouseX-width/2)*(mouseX-width/2)) + ((mouseY-height/2)*(mouseY-height/2)) ))-width/2);
if(inside >1) {
xy[0][0] = (width/2);
xy[1][0] = (height/2);
check(i);
}
}
makeRect(xy);

}
//check that points stay in circle
void check(int i){
float rndNumX1 = random(width);
float rndNumY1 = random(height);
float xStart = width/2;
float yStart = height/2;
float radius = width/2;
if((sqrt(((rndNumX1-xStart)*(rndNumX1-xStart))+((rndNumY1-yStart)*(rndNumY1-yStart))) - radius) < 1) {
xy[0][i] = rndNumX1;
xy[1][i] = rndNumY1;
}
else
{
while(i<xy[0].length){
check(i);
i++;
}
}
}
void makeRect(float[][]pts){
float a = random (255);
float b = random (255);
float c = random (255);
stroke(a,b,c);
smooth();
int steps = 100;
float scribVal = 2.0;
for (int i = 0; i<pts[0].length; i++){
strokeWeight(.7);
beginShape(POINTS);
vertex(pts[0][i], pts[1][i]);
strokeWeight(1.2);
if (i >0){
scribble(pts[0][i], pts[1][i], pts[0][i-1], pts[1][i-1], steps, scribVal*2);
}
}
endShape();
}
void scribble(float x1, float y1, float x2, float y2, int steps, float scribVal){
float xStep = (x2-x1)/steps;
float yStep = (y2-y1)/steps;
for (int i=0; i<steps; i++){
float xStart = width/2;
float yStart = height/2;
float radius = width/2;
if(i<steps-1){
float scribA = (x1+=xStep+random(-scribVal, scribVal));
float scribB = (y1+= yStep+random(-scribVal, scribVal));
if ( (sqrt( ((scribA-xStart)*(scribA-xStart))+ ((scribB-yStart)*(scribB-yStart)))- radius) < 1) {
beginShape();
vertex(x1, y1);
vertex(scribA ,scribB);
endShape();
}
}
}
}

3 comments:

Anonymous said...

I have just realised the your blog refers to vertex functions parts 1 - 4, which might be a bit confusing as there are several chunks dealing with vertex functions. I have for example started to write chunk42, which describes the line strip mode of vertex functions, I think yours is the first, but if you have parts 1 - 4 then numbering etc might get a bit confusing?

Ian Welch said...

Parts 1-4 which I have posted are just the breakdown of 500 ish words at a time of my main post, is this what you mean?

Anonymous said...

I guess thats what I meant, I had visions of the sections becoming sections in the book, I guess it will be up to Darrel to sort that out. It's just I did a bit of double take when I saw the section numbering in your blog vis a vis Darrels book chunks originally. I soon realised that was not the case.