make an array of concentric evolving circles

edited February 2014 in How To...
How do I write this with an array? 

  //circle
    int circleSize = 1;

     void setup()
    {
    size(420, 420);
    ellipseMode(CENTER);
    }

     void draw()
     {
     circleSize +=1;
      background(0);
      stroke(250);
      fill(0);

     // I want to write only one of these lines below, instead of having to write all of those, and get the same result. 


      ellipse(width/2, height/2, circleSize, circleSize);
      ellipse(width/2, height/2, circleSize/2, circleSize/2);
      ellipse(width/2, height/2, circleSize/3, circleSize/3);
      ellipse(width/2, height/2, circleSize/4, circleSize/4);
      ellipse(width/2, height/2, circleSize/5, circleSize/5);
      ellipse(width/2, height/2, circleSize/6, circleSize/6);
      ellipse(width/2, height/2, circleSize/7, circleSize/7);
      ellipse(width/2, height/2, circleSize/8, circleSize/8);
      ellipse(width/2, height/2, circleSize/9, circleSize/9);
      ellipse(width/2, height/2, circleSize/10, circleSize/10);
      ellipse(width/2, height/2, circleSize/11, circleSize/11);
      ellipse(width/2, height/2, circleSize/12, circleSize/12);
      ellipse(width/2, height/2, circleSize/13, circleSize/13);
      ellipse(width/2, height/2, circleSize/14, circleSize/14);
      ellipse(width/2, height/2, circleSize/15, circleSize/15);
      ellipse(width/2, height/2, circleSize/16, circleSize/16);
      ellipse(width/2, height/2, circleSize/17, circleSize/17);
      ellipse(width/2, height/2, circleSize/18, circleSize/18);
      ellipse(width/2, height/2, circleSize/19, circleSize/19);
      ellipse(width/2, height/2, circleSize/20, circleSize/20);
      ellipse(width/2, height/2, circleSize/21, circleSize/21);
      ellipse(width/2, height/2, circleSize/22, circleSize/22);
      ellipse(width/2, height/2, circleSize/23, circleSize/23);
      ellipse(width/2, height/2, circleSize/24, circleSize/24);
      ellipse(width/2, height/2, circleSize/25, circleSize/25);
      ellipse(width/2, height/2, circleSize/26, circleSize/26);
      ellipse(width/2, height/2, circleSize/27, circleSize/27);
      ellipse(width/2, height/2, circleSize/28, circleSize/28);
      ellipse(width/2, height/2, circleSize/29, circleSize/29);
      ellipse(width/2, height/2, circleSize/30, circleSize/30);
      ellipse(width/2, height/2, circleSize/31, circleSize/31); 
     }

Answers

  • edited February 2014 Answer ✓

    You dont need array. Use for loop instead

    for(int i=1;i<32;i++){
        ellipse(width/2, height/2, circleSize/i, circleSize/i); 
        }
    
  • edited February 2014

    hello,

    small additional remark:

    you could use an array but with the use of OOP (object oriented programming - see tutorial on www.processing.org ), if you know it.

    It would require a class Ellipse that could store the position and size and color of the Ellipse and a method to draw it.

    the Array would the be of type of that class Ellipse

    Each slot in the array would hold one object based on that class Ellipse. Each object would contain several properties for one ellipse (position and size and color).

    You would fill the array with all ellipses in setup() and display it in draw(). Also use a for-loop to loop over the array.

    • (you can also think of an non-OOP approach: 5 independent arrays. Those are parallel arrays: At one position (say 5) all arrays hold different values (x-pos, y-pos, size, color) for the same ellipse (the 5th ellipse if you like) - but OOP is much better)

    Greetings, Chrisir

  • like here....

    Ellipse [] ellipses = new Ellipse [32]; 
    
    //circle
    int circleSize = 1;
    
    void setup() {
      size(600, 600);
      for (int i=1;i<32;i++) {
        ellipses[i] = new Ellipse(width/2, height/2, circleSize, color( 220, 0, 0 ));
        circleSize +=11;
      }
    }
    
    void draw() {
      for (int i=1;i<32;i++) {
        ellipses[i].draw();
      }
    }
    
    // ============
    class Ellipse {
    
      float x;
      float y;
      float size;
      color col;
    
      Ellipse(float x, float y, float size, color col) {
        this.x=x;
        this.y=y;
        this.size=size;
        this.col=col;
      }
    
      void draw() {
        stroke(col);
        noFill();   
        ellipse(x, y, size, size);
      }
    } // class
    // 
    
  • one for-loop in setup() to define, one for-loop in draw() to display

Sign In or Register to comment.