FAQ
Cover
This is the archive Discourse for the Processing (ALPHA) software.
Please visit the new Processing forum for current information.

   Processing 1.0 _ALPHA_
   Programming Questions & Help
   Syntax
(Moderators: fry, REAS)
   explain behaviour
« Previous topic | Next topic »

Pages: 1 
   Author  Topic: explain behaviour  (Read 276 times)
roland_g


explain behaviour
« on: Jul 7th, 2004, 1:22pm »

While looking at one of the example programmes (applets?) by REAS, something happened that I can't quite understand.
 
I put the translate command/funtion in the draw_rect function and suddenly I got this pseudo 3d effect.
 
Now I don't quite understand why this is happening. Isn't the position of the center of each rectangle still the same? Shouldn't all the rectangles stay on top of each other?
 
thanks for your help
 
Code:

void setup()  
{  
  size(500, 500);  
  background(51);  
  noStroke();  
 rectMode(CENTER_RADIUS);  
}  
 
void loop()  
{  
background(#FFF000);
  //translate(mouseX-width/2,mouseY-height/2);
  draw_rect(width/2, height/2, 50, 10);
 
}  
 
void draw_rect(int xpos, int ypos, int size, int val)  
{  
  float grayvalues = 255/val;  
  float steps = size/val;  
  for(int i=0; i<val; i++) {  
  translate(mouseX-width/2,mouseY-height/2);
  fill(i*grayvalues);
  rect(xpos, ypos, size-i*steps, size-i*steps);  
  }  
}  
 
 
TomC

WWW
Re: explain behaviour
« Reply #1 on: Jul 7th, 2004, 1:50pm »

You put translate inside the for loop, so it's being translated 'val' times (10 in this case).  It will do what you expected if you move the translate to outside of the for loop.
 
Translations (and rotations and scales) persist until the end of loop, or until you call pop() (in which case they are restored to what they were when you last called push()).
 
Hope that helps.
 
roland_g


Re: explain behaviour
« Reply #2 on: Jul 11th, 2004, 12:37pm »

Thanks for the explanation tom. Although I am still not getting it (stupid me).
 
So when the first rect gets drawn within the loop
the second is getting drawn on the postion of the first then moved again and so on...wouldn't that mean that the second rect would already be outside of the window?
 
mouseX-width/2(250) + mouseX-width/2(250)
 
 
TomC

WWW
Re: explain behaviour
« Reply #3 on: Jul 11th, 2004, 5:43pm »

It depends where the mouse is, of course!  If the mouse is in the center of the screen, the offset is 0 for each iteration of the loop, so all the rects are on top of each other.  As the mouse moves away from the center, the offset gets bigger, but it's still quite small, so even the cumulative offset doesn't move all the rects off the screen until the mouse hits the edge.
 
Notice that without the mouse-dependent translate, all the rects are being drawn in the center.  The mouse translations are based on the distance from the center of the applet, so they are generally small.
 
The best advice I can give is to keep playing around with it until you work it out
 
Pages: 1 

« Previous topic | Next topic »