d4v3
YaBB Newbies
Offline
Posts: 15
mont-saint-michel
Re: swarm art
Reply #14 - Jul 17th , 2006, 11:38pm
This is what i've got so far: //Initialising global variables PImage picture; int location_x; int location_y; int generation=3; Swarm [] locust; //Setup of main variables void setup () { //Loading the image picture=loadImage("white.jpg"); framerate(1); size(500,500,P3D); background(199); camera(500, 300, (height/2.0) / tan(PI*60.0 / 360.0), width/2.0, height/2.0, 0, 0, 1, 0); //Initial co-ordinates for start of jumps location_x=100; location_y=100; //initialising generation locust = new Swarm[generation]; for (int i=0; i<generation; i++ ) { locust[i] = new Swarm(picture); } } //Recursive draw method void draw () { //clears the sketch every frame background(199); //starts the process for (int i=0; i<generation; i++ ){ locust[i].live(); println("ok "+i); } } //Swarm class class Swarm { PImage pic; //Constructor Swarm (PImage back) { pic=back; } //main method void live () { //Loading the image's pixels loadPixels(); //Initialising variables int random1=int(random(0,300)); int random2=int(random(0,300)); int random_red=int(random(-255,255)); int random_green=int(random(-255,255)); int random_blue=int(random(-255,255)); //Getting the random pixel int pixel=pic.pixels[random1+random2*pic.width]; int r=(pixel&0x00FF0000)>>16; int g=(pixel&0x0000FF00)>>8; int b=pixel&0x000000FF; //Setting destination int destination_x=random1+99; int destination_y=random2+99; //Setting stroke properties int randomstroke=int(random(0,255)); stroke(randomstroke); //Drawing the line line(location_x,location_y,0, destination_x,destination_y,0); location_x=destination_x; location_y=destination_y; //Changing the pixel's RGB r+=random_red; if(r<0) r=0; else if (r>255) r=255; g+=random_green; if(g<0) g=0; else if (g>255) g=255; b+=random_blue; if(b<0) b=0; else if (b>255) b=255; //Storing the change and updating the pixels pixel=(pixel&0xFF000000)+(r<<16)+(g<<8)+b; pic.pixels[random1+random2*pic.width]=pixel; pic.updatePixels(); //Assigning the image to a vertex beginShape(); noStroke(); texture(pic); vertex(100, 100, 0, 0); vertex(400, 100, 300, 0); vertex(400, 400, 300, 300); vertex(100, 400, 0, 300); endShape(); } } my problem is that this draws triplets of lines instead of three separate lines. if you change the generation variable to 1, it draws a line and then another starting from where the last one left off and so on. what i want to have happen as i increase the generation is multiples of this effect. instead what happens is that (for generation=3) it draws the three lines together and then the next three and so on... any ideas what i'm doing wrong?