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.
Page Index Toggle Pages: 1
draw (Read 2330 times)
draw
Dec 25th, 2009, 11:15am
 
Hi, Im in a mess with the draw function. I thought it was a loop that dont finish each step till all the instruction are finished, but it seems to me that it doesnt work like that, because I´ve tried to put a lot of instructions in it and the program don´t draw instruction by instruction but it show a clear window, after a while  a few drawins and after another while all the drawing complete.

This is the program

Code:
void Linea(float x, float y, float longitud,float angulo,float yema,float incrementoangulo,float veces){
float finalx=x+longitud*cos(angulo);
float finaly=y+longitud*sin(angulo);
float nuevox=x+longitud*cos(angulo)*yema;
float nuevoy=y+longitud*sin(angulo)*yema;;
float nuevalongitud;
float nuevoangulo;
delay(5);
line(x,y,finalx,finaly);
nuevalongitud=longitud*0.7;
if (veces>0){
Linea(nuevox,nuevoy,nuevalongitud,angulo+incrementoangulo,yema,incrementoangulo,veces-1);
Linea(nuevox,nuevoy,nuevalongitud,angulo-incrementoangulo,yema,-incrementoangulo,veces-1);
}
}
void setup(){
size(1000, 1000);
background(255);
}
void draw(){
background(255);
Linea (500,700,200,-PI/2,1,PI/5,6);
}
Re: draw
Reply #1 - Dec 25th, 2009, 12:09pm
 
The content of draw() is executed 60 times a second.

So :
background(255); will clear the background
Linea (500,700,200,-PI/2,1,PI/5,6); will call the Linea() method which draws some stuff

And that's what you're going to do 60 times a second until you press STOP.

You shouldn't call delay(5). If you want to wait for some time between two frames, you should better set the frameRate.
Re: draw
Reply #2 - Dec 25th, 2009, 1:57pm
 
Thank you, the framerate determines the time between two executions of the draw code, but I want to draw a fractal that have a whole lot of lines, and I want the program to wait a little bit between two lines, so they are not different frames, but to draw a frame slowly.

I want to draw a fractal in a way that anybody who sees the animation could understand how the fractal is created, so I want it to be drawn slowly.
Re: draw
Reply #3 - Dec 25th, 2009, 2:19pm
 
You cannot draw a frame slowly. Everything is drawn off-screen and then pasted to the window. You cannot see the intermediate steps, which is great : this has the enormous advantage of avoiding flickering and other horrible artefacts.

If you want to draw something in several steps, you'll have to manage another way.

You have two choices, actually.

Either choose not to clear the background, so when you draw something, it will sort of add a new layer on each frame :

Quote:
void setup() {
  frameRate(2);
  background(255);
}

void draw() {
  rect(frameCount*10%width, frameCount%width, 8, 8);
}


Or you can clear everything on each frame, and immediately draw everything until a certain step.

Quote:
void setup() {
  frameRate(2);
}

void draw() {
  background(255);
  for (int i = 1; i < frameCount; i++) {
    rect(i*10%width, i%width, 8, 8);
  }
}
Re: draw
Reply #4 - Dec 26th, 2009, 12:48am
 
Maybe I could find a solution later, but now I have no idea of how to do it, because a fractal is a very complicated drawing, unless it has a very simple algorithm. But it works wit recursive functions, so it is difficult to make it step by step and save the information for the next step. See the code in previous entries. I don't know how to do it.
Re: draw
Reply #5 - Dec 26th, 2009, 1:54am
 
Here is an example of how to go deeper in recursion on each frame :

Quote:
int counter = 0;

void recursive(int step) {
  
  // exit condition
  if (step > 0) {
    
    step--;
    
    // do your drawing here
    
    pushMatrix();
    rotate(0.2);
    line(0, 0, 0, -10);
    translate(0, -10);
    recursive(step);
    popMatrix();
    
    pushMatrix();
    rotate(-0.2);
    line(0, 0, 0, -10);
    translate(0, -10);
    recursive(step);
    popMatrix();
    
  }
  
}

void setup() {
  frameRate(1);
}

void draw() {
  background(255);
  counter++;
  translate(width/2, height);
  recursive(counter);
}
Page Index Toggle Pages: 1