array index out of bounds
              in 
             Programming Questions 
              •  
              2 years ago    
            
 
           
             What does it mean for the array index to be out of bounds?
            
             
            
            
             
              
             
             
                
             
             
              
             
              
           
 
            
           
              PImage img;       // the source image
             
             
              int cellsize = 4; // dimensions of each cell in the grid
             
             
              int cols, rows;   // number of columns and rows in our system
             
             
              void setup() {
             
             
                size(600,600,P3D);
             
             
                img = loadImage("flight patterns.jpg");  //load the image
             
             
                cols =width/cellsize;     // calculate # of columns
             
             
                rows = height/cellsize;   // calculate # of rows
             
             
              }
             
             
              void draw()  {
             
             
                background(0);
             
             
                loadPixels();
             
             
                // Begin loop for columns
             
             
                for (int i = 0; i < cols; i++) {
             
             
                //begin loop for rows
             
             
                  for (int j = 0; j < rows; j++) {
             
             
                    int x = i*cellsize + cellsize/2;  // x position
             
             
                    int y = j*cellsize + cellsize/2;  // y position
             
             
                    int loc = x + y*width;            // pixel array location
             
             
                    color c = img.pixels[loc];        // grab the color
             
             
                    // Calculate a z position as a function of mouseX and pixel brightness
             
             
                    float z = (mouseX/(float)width) *brightness(img.pixels[loc]) - 100.0;
             
             
                    // Translate to the location, set fill and stroke, and draw the rect
             
             
                    pushMatrix();
             
             
                    translate(x,y,z);
             
             
                    fill(c);
             
             
                    noStroke();
             
             
                    rectMode(CENTER);
             
             
                    rect(0,0,cellsize,cellsize);
             
             
                    popMatrix();
             
             
                  }
             
             
                }
             
             
              }
             
             
              
              1  
            
 
            