 # Multiple polygons with a loop

Hi there!

I have made a function to create a random polygon, could you please help me to create, for example, 30 polygons like this? Thank you so much

``````void setup(){

size(1000,1000);
float x1 = random(width);
float x2 = random(width);
float x3 = random(width);
float x4 = random(width);
float y1 = random(height);
float y2 = random(height);
float y3 = random(height);
float y4 = random(height);
for(int i=0; i<30; i++){

}
}

void drawQuad(float x1, float y1, float x2, float y2, float x3, float y3, float x4, float y4){
fill(255,0,255);
}

``````
1 Like

Hi @humano,

• Writing a function is fine but your `drawQuad` function always draw a quad with a pink color with the provided coordinates. This is the same as calling `quad` directly after setting the fill color.

• You want to do something multiple times. You guessed right: you have to use a loop.
The thing is that in your `for` loop, you are only calling your `drawQuad` function and therefore the `x1`, `x2`… coordinates never change, they are the same as the first time you assigned them to a random value.

You want a random quad each time so how about you try to move the variables initialization in the for loop?

Hope it helps 1 Like

Thank you so much! You mean something like this:

``````
size(1000,1000);
noStroke();
for(int i=0; i<30; i = i + 1){
float x1 = random(width);
float y1 = random(height);
float x2 = random(width);
float y2 = random(height);
float x3 = random(width);
float y3 = random(height);
float x4 = random(width);
float y4 = random(height);
noStroke();
fill(random(255),random(255),random(255));
}
``````
2 Likes

You nailed it! Pro tip: press `Ctrl+T` to auto format your code in the Processing editor

I have seen that some of this quads are twisted, do you know how could I avoid It? I can’t imagine the way to do that easily

Think we answered that question in your other post: Polygons Distribution with loops - #4 by rapatski

Try not to double/cross post if you can?

2 Likes

You are right, in other post about positions they gave me a solution for shapes, but actually I am talking about another thing. Maybe I should open another chat to explain It better.

The reason they cross is because of the order of vertices its expecting. When you look up quad() or beginShape() you’ll see that they are expecting the points (vertex/vertices) of the polygon to come in a certain order, namely clockwise or anticlockwise: It was the reason for example to use the trigonometry approach in the other thread, increasing the angle to find the x and y coordinates of the 4 corners using cos/sin. If you do it that way, the four corner vertices will always be in the right (counter) clockwise order.