处理:由于嵌套循环

时间:2017-09-13 15:14:43

标签: arrays nested processing indexoutofboundsexception

我想弄清楚第31行的loc位置的公式,但我无法弄明白。谁能引导我找到解决方案?

PImage theImage;
int cellSize = 6; // dimension of a cell where logic is applied
int cols, rows; // number of rows and columns based on cellsize 
int col; // column in the cell
int row; // row in the cell
float edgeR; // (amount of red in the corners)
float edgeB; // (amount of blue in the corners)
float edgeG; // (amount of green in the corners)
float middleR; // (amount of red in the center)
float middleB; // (amount of blue in the center)
float middleG; // (amount of green in the center)

void setup()
 {
  size(1570,1112); // fits the image
  theImage = loadImage("im.png"); // load the image
  cols = width/cellSize ; // number of colums in the grid of the image
  rows = height/cellSize ; // number of rows in the grid of the image
 }

void draw ()
 {
  background(0);
  loadPixels();
  theImage.loadPixels(); // load every pixel of the image in an array
  image(theImage,0,0); // display the image at pos 0 0 
  for ( int i=0; i< rows;i++) {  
    for ( int j = 0; j< cols;j++) {    // loop matrix of cells         
        for ( int x = i*cellSize; x<i*cellSize+cellSize;x++) {
         for ( int y = j*cellSize; y<j*cellSize+cellSize;y++) {  // 
            loop every pixel of the cell

            ---> int loc = (i*cellSize+x) + 
             (j*cellSize*width+y*cellSize*width); // grrrrr  <-----
              float r = red(theImage.pixels[loc]);  // red value
              float g = green(theImage.pixels[loc]); // green value
              float b = blue(theImage.pixels[loc]);  // blue value             
              if (x - i*cellSize <= cellSize /3 ) {   // it is in a column 1
           col = 1;
          }
           else if (x - i*cellSize >= cellSize*2/3 ) { // it is in a column 3
           col = 3;
           }
           else {
           col = 2;   // it is in a column 2
           }
           if (y - j*cellSize <= cellSize /3 ) {  // it is in row 1
           row = 1;
           }

           else if (y - j*cellSize >= cellSize*2/3)  { // it is in row 3
           row = 3;
           }
           else {   // in row 2
           row = 2;
           }                       
          if (col == 1 & row ==1 || (col == 3 & row == 3) || (col == 1 & row == 3) || (col == 3 & row == 1)) {
                edgeR = edgeR + r;   // aggregate the edge color values
                edgeG = edgeG  + g;
                edgeB = edgeB + b;             
           }
         else {
              middleR = middleR + r; // aggregate the center color values
              middleG = middleG + g;
              middleB = middleB + b;

           // here some magic will be applied if there would be no ArrayIndexOutOfBoundsException: 1745840 error

        }    
     }     
   }
}
}
}

当我运行代码时,输​​出窗口冻结,我得到一个ArrayIndexOutOfBoundsException:1752120错误/如果我把调试放在这个也继续说:调试器忙。

我很确定loc位置不正确,但我不知道如何修复公式。嵌套的for循环也可能是个问题。

非常感谢任何帮助,非常感谢。

一切顺利,

蒂姆

2 个答案:

答案 0 :(得分:0)

我改变了

int loc = (i*cellSize+x) + 
         (j*cellSize*width+y*cellSize*width);

int loc = x + y*width;

然后它有效。唯一的问题是,如果我使用大图像,我会得到ArrayIndexOutOfBoundsException,这可能是由于超出了数组中的最大索引数而引起的。谢谢你帮助我!

答案 1 :(得分:-1)

这太多代码要求我们为您调试。

您需要break your problem down into smaller steps并执行一些debugging来隔离您的问题。请注意,调试的第一步是用一张纸和一支铅笔来代替代码,而不是调试器本身!

您需要能够回答这些类型的问题:当您收到错误时,每个变量的值是多少? ijxy有哪些值? loc有什么价值? theImage.pixels有多少个索引?

请注意,我并没有要求您告诉我这些问题的答案。你需要自己回答这些问题,这样你就可以开始弄清楚哪一行代码没有达到预期的效果。

如果您仍然无法解决问题,则需要将问题范围缩小到MCVE

另外,只是旁注:你不应该写这样的整个程序,然后只在它完成后进行测试。您需要在较小的块中工作,并确保块在正常运行之前正常工作。在您的示例中,您可能只使用最外层的for循环开始,并确保迭代了您期望的值。然后是第二个for循环等。您可能还应该重构代码,这样就不会有四重嵌套for循环。