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 › Locked image ratio
Page Index Toggle Pages: 1
Locked image ratio ? (Read 534 times)
Locked image ratio ?
Nov 21st, 2006, 12:09am
 
Hi,

I've been playing around with this raindrop effect.

http://bodytag.org/tp02/

Then i realized that it only support 1:1 ratio (e.g 300x300 pixels)

I tried to modify it to support 4:3 (640x480, 800x600...) without success.

I believe (but not sure) that this has something to do with the float/int thing.

Quote:
int SEG = 2;
int WIDTH=300; // 640
int HEIGHT=300; // 480
int WH = WIDTH*HEIGHT;

int WIDTHSEG = WIDTH/SEG;
int HEIGHTSEG = HEIGHT/SEG;
int WHSEG = WIDTHSEG*HEIGHTSEG;

float bump[] = new float[WHSEG];
float vel[] = new float[WHSEG];
float t[] = new float[WHSEG];


Any idea ?
Re: Locked image ratio ?
Reply #1 - Nov 23rd, 2006, 1:24am
 
After investigations, i've found what's going wrong.

the scene.pixels[] array is getting refreshed on booth x and y at the same pixel's increment.

I've found out by using printnl(x + " " + y)

Quote:
x y
0 0
1 1
2 2
3 3
4 4
5 5
6 6
7 7
8 8
9 9
.  .
.  .
.  .
639 479


This is wrong for an image with a 1:1.33 ratio.

I'm trying now to modify this part of the code to float, to maintain the proper pixel's refresh speed.

Quote:
void draw() {
 int x, y, u, bu;
 int newpix;
 float mX, mY, du;
 newpix = 0;
 u = 0;
 bu= 0;
 mX = 0;
 mY = 0;
 
for(int i = WIDTHSEG+1; i < WHSEG-WIDTHSEG-1; i++) {
   bump[i] = (vel[i-1]
             + vel[i+1]
             + vel[i+WIDTHSEG]
             + vel[i-WIDTHSEG]
             + vel[i-WIDTHSEG-1]/1
             + vel[i-WIDTHSEG+1]/2
             + vel[i+WIDTHSEG-1]/2
             + vel[i+WIDTHSEG+1]/1)/4 - bump[i];
   
   bump[i] *= 0.90;
   }
 
 for(int i = 0; i < WHSEG; i++) {
   t[i] = vel[i];
   vel[i] = bump[i];
   bump[i] = t[i];
   }
 
 for(int i = 1; i < WH; i++)
 {
   x = i%WIDTH; // x should be equal to 1.3333... when y is 1
   y = i/HEIGHT; // y should be equal to 1 when x is 1.3333...
//  println(x + " " + y );
       newpix = 0;
  if(x > SEG && x < WIDTH-SEG && y > SEG && y < HEIGHT-SEG)
 {
     bu = (int)(x/SEG+((y/SEG)*WIDTHSEG));
     mX = (float)((bump[bu] - bump[bu-1]) + (bump[bu+1] - bump[bu]))*32;
     mY = (float)((bump[bu] - bump[bu-WIDTHSEG]) + (bump[bu+WIDTHSEG] - bump[bu]))/32;
     mX = constrain(mX,-8,1);
     mY = constrain(mY,-8,1);
     u = (int)(x+mX+(y+mY)*WIDTH);
    if(u < WH && u > 0)
    {
       newpix=scene.pixels[u];
      }
     }
   loadPixels();
   pixels[i] = newpix;
   updatePixels();
   }


Any help is warmly welcome ;)

Studying code and trying to understand it, my new hobby now :P

(Processing is really addictive when you dive into..)
Re: Locked image ratio ?
Reply #2 - Nov 23rd, 2006, 2:22am
 
I've got it to work !

Now it's image ratio independent.

I had to switch the "x and y" int values to float.

Quote:
void draw() {
 int u, bu; // REMOVED x, y
 int newpix;
 float  x, y, mX, mY, du; // ADDED x, y
 newpix = 0;
 u = 0;
 bu= 0;
 mX = 0;
 mY = 0;
 
for(int i = WIDTHSEG+1; i < WHSEG-WIDTHSEG-1; i++) {
   bump[i] = (vel[i-1]
             + vel[i+1]
             + vel[i+WIDTHSEG]
             + vel[i-WIDTHSEG]
             + vel[i-WIDTHSEG-1]/1
             + vel[i-WIDTHSEG+1]/2
             + vel[i+WIDTHSEG-1]/2
             + vel[i+WIDTHSEG+1]/1)/4 - bump[i];
   
   bump[i] *= 0.90;
   }
 
 for(int i = 0; i < WHSEG; i++) {
   t[i] = vel[i];
   vel[i] = bump[i];
   bump[i] = t[i];
   }
 
 for(int i = 0; i < WH; i++)   // REPLACED 1 by 0
 {
   x = i%WIDTH;
   y = i/WIDTH;
// println(x + " " + y );
       newpix = 0;
  if(x > SEG && x < WIDTH-SEG && y > SEG && y < HEIGHT-SEG)
 {
     bu = (int)(x/SEG+((y/SEG)*WIDTHSEG));
     mX = (float)((bump[bu] - bump[bu-1]) + (bump[bu+1] - bump[bu]))*32;
     mY = (float)((bump[bu] - bump[bu-WIDTHSEG]) + (bump[bu+WIDTHSEG] - bump[bu]))/32;
     mX = constrain(mX,-8,1);
     mY = constrain(mY,-8,1);
     u = (int)(x+mX+(y+mY)*WIDTH);
    if(u < WH && u > 0)
    {
       newpix=scene.pixels[u];
      }
     }
   loadPixels();
   pixels[i] = newpix;
   
   updatePixels();
   }
}



It's not yet perfect...
Page Index Toggle Pages: 1