Use of for-loop to create a stair of ellipses

螢幕截圖 2020-12-16 上午12.12.14

Do someone know how to create the pattern with loops?

Of course.

But please: Use two for loops and show your attempt

Hey! And welcome to the forum! Great to have you here!

Warm regards,

Chrisir

2 Likes

Hi Chrisir

Great to meet you!
I have tried to use two loops but…

Jay

1 Like

Yeah, you got it almost…

think again: How often do you want one row to be filled on the screen?

Think of a chess board with rows and columns.

you want to draw a circle row times and in each row column times

That’s mean I need to draw circle in each row/ column ?

Yes, that does it mean

Chrisir

Thank you very much!

Maybe next time I should clarify clearly about the way of drawing.

show your entire code, does it work now?

Your graphic was pretty clear, I just didn’t want to give you the solution right away but let you find the solution on your own… apologies… :wink:

okay~
long and repetitive code XDD

No, no…

when you have several times almost the same three lines… think again…

You need two for-loops, one for-loop for the rows and in it one for-loop for the columns…

and only ONE line with ellipse() command…

1 Like

Your teaching and inspiration is very good~

1 Like

Here is my way of doing it

That’s what I thought you meant first…

:wink:


float angle=1.4;

void setup() {
  size( 1800, 900, P3D );
} // setup

void draw() {
  background(0);
  lights();

  translate( width/2, 0, -300 );  
  rotateY(angle); 
  angle+=0.014;

  stair();

  drawFloor();

  //
} // draw

//-----------------------------------------------------------------

void stair() {
  //
  int x2=300;

  for (int x = height-111-44; x > height-111-633; x -= 66) {
    pushMatrix();

    stroke(111); 
    fill(0, 2, 222);  // gray 
    // fill(222); 
    //noFill();

    translate ( x2, x, 0);
    x2-=66; 
    rotateX(radians(90)); 
    // box(66); 
    ellipse(0, 0, 
      180, 77); 
    popMatrix();
  }//for
  //
}

void drawFloor() {

  stroke(111);  // gray 
  fill(255, 2, 2); // red
  float factor1=80.0; // dist between boxes

  translate(width/2, height/2, 0); 

  for (int x=-55; x<55; x+=2) {
    for (int z=-55; z<55; z+=2) {

      pushMatrix(); 
      translate(x*factor1 - (4*factor1/2), 
        height-111, 
        z*factor1 - (4*factor1/2) );
      box(factor1-5);  // size of 40 (width, height and depth) 
      popMatrix();
    }
  }
}
//

just a joke…

that won’t help you…

seriously your draw() only needs 8 lines approx.

XD
It seems something about the relation between x and y

one more…


float angle=1.4;

void setup() {
  size( 1800, 900, P3D );
} // setup

void draw() {
  background(0);
  lights();

  translate( width/2, 0, -300 );  
  rotateY(angle); 
  angle+=0.014;

  stair();

  drawFloor();

  //
} // draw

//-----------------------------------------------------------------

void stair() {
  //
  int x2=300;
  int a1=0; 

  for (int y = height-111; y > height-111-633-333-333; y -= 22) {
    pushMatrix();

    a1+=20;
    int r= 333; 

    float x1= cos(radians(a1)) * r;  
    float z1= sin(radians(a1)) * r;

    stroke(111); 
    fill(0, 2, 222);  // gray 
    // fill(222); 
    //noFill();

    translate ( x1, y, z1);

    rotateX(radians(90)); 
    // box(66); 
    ellipse(0, 0, 
      180, 111); 
    popMatrix();
  }//for
  //
}

void drawFloor() {

  stroke(111);  // gray 
  fill(255, 2, 2); // red
  float factor1=80.0; // dist between boxes

  translate(width/2, height/2, 0); 

  for (int x=-55; x<55; x+=2) {
    for (int z=-55; z<55; z+=2) {

      pushMatrix(); 
      translate(x*factor1 - (4*factor1/2), 
        height-111, 
        z*factor1 - (4*factor1/2) );
      box(factor1-5);  // size of 40 (width, height and depth) 
      popMatrix();
    }
  }
}
//

He’s screaming nested loops :blush:
Nested loops are confusing initially, take your time @Jayda

I appreciate your teaching style @Chrisir, loved your passion!

1 Like

Ha ha, tried not to reveal it too early… :wink:

1 Like