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 › multiple arrays causes data to drop
Page Index Toggle Pages: 1
multiple arrays causes data to drop (Read 280 times)
multiple arrays causes data to drop
Nov 9th, 2008, 11:55pm
 
I created a sketch that works properly with one array. When I add two more arrays to the sketch it thinks the length of one array applies to all the arrays.

For example, if I want three different arrays of 60 images each to draw sequentially, it will draw only 20 images per array (totaling 60 instead of the desired 180).

Does anyone know what I'm doing incorrectly?

Here is the code:

// This sketch draws stills from three different video streams.
import processing.video.*;
int xpos;
int ypos;
int xWidth;
int yHeight;
int count = 0;
int numImagesB = 48;
int numImagesM = 48;
int numImagesT = 48;
PImage[] imagesB = new PImage[numImagesB]; // Declairs the bottom images
PImage[] imagesM = new PImage[numImagesM]; // Declairs the middle images
PImage[] imagesT = new PImage[numImagesT]; // Declairs the top images
PImage maskImg;
MovieMaker mm;  // Declare MovieMaker object

void setup() {
 size(1020, 840);
 background(0);
 frameRate(6);
 xpos = width - 300;
 ypos = 0;
 mm = new MovieMaker(this, width, height, "drawing.mov", 6, MovieMaker.H263, MovieMaker.LOSSLESS);

// I don't think that this does what I thought it does, and I don't think I need it.
/*  // This loop draws the mask over each image in all image arrays.
    for (int i = 1; i <= imagesB.length; i++) {
    imagesB[i-1] = loadImage("B (" + i + ").jpg");
    }
*/

 maskImg = loadImage("aMask3.jpg");

 // This loop prepairs each image in the bottom image array.
 for (int i = 1; i <= imagesB.length; i++) {
   imagesB[i-1] = loadImage("B (" + i + ").jpg");  
   imagesB[i-1].mask(maskImg);
 }

 // This loop prepairs each image in the middle image array.
 for (int j = 1; j <= imagesM.length; j++) {
   imagesM[j-1] = loadImage("M (" + j + ").jpg");  
   imagesM[j-1].mask(maskImg);
 }

 // This loop prepairs each image in the top image array.
 for (int k = 1; k <= imagesT.length; k++) {
   imagesT[k-1] = loadImage("T (" + k + ").jpg");  
   imagesT[k-1].mask(maskImg);
 }

}


void draw() {
 // Drawing the bottom image array
 if (count < imagesB.length) {
   tint(225, 225, 225, createTint());
   image(imagesB[count], xpos, ypos + 600);
   count++;
   mm.addFrame();  // Add window's pixels to movie
 }
 for (int f = distanceBtwnImages(); f <= distanceBtwnImages(); f++) {
   xpos = xpos - f;
 }

 // Drawing the middle image array
 if (count < imagesM.length) {
   tint(225, 225, 225, createTint());
   image(imagesM[count], xpos, ypos + 300);
   count++;
 }
 for (int g = distanceBtwnImages(); g <= distanceBtwnImages(); g++) {
   xpos = xpos - g;
 }

 // Drawing the top image array
 if (count < imagesT.length) {
   tint(225, 225, 225, createTint());
   image(imagesT[count], xpos, ypos);
   count++;
 }
 for (int h = distanceBtwnImages(); h <= distanceBtwnImages(); h++) {
   xpos = xpos - h;
 }

}

int distanceBtwnImages(){
 int numberIs;
 numberIs = 9;
 return numberIs;
}

int createTint(){
 int numberIs;
 numberIs = 160;
 return numberIs;
}

// This is the method that stops the recording of the movie
void keyPressed() {
 if (key == ' ') {
   mm.finish();  // Finish the movie if space bar is pressed!
 }
}
Re: multiple arrays causes data to drop
Reply #1 - Nov 10th, 2008, 12:49am
 
Hi,

I've only looked at the code 'diagonnaly', but it seems that you're incrementing the count variable three times per frame. So for the first array, the sequence is 0,3,6,9... which means that you  draw 1/3 of the array...
Re: multiple arrays causes data to drop
Reply #2 - Nov 10th, 2008, 3:52pm
 
Thanks! I made three different count variables and that did the trick.
Re: multiple arrays causes data to drop
Reply #3 - Nov 10th, 2008, 8:14pm
 
Another way to do it would be to just use one variable and only increment it once each time, then add any offsets:

(just an example, not even compiled)
Code:

// Early out if the array lengths are not equal
if( imgArray1.length != imgArray2.length &&
imgArray2.length != imgArray3.length )
{
println( "Error: Image arrays are not equal in size!" );
return;
}

for( int i=0; i<imgArray1.length; ++i )
{
imgArray1[i] // Access to first image
imgArray2[(i+1)%imgArray2.length] // Access to second image
imgArray3[(i+2)%imgArray3.length] // Access to third image
}



This way there is less that can go wrong in your code by accidentally tampering with the incremented variables. However, you pay the price of having to do more work each frame but you might get back that time by not using as much memory.

Pre-increment on variables (++i instead of i++)is good habit as well because it doesn't require a temporary variable. It may not be an issue in java though because Java doesn't support operator overloading so increments can only happen on primitive types. (You would have a much greater performance hit creating a temporary object vs. primitive type)

Just wanted to share that info. If anyone sees anything wrong with my learning's please enlighten me - most of this I've brought over from C++.

Jack
Page Index Toggle Pages: 1