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 › Relationship of Draw and Translate
Page Index Toggle Pages: 1
Relationship of Draw and Translate (Read 471 times)
Relationship of Draw and Translate
Sep 3rd, 2008, 2:59am
 
Hi folks,

I'm hoping that someone here can help with what I assume is a fundamental misunderstanding on my part of both the draw() method and the translate() method. From what I've read, the code inside draw() is supposed to execute repeatedly and that the the effects of the translate() are cumulative.

So here is my code:
Code:

int loopVal=0;
boolean togle = true;

void setup(){
size(800,600); background(255);
stroke(5,5,125); strokeWeight(2); smooth();
} // end setup

void draw() {
translate(5,5); // just one dot on screen
for (int i=1; i< 10; i++) {
loopVal++;
// translate(12,12); results in 9 displaced dots
point(2,2);
} // end for loop
} // end draw

void mousePressed() {
println("loopVal="+loopVal);
if (!togle) {loop(); togle=true;}
else {noLoop(); togle=false;};
}


In the code I have only one translate() active at a time. When the translate(5,5) is not commented out, I get one dot on the screen. When the translate(12,12) is not commented out I get 9 dots on the screen. You will see that within the for loop I have a variable loopVal which I can display the value of with a mouse click. A click reveals that it has a value in the thousands - meaning that statement executed thousands of times. So why does a translate inside the loop stop after 9 dots and a translate outside the loop stop after 1?

Thanks for any help you can provide.

Jim
Re: Relationship of Draw and Translate
Reply #1 - Sep 3rd, 2008, 9:47am
 
It's because any transformations that result from translate, rotate, scale etc. are reset each time the draw() method is executed.

It is true though that these transformations are cumulative within the draw() method though, which is why your 9 dot example works.
Re: Relationship of Draw and Translate
Reply #2 - Sep 3rd, 2008, 2:27pm
 
Hi Jo,

Thank you for that. Rereading the documentation, I must not have been paying attention because it states quite clearly:

"If translate() is called within draw(), the transformation is reset when the loop begins again."

Moving the drawing code from within draw() to a method that is called by draw() calls yields the same results. So it looks like if I use draw() the only way to make the effects of translate() cumulative is to set up variables and use those to control the translate:

translate(x,y) instead of translate(5,5)

Thanks for your help Jo.

Jim
Re: Relationship of Draw and Translate
Reply #3 - Sep 3rd, 2008, 3:10pm
 
For a simple translation, you are probably best off doing what you say, storing the translation values x,y in variables.

For more complex transformations, you can keep a copy of the cumulative transformation by using the 'matrix stack' as in the following example.

Quote:
void setup()
{
  size(400,400);
  smooth();
  fill(180,100,100);
  pushMatrix();
}

void draw()
{
  popMatrix();
  background(255);
  translate(1,1);
  ellipse(10,10,10,10);
  pushMatrix();
}


The first pushMatrix(), inside setup(), stores a copy of the initial transformation (which does nothing at first). Every time the draw() method is called, the popMatrix() command retrieves the current transformation settings. Calls to translate(), rotate() etc. are then applied before storing the now modified transformation settings with the final call to pushMatrix().
Re: Relationship of Draw and Translate
Reply #4 - Sep 4th, 2008, 6:10am
 
Hi Jo,

Thanks for the additional examples. I created two versions: one using variables and the other using push/pop. Both work fine.

Page Index Toggle Pages: 1