How can I do a vector 2d array ?

edited February 2017 in How To...

Can I even make an 2d array of vectors, if yes, how ?

Tagged:

Answers

  • edited February 2017 Answer ✓

    PVector [][] mapPVectors = new PVector[100][100];

  • PVector [][] mapPVectors = new PVector[100][100];
    int max1=100;
    
    void setup() {
      size(700, 700);
      for (int x=0; x<max1; x++) {
        for (int y=0; y<max1; y++) {
          mapPVectors[x][y] = new PVector (5*x+19, 5*y+19);
        }
      }
    }
    
    void draw() {
      for (int x=0; x<max1; x++) {
        for (int y=0; y<max1; y++) {
          point(mapPVectors[x][y].x, mapPVectors[x][y].y);
        }
      }
      //
    } 
    
  • Answer ✓

    there is a tutorial on 2D arrays

    https://www.processing.org/tutorials/2darray/

  • Thanks a lot ! Again :) @Chrisir

  • edited February 2017

    replacing PVector with your own class Cell to store a color etc. in addition to the position inside the class

    final int max1=100;
    Cell [][] grid = new Cell[max1][max1];
    
    void setup() {
    
      size(740, 740);
    
      for (int x=0; x<max1; x++) {
        for (int y=0; y<max1; y++) {
    
          color color1 = color(random(256), random(256), random(256));
          boolean exist = random(100) > 30; 
          grid[x][y] = new Cell (7*x+20, 7*y+20, color1, exist);
        }
      }
    }
    
    void draw() {
      for (int x=0; x<max1; x++) {
        for (int y=0; y<max1; y++) {
    
          grid[x][y].display();
        }
      }
    } 
    
    // ===================================================
    
    class Cell {
    
      // Attributes
      float x;
      float y;
    
      float diameter  = 5;
      color cellColor=0;
    
      boolean exist;  
    
      // constructor
      Cell(float theX, float theY, 
        color theColor, 
        boolean theExist) {
        x = theX;
        y = theY;
        cellColor=theColor;
        exist=theExist;
      } // constructor
    
      void display() {
        if (exist) {
          fill(cellColor);
          // noStroke(); 
          rect (x, y, diameter, diameter);
        }
      }// method
      //
    }//class 
    //
    
  • edited February 2017

    and 3D grid:

    it needs to be mentioned that we can do a 3D grid within a 1D array easily - so there is no connection between 3D grid (what you display) and the 3D / 1D array (the internal data structure).

    Because also in a 1D array yoiu can store 3D data.

    // grid in 3D 
    
    final int max1=30;
    Cell [][][] grid = new Cell[max1][max1][max1];
    float angle1=0.0;
    
    void setup() {
    
      size(740, 740, P3D);
    
      for (int x=0; x<max1; x++) {
        for (int y=0; y<max1; y++) {
          for (int z=0; z<max1; z++) {
    
            color color1 = color(random(256), random(256), random(256));
            float threshold =  (map(z, 0, max1, 60, 99.9)); 
            boolean exist = random(101) > threshold; 
            grid[x][y][z] = new Cell (7*x-(7*30/2), 7*y-(7*30/2), 7*z-(7*30/2), 
              color1, 
              exist);
          }
        }
      }
      noStroke();
      background(0);
    }
    
    void draw() {
      background(0);
      lights(); 
    
      camera(0, -69, 266, 
        0, 0, 0, 
        0, 1, 0);
    
      rotateY (angle1); 
      angle1+=0.01; 
    
      for (int x=0; x<max1; x++) {
        for (int y=0; y<max1; y++) {
          for (int z=0; z<max1; z++) {
    
            grid[x][y][z].display();
          }
        }
      }
    }
    
    // =========================================================
    
    class Cell {
    
      // Attributes
      float x;
      float y;
      float z;
    
      float diameter  = 5;
      color cellColor=0;
    
      boolean exist;  
    
      // constructor
      Cell(float theX, float theY, float theZ, 
        color theColor, 
        boolean theExist) {
        x = theX;
        y = theY;
        z = theZ;
        cellColor=theColor;
        exist=theExist;
      } // constructor
    
      void display() {
        if (exist) {
          fill(cellColor);
          // noStroke();
          // stroke(111);
          pushMatrix();
          translate(x, y, z); 
          box(diameter);      
          popMatrix();
        }
      }// method
      //
    }//class 
    //
    
Sign In or Register to comment.