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 & HelpPrograms › matrix scale
Page Index Toggle Pages: 1
matrix scale (Read 988 times)
matrix scale
Dec 1st, 2006, 5:16pm
 
Hello,

Maybe this problem has something with precision conversion but I'm not sure. I'm using keys UP and Down to increase matrix size but after some greater values, matrix is moved in upper left corner and scaling is not correct...

Someone has an idea?

thanks in advance,
best,
uki

here is the code:


int resolution=1;

void setup(){
 size(400, 400);
 background (0);
 noStroke();
 smooth();
 ellipseMode(CENTER);
}

void keyPressed() {
 if (key == CODED) {
   if (keyCode == UP) {
     resolution ++;
   }
   else if (keyCode == DOWN) {

     resolution--;
   }
   if(resolution < 0){
     resolution=0;

   }
 }
}
void draw(){
 background(0);

 float MatriceX[] = new float [resolution];
 float MatriceY[] = new float [resolution];
 
 for (int i=0;i<resolution; i++){
   for (int j=0; j<resolution; j++){
   
     float ratio=width/resolution;
     float ratio2 = ratio/2;
     
     MatriceX[i]=i*ratio;
     MatriceY[j]=j*ratio;

     ellipse(MatriceX[i]+ratio2,MatriceY[j]+ratio2,ratio2,ratio2);

   }
 }

}


Re: matrix scale
Reply #1 - Dec 2nd, 2006, 1:39am
 
A true classic...

Code:
float ratio=width/resolution; 


In this line you would expect the "ratio" to be a floating point number as expected (and it is) - however both other arguments ("width" and "resolution") of the equation are integers... so the division will still happen in the integer domain! The casting to "float" is only done with the already computed result.

You on the other hand need to force the virtual machine to execute the equation already in the "float" domain by casting one of the arguments to be interpreted as "float":

Code:
float ratio=(float)width/resolution; 


hth!
Re: matrix scale
Reply #2 - Dec 2nd, 2006, 3:17pm
 
yes, a true classic error...

thank you very much toxi!

best
uki
Page Index Toggle Pages: 1