Nested Loops

Good Evening everyone,

I quite new to processing and am working on learning nested loops. I am trying to put "Birds onto wires" and have only got it to half work.

    final int BIRD_HEIGHT = 10;
    final int BIRD_DIAM = 20;
    final int HORIZONTAL_LINES = 7;

size(500,500);
background(255);

float wireHeight = (height-1) / (HORIZONTAL_LINES-1.0); // height of one row
float wireY; // y-coordinate of the current horizontal grid line

float birdSpacing = random((width/8),(width/2));

for ( int i = 0; i < HORIZONTAL_LINES; i++ ) {
  wireY = i * wireHeight;
  line (0, wireY, width, wireY);
  for(float j = 0; j < birdSpacing; j++){
    j = j + birdSpacing;
    ellipse(j,wireY-BIRD_HEIGHT,BIRD_DIAM,BIRD_DIAM);

  }
}

``

I have gotten the code to work to make one bird on each wire in the exact same place but its supposed to put a different number of birds on each wire with different spacing... any tips on how to change it? Any advise welcome :D

Answers

  • Answer ✓

    in line 17 you should use a new variable xBird instead of j; use it in line 18 too instead of j

    also line 16 is completely wrong

    try ...... j<width; j+=birdSpacing) {

  • @Chrisir Thank you that definitely helps me!!

  • There are different ways of doing this

    Your outer for loop is perfect, counting with i++ until the number of wires, then in line 14 calculating the position with i*

    The inner for loop could follow the same principle

Sign In or Register to comment.