# Polygons Distribution with loops

**URL:** <https://discourse.processing.org/t/polygons-distribution-with-loops/32270>\
**Category:** Coding Questions\
**Created:** [September 15, 2021, 2:20pm UTC](https://discourse.processing.org/t/polygons-distribution-with-loops/32270 "2021-09-15T14:20:19Z")\
**Posts on this page:** 1\
**Showing post:** 4

<div class="post-metadata">

**Author:** ![rapatski](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/rapatski/32/5164_2.png) [@rapatski](https://discourse.processing.org/u/rapatski)\
**Post date:** [September 15, 2021, 9:38pm UTC](https://discourse.processing.org/t/polygons-distribution-with-loops/32270/4 "2021-09-15T21:38:39Z")

</div>

@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?

```auto
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);
}

```

---

_[View the full topic](https://discourse.processing.org/t/polygons-distribution-with-loops/32270)._
