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 › beginnger stuck: for function not exiting
Page Index Toggle Pages: 1
beginnger stuck: for function not exiting (Read 440 times)
beginnger stuck: for function not exiting
Jul 2nd, 2008, 10:13pm
 
Hello Processing Community,
  I started learning processing from the new book a few months ago, and it has been a blast to learn programming from this angle.  I have a background in architecture (graduated 2 years ago) but a heavy interest in programming which I would like to use for design and art.  Recently I have been trying to learn the ropes of using processing for 'VJ' and music visualization purposes.  I am still learning all I can, and recently came against a wall whilst trying to carry out a simple exercise in creating a 2dimensional grid in 3d space.  I successfully made one that starts as a grid and undulates to sine and cosine as you increase certain parameters (using controlP5).  
   I am now trying to structure the program more ( using classes) so that I can create multiple grids and control the point values better (using cellular automata, fft data, etc.) In doing this I have come up against an annoying problem when I try to iterate over my 2D array of 'y-values' for the grid.  
  I have been awaiting my chance to get involved on the forums and start learning from all of you out there, so I suppose this a great time to start!  I am sure the problem at hand is something that I have just overlooked, and would love help nailing it down.  I am also sure that the way in which I have made these grids is rather inefficient and would welcome pointers to hone the code while keeping control flexibility intact ( being able to render lots of different shapes at the points, and use different equations and rules to move and pulsate  the grid).

Following is my original, successful code, unclassed and laid out relatively simply:

import controlP5.*;
import processing.opengl.*;

float zn = 75;
float xn = 75;
float xrange = 600;
float zrange = 600;
float ypos = 200;
float zcenter = -200;
float xcenter = 0;


float xstart;
float xend;
float xint;
float zstart;
float zend;
float zint;

float[][] grid1y;
float[] grid1z;
float[] grid1x;

float metroN = 0;
float tempo = .5;
float direction = 1;
float yamp = 0;
float waveFreq = 0;

ControlP5 controlP5;
ControlWindow controlWindow;

void setup(){
 
 size(1200,800, OPENGL);
 background(0);
 frameRate(20);
 stroke(255);
 fill(255);
 smooth();
 colorMode(HSB, 360, 100, 100);
 
 controlP5 = new ControlP5(this);
 controlP5.setAutoDraw(false);
 controlWindow = controlP5.addControlWindow("controlP5window",100,100,400,500);
 controlWindow.setBackground(color(40));
 Controller yampSlider = controlP5.addSlider("yamp",0,50,10,10,200,20);
 yampSlider.setWindow(controlWindow);
 Controller wavefreqSlider = controlP5.addSlider("waveFreq",0,50,10,30,200,20);
 wavefreqSlider.setWindow(controlWindow);
 
 xstart = -(xrange/2) + xcenter;
 xend = (xrange/2) + xcenter;
 zstart = -(zrange/2) + zcenter;
 zend = (zrange/2) + zcenter;
 
 grid1y = new float[(int)xn][(int)zn];
 grid1z = new float[(int)zn];
 grid1x = new float[(int)xn];

 xint = xrange/xn;
 zint = zrange/zn;
 
 for ( int i = 0; i < zn; i++){
   grid1z[i] = zstart + i*(zint);
 }
 
 for ( int i = 0; i < xn; i++){
   grid1x[i] = xstart + i*(xint);
 }
 
 xint = xrange/xn;
 zint = zrange/zn;
}

void draw(){
 
 background(0);
 
 camera(0, 0, 500.0, // eyeX, eyeY, eyeZ
 0.0, 0.0, 0.0, // centerX, centerY, centerZ
 0.0, 1.0, 0.0); // upX, upY, upZ
 
 for(int i = 0; i<xn-1; i ++){
   for(int g = 0; g < zn-1; g ++){
     grid1y[i][g] = (sin(metroN - map(i,0,xn-1,0,PI)*waveFreq)-(cos(metroN - map(g,0,zn-1,0,PI)*waveFreq)))*yamp;
   }
 }
 
 for(int x=1; x< xn-1; x++){
   line(grid1x[x], ypos+grid1y[x][(int)zn-1], grid1z[(int)xn-1],grid1x[x]+xint, ypos+grid1y[x][(int)zn-1], grid1z[(int)zn-1]);
     for(int z = 1; z<zn-1; z++){
       line(grid1x[x], ypos+grid1y[x-1][z], grid1z[z], grid1x[x]+xint, ypos+grid1y[x][z], grid1z[z]);
       line(grid1x[x], ypos+grid1y[x][z-1], grid1z[z], grid1x[x], ypos+grid1y[x][z], grid1z[z]+xint);
     }
 }
 
 for(int z = 0; z<zn-1; z++){
  line(grid1x[(int)xn-1], ypos+grid1y[(int)xn-1][z], grid1z[z],grid1x[(int)xn-1], ypos+grid1y[(int)xn-1][z], grid1z[z]+zint);
 }
 
 metroN += (tempo * direction);
}
 


continued in first reply...
beginnger stuck: for function not exiting (cont)
Reply #1 - Jul 2nd, 2008, 10:14pm
 
Now here is the code that I am trying to make work now, no rendered objects, just the structure for updating the 'y-values'.  The error occurs when the program tries to iterate over my 2d array of y-values (pointABnew[][]) claiming that the array index is out of bounds.  It seems that the for function does not stop at one minus the length of the array, despite the command to do so.  I have tried printing the i value, and it indeed advances far past 99 and eventually slows the applet to a halt. The error occurs almost at the bottom in the update() function:


import controlP5.*;
import processing.opengl.*;

//initial values for grid position and spacing, eventually mapped to sliders
float cent1a = 0;  
float cent1b = 0;
float range1a = 500;
float range1b = 500;
float num1a = 100;
float num1b = 100;

//define a single grid for now
Grid3d grid1;

//counter variables
float metroN = 0;
float tempo = .5;
float direction = 1;

//y value augmentation variables
float yamp = 0;
float waveFreq = 0;

//control array for grid, used to pass various information to grid objects,
float[] grid1Control = { cent1a, cent1b, range1a, range1b, num1a, num1b, 0, 0, 0, 0 };

ControlP5 controlP5;
ControlWindow controlWindow;

void setup(){
size(1300,900,OPENGL);
background(0);
frameRate(20);
smooth();
colorMode(HSB,360,100,100);

//conrolP5 setup
controlP5 = new ControlP5(this);
controlP5.setAutoDraw(false);
controlWindow = controlP5.addControlWindow("controlP5window",100,100,400,500);
controlWindow.setBackground(color(40));

Controller yampSlider = controlP5.addSlider("yamp",0,50,10,10,200,20);
yampSlider.setWindow(controlWindow);
Controller wavefreqSlider = controlP5.addSlider("waveFreq",0,50,10,30,200,20);
wavefreqSlider.setWindow(controlWindow);

//assign grid1
grid1 = new Grid3d();  
}

void draw(){
 
 background(0);
 
 camera(0, 0, 500.0, // eyeX, eyeY, eyeZ
 0.0, 0.0, 0.0, // centerX, centerY, centerZ
 0.0, 1.0, 0.0); // upX, upY, upZ
 
 controlP5.draw();
 
 //refresh the grid using the grid1control array, eventually there will be an update command for the control array so that the grid can change parameters
 grid1.refresh(grid1Control);
 
 //grid update and draw  
 grid1.update();
 grid1.render();
 
 //counter goes up
 metroN += (tempo * direction);
}

class Grid3d {
 //initial grid parameters
 float centA;
 float centB;
 float rangeA;
 float rangeB;
 float intA;
 float intB;
 float numA;
 float numB;
 float startA;
 float startB;
 float yPos = 200;
 
 //grid control array
 float [] gridcontrol = new float[10];
 
 //point value arrays
 float[] pointA;
 float[] pointB;
 float[][] pointABnew;
 float[][] pointABprev; //setting up for cellular automata rules

 
 Grid3d(){
   //constructor
 }
 
 public void refresh(float[] gc){
   //update object control array and pass values to parameters
   gridcontrol = gc;
   centA = gridcontrol[0];
   centB = gridcontrol[1];
   rangeA = gridcontrol[2];
   rangeB = gridcontrol[3];
   numA = gridcontrol[4];
   numB = gridcontrol[5];
   
   //point intervals updated
   intA = rangeA/numA;
   intB = rangeB/numB;
   
   //start positions(top left corner) updated
   startA = centA - (rangeA/2);
   startB = centB - (rangeB/2);
   
   //point arrays updated
   pointA = new float [(int)numA];
   pointB = new float [(int)numB];
   pointABnew = new float [(int)numA][(int)numB];
   pointABprev = new float [(int)numA][(int)numB];
   
   for ( int i = 0; i < numA-1; i++ ) {
     pointA[i] = startA + (i*intA);
   }
   
   for ( int i = 0; i < numB-1; i++ ) {
     pointB[i] = startB + (i*intB);
   }
 }
 
 public void update() {
   //update pointABnew array (y-values for now of grid1)
   //println(pointABnew[99][99]);
   // error occurs when ever 2d array is iterated over, array index out of bounds, printing 'i' shows that the for loop is not being exited and 'i' gets huge
    for (int i = 0; i< numA-1; i++){
      for (int g = 0; g< numB-1; i++){
       pointABnew[i][g] = (sin(metroN - map(i,0,numA-1,0,PI)*waveFreq)-(cos(metroN - map(g,0,numB-1,0,PI)*waveFreq)))*yamp;
     }
   }
 }
 
 public void render() {
   //nothing here yet, use y-values to draw objects
 }

}


I know I am probably not using the correct terminology or protocol.  I would love any pointers in regards to the problem, the code, or my approach here on the boards.  Thanks to anyone who got down to here on the post!
Good day to all of you!
-archo_p
Re: beginnger stuck: for function not exiting
Reply #2 - Jul 15th, 2008, 12:32am
 
Hi,

I'm replying quickly and maybe I'm missing your point but it seems to me that the offending line is this one:

for (int g = 0; g< numB-1; i++){

most likely you meant

for (int g = 0; g< numB-1; g++){

given that i is the variable used in the external loop.

I hope this will solve your problem.

Cheers.
Re: beginnger stuck: for function not exiting
Reply #3 - Jul 15th, 2008, 3:39am
 
Gianzoo,
  Thanks for taking the time to dig through there.   As I thought, it was a stupid mistake, that I should have caught before going to the boards!  I had been too busy to work on it for a bit, and came back and just retyped the for loops to make sure, and sure enough it worked!
  I'm still trying to hone that programmers eye I suppose.  Thanks for the help!
Page Index Toggle Pages: 1