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 › rotate and translate / understanding push pop
Page Index Toggle Pages: 1
rotate and translate / understanding push pop ? (Read 458 times)
rotate and translate / understanding push pop ?
Apr 13th, 2008, 10:56pm
 
Hello !

I'm trying to get my head round the push pop method for placing stuff on screen. And it seems great for certain type of functions but I've run into a simple problem that you guys can help me with. I'm sure it will enable me to better understand this technique.

here's what I'm getting to :
http://farm3.static.flickr.com/2411/2410704467_9cbe7be48f_o.png

and this is the code :

size(300, 300);
randomSeed(4);
smooth();
background(255);
PFont tiza = loadFont("Tiza-90.vlw");
textAlign(CENTER);
translate(height/2, width/2);
textFont(tiza, 45);

float[] angle = {0, -PI/2, -PI/4};

for (int i = 0; i< 100; i++){
 fill(0);
 stroke(0);
 pushMatrix();

 rotate(angle[round(random(2))]);
 translate(random(-width, width), random(-width, width));
 scale(random(0.2, 1));
 text("black",0,0);
 popMatrix();
}
//save("test.png");



My problem is that I translate after the rotation. So its not simple to get words inside the canvas after a rotation.
My simple solution is this example is to increase the random translate out of the canvas size to be sure that all the canvas is filled with words. Is there a more elegant way to insure that after a  rotation a random translate stays in the canvas ?

Thx
Re: rotate and translate / understanding push pop
Reply #1 - Apr 14th, 2008, 4:46pm
 
a picture or description detailing what you're aiming for might be useful. 8)

that said, you have a screen of size width and translate by width / 2 so your coords go from -width / 2 to +width / 2

BUT you're choosing your random position from -width to +width which is twice the size of the screen...
Re: rotate and translate / understanding push pop
Reply #2 - Apr 14th, 2008, 5:03pm
 
The idea is simply to place text at a random position / scale / rotation on the screen. There's a link to the image in my first post.

And my question is how to translate and stay in the screen if you've already rotated ?
I use a random between -width and width to insure that I fill the screen with the text (even after a rotation !!) Its a cheap solution.
So how can I do this ?


Re: rotate and translate / understanding push pop
Reply #3 - Apr 14th, 2008, 5:13pm
 
> I use a random between -width and width to insure that I fill the screen with the text (even after a rotation !!)

but that means you're picking points outside the screen a lot of the time.

try

 translate(random(-width / 2, width / 2), random(-width / 2, width / 2));

and see the difference
Re: rotate and translate / understanding push pop
Reply #4 - Apr 14th, 2008, 5:36pm
 
Thx koogs that solves half the problem Smiley

It was actually another error in my code !

But this won't work as soon as I use a rectangular screen size (instead of a square). Once a text will have been rotated 90 degrees its X translate will end up representing a a translation on the Y axis of the screen.

So is there a simple math trick to link the right translation after a rotation ? Or a different way to do it ?

I'm used to code where you simply move the object!


Re: rotate and translate / understanding push pop
Reply #5 - Apr 14th, 2008, 6:41pm
 
> But this won't work as soon as I use a rectangular screen size (instead of a square).

well, you'll need to use height and width instead of width and width in the translation but it should be ok.

> My problem is that I translate after the rotation

not a problem

when you rotate, you are rotating the text around its centre* BEFORE doing the translate (try commenting out the translate like i mentioned last time and you'll see. try using different colours for each bit of text as well, stop you getting a black blob in the middle). then you are translating the rotated text to its new position, which is a case of choosing from -width/2 to width/2 and from -height/2 to height/2

try it, it'll take 5 seconds.

(* actually, you aren't, you are rotating around the middle of the baseline of the text. change the textAlign to (CENTER, CENTER) and that'll improve things)
Page Index Toggle Pages: 1