Polygons Distribution with loops

@jb4x that’s quite the pro approach.

I’ve added below two other ways you achieve a similar result, one a slightly simpler version of the one jb4x proposed. I’m not a fan of the “quad” method… it’s awkward having to put 8 arguments into it… @humano look up “beginShape()” in the Processing reference?

int numObjects = 100;

void setup() 
{
  size(500, 500);  
  
}

void draw() 
{
  background(255);
  noStroke();
  
  // draw red 'quads' using the random rect method
  for(int i = 0; i < numObjects; i++)
  {
    float xPos = random(width/2);
    float yPos = random(height);
    float minS = 10;
    float maxS = 20;
    
    colorMode(HSB, 360, 100, 100);
    fill(0, 100, random(100));
    drawRandomRect(xPos, yPos, minS, maxS);
  }
  
  // draw blue quads using the angular quad method
  for(int i = 0; i < numObjects; i++)
  {
    float xPos = width/2 + random(width/2);
    float yPos = random(height);
    float minS = 10;
    float maxS = 20;
    
    colorMode(HSB, 360, 100, 100);
    fill(220, 100, random(100));
    drawRandomQuad(xPos, yPos, minS, maxS);
  }
  
  noLoop();
}

void drawRandomRect(float x, float y, float minSize, float maxSize) 
{
  float randomRot = random(HALF_PI); // randomly rotate each rect
  
  pushMatrix();
  translate(x, y);
  rotate(randomRot);
  
  beginShape();
  vertex(0, 0);
  vertex(0 + random(minSize, maxSize), 0);
  vertex(0 + random(minSize, maxSize), 0 + random(minSize, maxSize));
  vertex(0, 0 + random(minSize, maxSize));
  endShape(CLOSE);
  
  popMatrix();
}

void drawRandomQuad(float x, float y, float minSize, float maxSize) 
{  
  float randomRot = random(HALF_PI); // randomly rotate each quad
  
  beginShape();
  for (int i = 0; i < 4; i++) {
    // imagine that we're going round a circle, stopping at 4 points along the circle
    float angle = randomRot + i * HALF_PI; 
    float r = random(minSize, maxSize); // choose a different distance from the centre for each point
    float vx = x + r * cos(angle); // look up trigonometry on wikipedia
    float vy = y + r * sin(angle);
    vertex(vx, vy);
  }
  endShape(CLOSE);
}

1 Like