Problem looping through a 4D array

OK, well I managed to spend a bit of time on this, but haven’t made a great deal of progress, and what I have done might be a backwards step!

I’ve merged the two sketches from @Chrisir, and used the function from @RichardDL that calculates 3 bits-on numbers…


// list uses gray scales as index. The list contains the
//different permuts for this gray scale (number of permuts can vary).
ListOfPermuts[] permuts = new ListOfPermuts[256];   

PixelRepresentation[][] myList; 
IntList myBinaryList;
int binaryListLength;
int num;
int count3 = 0;

PImage img;

void setup() {
  size(600, 600);
  noSmooth();
  noStroke();
  //frameRate(1);

  img = loadImage("fashion_MNist_01.png");
  img.resize (200, 200);

  myList = new PixelRepresentation[img.width][img.height];
  myBinaryList = new IntList();


  // RichardDL's code for finding 12 bit binary numbers with 3 bits ON
  for (num  = 0; num < 4096; num++) {
    if (is3bits(num)) {
      count3++;
      myBinaryList.append(num);
    }
  }


  // fill permuts 
  // i represents gray value 
  for (int i=0; i<256; i++) {
    permuts[i] = new  ListOfPermuts( i );
  }//for 

  for (int x = 0; x < img.width; x++) {
    for (int y = 0; y < img.height; y++ ) {
      // Calculate the 1D pixel location
      int loc = x + y*img.width;

      color c1=img.pixels[loc]; 
      myList[x][y] = new PixelRepresentation (c1, x*5, y*5);
    }// for
  }// for
}// setup

void draw() {
  //image(img, 10, 10);

  color c1 = get(mouseX, mouseY); 

  for (int x = 0; x < img.width; x++) {
    for (int y = 0; y < img.height; y++ ) {
      myList[x][y].display();
    }// for
  }// for

  permuts[int(brightness(c1))].display();
  
}// draw


// ================================================================

class PixelRepresentation {

  //
  color col;
  float x, y;

  boolean[][] fieldArray = new boolean [ 4 ][ 3 ]; // !!!!!!!!!!!!  

  PixelRepresentation (color col_, 
    float x_, float y_) {
    col=col_;
    x=x_;
    y=y_;
  }

  void display() {
    // use fieldArray here 
    fill(col); 
    rect(x, y, 3, 3 );
  }
}//class
//


//================================================================

class ListOfPermuts {

  // class holds all permutations for ONE brighntness 

  // length can vary (therefore ArrayList)
  ArrayList<Field> listOfPermutations = new ArrayList();

  int brightnessOfField; 

  // counter or permut for display as an animation
  int permutNum;

  //constr 
  ListOfPermuts (int brightnessOfField_) {

    brightnessOfField = 
      brightnessOfField_;

    // random number of permuts for this brightness.....
    // replace by something that gives the permuts for the brightnessOfField_

    binaryListLength = myBinaryList.size();

    for (int i = 1; i < binaryListLength; i++) {      
      Field f1 = new Field(brightnessOfField);
      listOfPermutations.add(f1);
    } //for
  } // constr ---------

  void display() {
    int xoffset = 330;
    int yoffset = 300;

    // make a horizontal list
    for (Field f1 : listOfPermutations) {
      f1.display(xoffset, yoffset); 
      xoffset += 40;
    }//for

    // Animation 
    Field f1 = listOfPermutations.get(permutNum%listOfPermutations.size()); 
    f1.display(330, height-139);
    permutNum++;
    //
  } // func  
  //
} //class 

//================================================================

class Field {

  // ONE 3x4 Field 

  boolean [][] field = new boolean [3][4];  

  //constr 
  Field (int brightnessOfField_) {

    // replace by something that gives the permuts for the brightnessOfField_
    for (int x = 0; x < field.length; x++) {
      for (int y = 0; y < field[0].length; y++ ) {
        field[x][y] = false;
        int position = x + (y * field.length);
        
        // bitwise trick to iterate over binary digits
        int onOrOff = ((myBinaryList.get(0) & 1<<position) >> position);
        
        if (onOrOff > 0) 
          field[x][y] = true;
      }
    }
  }//constr ---------

  void display( int xoffset, int  yoffset) {
    //
    for (int x = 0; x < field.length; x++) {
      for (int y = 0; y < field[0].length; y++ ) {
        //
        fill(0); 

        if (field[x][y]) fill(255); // WHITE 
        rect(xoffset + x*5, yoffset +  y*5, 4, 4);
      }
    }
  }//func
  //
}//class
//


// ================================================================


// RichardDL's code for finding 12 bit binary numbers with 3 bits ON - function

boolean is3bits(int no)
{
  // return true if given number has 3 bits set.
  int ix;
  int tot = 0;
  for (ix = 0; ix < 12; ix++) {
    if ((no & 1) == 1) tot++;
    no = no >> 1;
  }
  return tot == 3;
}

I am now able to fill the Field object with correct numbers for 3-bits-on, but am filling it with only ONE configuration over and over… I realise this is because I am just using the ‘0th’ item in myBinaryList, but I’m not quite sure what to do to add ALL 220 items in myBinaryList here…

int onOrOff = ((myBinaryList.get(0) & 1<<position) >> position);

I’ll try and break my code down and explain it a bit better tomorrow, but for now, I’m screened out!

1 Like