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 › iteration issue...
Page Index Toggle Pages: 1
iteration issue... (Read 1201 times)
iteration issue...
May 31st, 2010, 9:10am
 
ho can I rewrite my code in order to shorten it?

Code:
int numFrames = 200;  // The number of frames in the animation
int frame = 0;
PImage[] images = new PImage[numFrames];

void setup()
{
 size(720, 384);
 frameRate(25);

 for(int i=1; i<numFrames; i++) {
   String imageName = "/data/Videodrome.1983.by.David.Cronenberg.DVDRiP.ITA.DXL-R " + nf(i, 3) +".jpg";
   images[i] = loadImage(imageName);
 }
}


void draw() {

 frame = (frame+1);
 
 if (frame == numFrames) {
   frame = 1;
 }
 image(images[frame], 0, 0, 72, 38.4);

 if (frame +10 == numFrames) {
   frame = 1;
 }
 image(images[frame+10], 72, 0, 72, 38.4);

 if (frame +20 == numFrames) {
   frame = 1;
 }
 image(images[frame+20], 144, 0, 72, 38.4);
 
   if (frame +30 == numFrames) {
   frame = 1;
 }
 image(images[frame+30], 216, 0, 72, 38.4);
 
   if (frame +40 == numFrames) {
   frame = 1;
 }
 image(images[frame+40], 288, 0, 72, 38.4);
 
if (frame +50 == numFrames) {
   frame = 1;
 }
 image(images[frame+50], 360, 0, 72, 38.4);

if (frame +60 == numFrames) {
   frame = 1;
 }
 image(images[frame+60], 432, 0, 72, 38.4);
 
 /* etc.... for the width*/
 
 if (frame +110 == numFrames) {
   frame = 1;
 }
 image(images[frame+110], 0, 38.4, 72, 38.4);
 
   if (frame +120 == numFrames) {
   frame = 1;
 }
 image(images[frame+120], 72, 38.4, 72, 38.4);
 
 /* and so on.........*/
}



my goal is to obtain a mosaic of jpg that simulate a motion on all over the screen....
Re: iteration issue...
Reply #1 - May 31st, 2010, 9:32am
 
It would help if you could explain a little more about the effect you're trying to achieve; it's not really obvious from the code what you're trying to do.

It looks like you're loading frames in an animation and playing the animation in tiles across the screen at different starting points.

What's confusing to me is the lines like
Code:
if (frame +120 == numFrames) {
frame = 1;
}

Are you trying to use these just to have each animation loop?  Because what's going to happen here is that as soon as you run through 10 frames, the 20th animation, which is 190 frames ahead of the first one, is going to trigger a reset of frame to 1, which is going to restart all of the animations.  

Is that what you want?  I think it probably isn't... but this is your project of course, so maybe you know what you're doing and that is what you want!

So, it would be helpful if you could describe the effect you want to achieve.  I don't think it'll be hard to explain how to code it, but first you need to explain what you want. Wink

Re: iteration issue...
Reply #2 - May 31st, 2010, 9:39am
 
You're right..... I only use this to obtain a loop for each animation... but it goes wrong....

i want to obtain something like this:

http://www.shiffman.net/2008/09/30/tyneside-cinema-launch/
Re: iteration issue...
Reply #3 - May 31st, 2010, 10:22am
 
All right, that's what I thought.  Your old setup() method looks fine, so keep that and the other variables you define above it.  So here are some tips for the rest:

1.  If you have numbers you re-use frequently, it's better to define variables for them and then use the variables in your code.  It makes it easier to change them later if you need to, and it also makes it easier to understand your code if you have variable names in it instead of "magic numbers":

Code:

int imW = 72; // Image width
int imH = 38.4; // Image height

int numImagesAcross = 10;
int numImagesDown = 10;


2.  Processing has a built-in variable called frameCount that's advanced by 1 each frame; I recommend using that instead of your own frame variable.  (I'll show you how to handle "wrap-around" in a minute.)

3.  You want to iterate over a grid of images, so you want your draw() method to have two nested for loops, like this:
Code:
void draw() {

for (int i = 0; i < numImagesDown; i ++) {
for (int j = 0; j < numImagesAcross; j ++) {

// image display code goes in here

}
}

}

Note that i and j each start at 0 and go up to 9, instead of going from 1 to 10.  This is considered "normal" for most programming languages, and it usually makes things easier to do it this way.

4.  You want each image to be 10 frames ahead of the one before it.  The easiest way to do this is to use another variable like this inside the nested for loops:
Code:
void draw() {

for (int i = 0; i < numImagesDown; i ++) {
for (int j = 0; j < numImagesAcross; j ++) {

int a = i*numImagesAcross + j;

int imageNo = frameCount + a*10;

imageNo = imageNo % numFrames;

image(images[imageNo], j*imW, i*imH, imW, imH);

}
}

}

The variable a will count up from 0 to 99 as the loops go through all the tiles.  The next line sets which image to use for this tile on this frame.  

The line after that takes care of "re-setting" the animation if necessary; the % operator effectively causes imageNo to "wrap around" if it's equal to or higher than numFrames.  For example, if imageNo 300 and numFrames is 200, then imageNo % numFrames will be 100.

(The images[] array will start at 0 and go up to 199, and this line of code will make sure imageNo is always in the same range.)

And finally, the last line inside the loop actually draws the image: Since j is counting across, we multiply that times the image width to get the x position.  Similary we use i to find the y position.  

I can't promise this will work, since I haven't tested it and I might have a typo or some other bonehead error.  But this should hopefully give you a general idea.  Please ask if you still have questions!

Page Index Toggle Pages: 1