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 › scale(), transform(), flipped fonts, and objects
Page Index Toggle Pages: 1
scale(), transform(), flipped fonts, and objects (Read 394 times)
scale(), transform(), flipped fonts, and objects
Oct 11th, 2008, 8:32am
 
How can an object encapsulate coordinate transformations (such as scale and translate) so they are separate from the environment?  An object should be able to request that an environment be "unscaled" or "untransformed," should it not?  

Here's my problem: I'm using scale and transform a lot, but I also want to have my objects label themselves without the text being backward, upsidedown, or otherwise.  How can my objects request that the environment be temporarily unscaled (so my fonts don't appear upside down when I use scale(1,-1) to get a more mathematically conventional coordinate system)?  I'm relatively familiar with how to use the matrix stack, but there's no way to call pushMatrix(), then resetMatrix(), then popMatrix().  Are there CoordinateManagers or something behind the scene?  

Here's a small example of the problem:

/////////////example problem////////////
LabelBox b1;

void setup()
{
 size( 200, 200 );
 background( 255 );
 noLoop();
 b1 = new LabelBox( 50, 50, 50, 50, 1 );
}

void draw()
{
 b1.paint();
 // now with the coordinates translated and scaled
 // so as to be like a graphing calculator, with
 // with the positive y on top, the negative y on
 // bottom, and the origin in the middle
 translate( width/2, height/2 );
 scale( 1, -1 );
 b1.paint(); // notice how font is upside down
}

// example class that would need to be able
// to know if scale had been called
class LabelBox
{
 PFont font;
 final int x, y, w, h, boxNum;
 
 LabelBox( int x, int y, int w, int h, int boxNum  )
 {
   this.x = x;
   this.y = y;
   this.w = w;
   this.h = h;
   this.boxNum = boxNum;
   font = createFont( "Arial", 16 );
 }
 
 void paint()
 {
   textFont( font );
   textAlign( CENTER, CENTER );
   rectMode( CENTER );
   noFill();
   rect( x, y, w, h );
   fill( 0 );
   text( "Rect " + boxNum, x, y );
 }
}
Re: scale(), transform(), flipped fonts, and objec
Reply #1 - Oct 11th, 2008, 9:16am
 
You might be interested by this thread: store transformation matrices.
Re: scale(), transform(), flipped fonts, and objec
Reply #2 - Oct 11th, 2008, 2:25pm
 
In that case you should be using:

pushMatrix()
(draw your shape here)
popMatrix()
(draw your text label here)

Calling resetMatrix() inside push/pop will simply nuke your camera setting. So if you really want to do the resetMatrix(), just call camera() after the reset so that it's back to normal. But that's overkill--a simple push and pop will do the trick.
Re: scale(), transform(), flipped fonts, and objec
Reply #3 - Oct 12th, 2008, 1:59am
 
@ PhiLho - Thanks for pointing out that thread.  Storing and retrieving transformation matrices (matrix hash table?)is exactly what I want to do, except in this case, I want to store the current transformation matrix, and retrieve the default matrix without erasing the current transformation matrix.  Fry said at the end of that thread that 0149 would have a getMatrix, so maybe I'll wait around for that.  

@ Fry - Thanks for your suggestion.  I tried to implement it, and encountered some more problems (both examples are available at links below via OpenProcessing).  What follows is a more detailed explanation of the context of this problem.  A while back, I made a class TickedLine that represented a line with tick marks that marked the distances from a starting point.  You can see an example of it and the source online at OpenProcessing( http://www.openprocessing.org/visuals/?visualID=521 ).  Now, some students of mine are making a graphing calculator in processing, and I'd like to optimize the TickedLine class to encapsulate the details about drawing tick marks on the axis, so they can concentrate on writing functions for the graphing calculator.  My labels need to know about where the coordinates in the graphing calculator are - I can't popMatrix before printing them, because they'll label the wrong points.  I could hack around this to get the right behavior, but I don't want to deal with the added complication of explaining to my students how to reproduce my hack to get the right behavior.  I also posted an example of the problem at open processing (link below ).

Does this mean that to encapsulate coordinate transformations we should  to require an object's environment to register desired transformations with an object and have the object perform the translations?  Is there no way to build an object that can query the state of its environment's existing transformations, and compensate for them?

Link 1 illustrates the TickedLine class and it's original behavior:
http://www.openprocessing.org/visuals/?visualID=521

Link 2 illustrates the problem of using TickedLine to label a coordinate system:
http://www.openprocessing.org/visuals/?visualID=522
Page Index Toggle Pages: 1