Search code examples
processing

How can I fill a plane without gaps with regular squares?


I'm new to processing, and trying to do this task. Maybe you know how to do it, or know where I could read about it.

I need something like this:

link

Thanks in advance for your help.


Solution

  • The image is a 4x3 grid with zero spacing between the columns(vg) and rows(hg):

    final int _numRows = 3;
    final int _numCols = 4;
    
    void rectGrid(int l, int t, int w, int h, int hg, int vg) {
      int left;
      int top;
      for (int k = 0; k < _numRows; k++) {
        for (int j = 0; j < _numCols; j++) {
          left = l + j*(w+vg);
          top = t + k*(h+hg);
          stroke(0);
          strokeWeight(2);
          fill(255);
          rect( left, top, w, h);
        }
      }
    }
    
    void setup() {
      size(640, 340);
      background(209);
      rectGrid(20, 20, 150, 100, 0, 0);
    }