Colorful ellipses

edited October 2016 in Questions about Code

Here is the code, please, click a mouse many times. The code is creating big circle consisting of little ellipses, every time the mouse clicked. The question is - how to make every big circle random colored (so, every time mouse clicked, big circle is creating, and its color is certain, and different from another big circle)? I tried, but all I got is all little ellipses are different, or, all the ellipses are the same..

ArrayList<Boid> boids;

void setup()
{
  size(900, 900);
  frameRate(90);
  background(0);
  boids = new ArrayList<Boid>();
}


void draw()
{
  background(0); 
  for (int i = boids.size ()-1; i >= 0; i--)
  { 
    boids.get(i).display();
  }
}


void mousePressed()
{
    int points = int(random(20,100)); //number of points
    float pointAngle = 360/points; //angle between points
    float radius=random(width/2);

    for(float angle = 0; angle < 360; angle = angle+pointAngle) 
    {
      float x = cos(radians(angle)) * radius; //convert angle to radians for x and y coordinates
      float y = sin(radians(angle)) * radius;

      boids.add(new Boid(x+width/2, y+height/2));
    }
}

 class Boid
{
  float rad = 10;
  PVector pos;

  Boid (float xx, float yy) {
    pos = new PVector(0, 0);
    pos.x = xx;
    pos.y = yy;
  }

  void display()
  {
    pushMatrix();
    noStroke();
    translate(pos.x, pos.y);
    ellipse(0, 0, rad, rad);
    popMatrix();
  }
}
Tagged:

Answers

  • Answer ✓

    make color part of class

    use color after line 52

    set a color in a var in line 27

    use it in 33 and 42 and 46

  • I will try, thanks.

  • But if I try to set a color I created before in the class, in var in line 27, there is an error, because this color is outside of the class.

  • After a 10-20 variations of code I finally did it : ) Thanks!

  • Post your entire code please

  • ArrayList<Boid> boids;
    color [] col = new color [100];
    int what=int(random(100));
    
    void setup()
    {
      size(900, 900);
      frameRate(90);
      background(0);
      boids = new ArrayList<Boid>();
    
      for (int i = 0; i <100; i++)
      { 
        col[i]=color(random(255),random(255),random(255));
      }
    }
    
    
    void draw()
    {
      background(0); 
      for (int i = boids.size ()-1; i >= 0; i--)
      { 
        boids.get(i).display();
      }
    }
    
    
    void mousePressed()
    {
        int points = int(random(20,100)); //number of points
        float pointAngle = 360/points; //angle between points
        float radius=random(width/2);
        what=int(random(100));
        for(float angle = 0; angle < 360; angle = angle+pointAngle) 
        {
          float x = cos(radians(angle)) * radius; //convert angle to radians for x and y coordinates
          float y = sin(radians(angle)) * radius;
          color cole1 = col[what];
    
          boids.add(new Boid(x+width/2, y+height/2,cole1));
        }
    }
    
    ///////////////////////////////////////////////////////////////////////////////// 
    
     class Boid
    {
      float rad = 10;
      PVector pos;
      color cole2;
    
      Boid (float xx, float yy,color colein) {
        pos = new PVector(0, 0);
        pos.x = xx;
        pos.y = yy;
        cole2=colein;
      }
    
      void display()
      {
        pushMatrix();
        noStroke();
        translate(pos.x, pos.y);
        fill(cole2);
        ellipse(0, 0, rad, rad);
        popMatrix();
      }
    }
    
  • Nicely done

    You can merge line 1 and 10

    The array of colors in line 2 and 12-15 is not necessary

    Instead in line 34 just set cole1 like in line 14

  • Thanks for help!

  • ArrayList<Boid> boids = new ArrayList<Boid>();
    
    void setup()
    {
      size(900, 900);
      background(0);
    }
    
    void draw()
    {
      background(0); 
      for (int i = boids.size ()-1; i >= 0; i--) 
      { 
        boids.get(i).display();
      }
    }
    
    void mousePressed()
    {
      int points = int(random(20, 100)); //number of points
      float pointAngle = 360/points;     //angle between points
      float radius=random(width/2);
      color cole1 = color(random(255), random(255), random(255));
      for (float angle = 0; angle < 360; angle = angle+pointAngle) 
      {
        float x = cos(radians(angle)) * radius; //convert angle to radians for x and y coordinates
        float y = sin(radians(angle)) * radius;
        boids.add(new Boid(x+width/2, y+height/2, cole1));
      }
    }
    
    ///////////////////////////////////////////////////////////////////////////////// 
    
    class Boid
    {
      float rad = 10;
      PVector pos;
      color cole2;
    
      Boid (float xx, float yy, 
        color colein) {
        // constr 
        pos = new PVector(xx, yy);
        cole2=colein;
      }//constr
    
      void display()
      {
        pushMatrix();
        noStroke();
        translate(pos.x, pos.y);
        fill(cole2);
        ellipse(0, 0, rad, rad);
        popMatrix();
      }
    }//class
    //
    
  • Line 49/54/51 unnecessary

    Just use ellipse with pos.x,pos.y

Sign In or Register to comment.