We are about to switch to a new forum software. Until then we have removed the registration on this forum.
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();
}
}
Answers
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
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!
Line 49/54/51 unnecessary
Just use ellipse with pos.x,pos.y
okay!