Array cannot be resolved to variable

edited February 2016 in Questions about Code

So, I'm trying to do a version of the EvoLisa project in Processing.

The concept is pretty simple; import a photo, draw 50 triangles of random position and rgba values, and then evaluate how close to the original image the triangles are. Rinse and repeat.

However, I'm coming up against what I'm fairly sure is a scope error; I just don't understand the problem. The code that's throwing the error is this:

 if ((fitness-prevFitness)<0) {
    polygons[value][polygon] = currentPolygonValue;
  }

The array polygons[][] is two-dimensional and declared in setup().

The full code is here:

PImage photo;
float polygonQuantity = 50,changes = 0, mutations = 0;
float fitness, prevFitness, initialFitness;


void setup() {
  size(1800,300);
  //randomly populated in a few lines down.
  float[][] polygons = new float[10][int(polygonQuantity)];
  photo.resize(width/2,height/2);
  image(photo,0,0);
  for(int x = 0;x<polygonQuantity;x++) {
    polygons[0][x] = random(width/2);
    polygons[1][x] = random(width/2);
    polygons[2][x] = random(width/2);
    polygons[3][x] = random(height/2);
    polygons[4][x] = random(height/2);
    polygons[5][x] = random(height/2);
    polygons[6][x] = int(random(256));
    polygons[7][x] = int(random(256));
    polygons[8][x] = int(random(256));
    polygons[9][x] = int(random(100));
  }
  //this section produces an initial fitness assessment
  //(because only a completely black image would be completely unfit)
  //and then normalises it.
  loadPixels();
  int difference = 0, loc = width/2, compLoc = 0;
  for (int x = width/2;x<width;x++) {
    for (int y = 0;y<height/2;y++) {
      loc = x + (y * width);
      compLoc = x + (y * width) - width/2;

      difference += (sqrt(sq(red(pixels[loc]) - red(pixels[compLoc]))) + (sqrt(sq(green(pixels[loc]) - green(pixels[compLoc])))) + (sqrt(sq(green(pixels[loc]) - green(pixels[compLoc])))));

    }
  }
  initialFitness = map(difference,(675*(width/2)*(height/2)),0,0,1);
}

void draw() {
  float currentPolygonValue;
  drawPolygons();
  evalAndReturnFitness();
  prevFitness = fitness;
  mutatePolygon();
  evalAndReturnFitness();
  if ((fitness-prevFitness)<0) {
    polygons[value][polygon] = currentPolygonValue;
  } else {
    changes++;
    println("Total changes: " + changes + " with overall fitness: " + (fitness+(fitness-prevFitness)) + " after " + mutations+1 + " mutations.");
  }
  mutations++;
}

void evalAndReturnFitness() {
  loadPixels();
  //the imported image is drawn at 0,0. The triangles are drawn to the right of it.
  //compLoc is the comparative location - i.e., for pixel 51 of the triangles, the compLoc is pixel 51 of the image
  int difference = 0, loc = width/2, compLoc = 0;
  for (int x = width/2;x<width;x++) {
    for (int y = 0;y<height/2;y++) {
      loc = x + (y * width);
      compLoc = x + (y * width) - width/2;
      //horrible code, I know. This section finds the difference between the red, green and blue values
      //of the triangles and the original images, then squares and square roots it so that all values are positive.
      //The goal - i.e. 100% fitness - is difference = 0.
      difference += (sqrt(sq(red(pixels[loc]) - red(pixels[compLoc]))) + (sqrt(sq(green(pixels[loc]) - green(pixels[compLoc])))) + (sqrt(sq(green(pixels[loc]) - green(pixels[compLoc])))));

    }
  }
  fitness = map(difference,1,initialFitness,0,1);
  updatePixels();
}

void mutatePolygon() {
  int polygon = int(random(polygonQuantity));
  int value = int(random(10));
  currentPolygonValue = polygons[value][polygon];
  if(value<3) {
    polygons[value][polygon] = random(width/2);
  } else if (value<6) {
    polygons[value][polygon] = random(height/2);
  } else if (value<9) {
    polygons[value][polygon] = int(random(255));
  } else {
    polygons[value][polygon] = int(random(100));
  }
}

void drawPolygons() {
  for (int x = 0; x<polygonQuantity; x++) {
    fill(polygons[6][x],polygons[7][x],polygons[8][x],polygons[9][x]);
    triangle(polygons[0][x],polygons[3][x],polygons[1][x],polygons[4][x],polygons[2][x],polygons[5][x]);
  }
}

Any light that anyone can shed on this is really very appreciated!

Tagged:

Answers

  • scope.

    polygons is defined in setup() and is only available during setup().

    moving line 9 to line 4, outside setup(), will make it global and accessible within draw() (and everywhere else)

  • Aha; thank you.

    Blindingly obvious once you see it. Much appreciated!

Sign In or Register to comment.