We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › recall setup() or reseting the variables
Page Index Toggle Pages: 1
recall setup() or reseting the variables (Read 647 times)
recall setup() or reseting the variables
Jul 22nd, 2009, 7:06am
 
hey! i've been asked to make a generative visual for the background of a show and since my sketch is random it sometimes gets of screen so i would like to know a way that i can control it by recalling the setup() or reseting the variables
Re: recall setup() or reseting the variables
Reply #1 - Jul 22nd, 2009, 7:10am
 
For stuff like this I always just have an init() routine which is essentially a copy of setup(). Just have it initialize all your variables to their original states. Another option is to set up some boundaries so your random drawings can't draw off-screen.
Re: recall setup() or reseting the variables
Reply #2 - Jul 22nd, 2009, 7:33am
 
Modulo is really usefull in most of these cases
http://processing.org/reference/modulo.html
but its hard to tell without the code.
Re: recall setup() or reseting the variables
Reply #3 - Jul 22nd, 2009, 10:42am
 
Code:
Linhas l1 = new Linhas(0,0);
Linhas l2 = new Linhas(0,0);
Linhas l3 = new Linhas(0,0);
Linhas l4 = new Linhas(0,0);
Linhas l5 = new Linhas(0,0);
Linhas l6 = new Linhas(0,0);
Linhas l7 = new Linhas(0,0);
Formas f1 = new Formas(0,0);
Formas f2 = new Formas(0,0);
Formas f3 = new Formas(0,0);
Formas f4 = new Formas(0,0);
PerlinBolas b1 = new PerlinBolas(0,0);
PerlinBolas b2 = new PerlinBolas(0,0);
PerlinBolas b3 = new PerlinBolas(0,0);
PerlinBolas b4 = new PerlinBolas(0,0);
PerlinBolas b5 = new PerlinBolas(0,0);
PerlinBolas b6 = new PerlinBolas(0,0);
PerlinBolas b7 = new PerlinBolas(0,0);

void setup(){
size(1024,768);
smooth();
background(0);



}

void draw(){
fill(0,1);
rect(-1,-1,width,height);
if(key=='q'){

l1.desenhar();
l2.desenhar();
l3.desenhar();
l4.desenhar();
l5.desenhar();
l6.desenhar();
l7.desenhar();
}
if(key=='w'){

f1.desenhar();
f2.desenhar();
f3.desenhar();
f4.desenhar();
}
if(key=='e'){
b1.desenhar();
b2.desenhar();
b3.desenhar();
b4.desenhar();
b5.desenhar();
b6.desenhar();
b7.desenhar();
}

}


Code:
class Formas{

float x;
float y;
float x2;
float y2;

float px;
float py;
float px2;
float py2 ;
float vel;
float dir;
float dir2;

Formas(float x,float y){
x= y=px=py=200;
x2=y2=px2=py2=300;


vel = random(50,100);
dir = random(360);
dir2 = random(360);

}
void desenhar(){
noStroke();
fill(255,0,0,40);
px = x;
py = y;
px2 = x2;
py2 = y2;
x+= cos(radians(dir))*vel;
y+= sin(radians(dir))*vel;
x2+= cos(radians(dir2))*vel;
y2+= sin(radians(dir2))*vel;
stroke(0);
beginShape();
vertex(px,py);
vertex(x,y);
vertex(px2,py2);
vertex(x2,y2);
endShape(CLOSE);
//line(px,py,x,y);
//line(px2,py2,x2,y2);


dir+=random(-30,30);
dir2+=random(-30,30);
line(x,y,x2,y2);
}
}


Code:
class Linhas{

float x;
float y;
float x2;
float y2;

float px;
float py;
float px2;
float py2 ;
float vel;
float dir;
float dir2;

Linhas(float x,float y){
x= y=px=py=200;
x2=y2=px2=py2=300;


vel = random(10);
dir = random(360);
dir2 = random(360);

}
void desenhar(){
px = x;
py = y;
px2 = x2;
py2 = y2;
x+= cos(radians(dir))*vel;
y+= sin(radians(dir))*vel;
x2+= cos(radians(dir2))*vel;
y2+= sin(radians(dir2))*vel;
stroke(0,100,175);
line(px,py,x,y);
line(px2,py2,x2,y2);


dir+=random(-30,30);
dir2+=random(-30,30);
line(x,y,x2,y2);
}
}



Code:
  class PerlinBolas{
float r3 = random(100,200);
float g3 = random(100,200);
float b3 = random(100,200);
float a3 = random(100,200);



float xplus = 0.01;
float yplus = 0.01;

float xx = random(0, 0.05);
float yy = random(0, 10);

PerlinBolas(float x2,float y2){
}
void desenhar(){

float x = noise(xx)*width+random(10);
float y = noise(yy)*height+random(10);
xx += xplus;
yy += yplus;
fill(0,g3,b3,a3);

ellipse(x, y, 30, 30);

}
}


here is the code! what i need is to have it like it was started again!
Re: recall setup() or reseting the variables
Reply #4 - Jul 22nd, 2009, 11:39am
 
Wow, you know, arrays and for loops are nice and easy to learn... :-P

I haven't tried your code, but if you put your init code (before setup()) in a function, call it from setup(), then call it when you need to restart (along with a background() call), it might work.
Re: recall setup() or reseting the variables
Reply #5 - Jul 22nd, 2009, 2:19pm
 
so i want to call the init code on the classes i think! as for the arrays and loops... one step at the time Tongue i'll get there soon i promise
Re: recall setup() or reseting the variables
Reply #6 - Jul 22nd, 2009, 6:12pm
 
Exactly! You'll want the class to have it's own init() function so that each object can init() itself. But to do this efficiently, you'll want to use for() loops. So you'll have an array storing all your Linhas objects, and you'd do something like:

Code:

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


And those three lines would reset all your variables! Easy, huh? Read up on arrays and for() loops and you'll get the hang of it! Come back here if you need help or have questions.    
Page Index Toggle Pages: 1