[SOLVED] I'm failing at drawing multiple quads in a loop

I’m a little OOOCD perhaps, but how about making a Rect or Quad class to store your four coordinates in before you draw? So much cleaner?


int numQuads = 16;
Quad[] quads;

void setup()
{
  size(800, 600);
  
  // your list of quads
  quads = new Quad[numQuads];
  
  // assign attributes / initialize each quad with some values 
  // (ignore the below, you will do this in your own way)
  for(int i = 0; i < quads.length; i++)
  {
    // pick a random location (x, y)
    PVector randomLoc = new PVector(random(width), random(height));
    
    quads[i] = new Quad();
    
    // create a randomly rotated square (in a bit of a backward way) and add it to the random loc above
    float rotOffset = random(PI/2);
    float radius = 10;
    quads[i].a = PVector.add(randomLoc, new PVector(radius * cos(rotOffset + 0), radius * sin(rotOffset + 0)));
    quads[i].b = PVector.add(randomLoc, new PVector(radius * cos(rotOffset + 0.25 * TWO_PI), radius * sin(rotOffset + 0.25 * TWO_PI)));
    quads[i].c = PVector.add(randomLoc, new PVector(radius * cos(rotOffset + 0.5 * TWO_PI), radius * sin(rotOffset + 0.5 * TWO_PI)));
    quads[i].d = PVector.add(randomLoc, new PVector(radius * cos(rotOffset + 0.75 * TWO_PI), radius * sin(rotOffset + 0.75 * TWO_PI)));
  }
  
}

void draw()
{
  // render your quads
  for(int i = 0; i < quads.length; i++)
  {
    stroke(0);
    quads[i].render();
  }
}

class Quad
{
  PVector a, b, c, d;
  
  void render()
  {
    beginShape();
    vertex(a.x, a.y);
    vertex(b.x, b.y);
    vertex(c.x, c.y);
    vertex(d.x, d.y);
    endShape(CLOSE);
  }
}


2 Likes