How to use nested for-loop to draw a tree

Hi everyone,

Should I use nested triple for loop to do it?

This is my goal:
tree

here is my code :

int x,y,w,h;

void setup() 
{
  background(255);
  size(500, 500);

  x=250;//The center of the starting circle at the top is at position (250, 25)
  y=25;
  w=50;//width and height of ALL circles are 50
  h=50;
}

void draw() 
{
  
  fill(0,255,0);
  for(int j=0;j<=7;j++){
    for(int i=0;i<=j;i++){
   ellipse(x-25*i,y+j*50,w,h);
      for(int k=0;k<=i;k++){
 ellipse(x+25*k,y+j*50,w,h);
      }
    }
  }


}

A nested for-loop with 2 for-loops is enough

one for the rows and the inner for the columns (increase by 1 every new line)

try this first:

*
**
***
****
*****
1 Like

Thank you so much, I finally did it :smile:

1 Like