using a random color only once
in
Programming Questions
•
2 years ago
Hi all, new to processing but having great fun trying out a few basic building blocks on my fist project.
the area I'm stuck with and would appreciate some pointers is I've created the following code to generate 4 colors, then assign a random color to draw 4 blobs. The code is working as I expected but I want to use each of the colors ONLY ONCE hence to have all 4 colors in a different pattern.
I suspect I need to use 'if' or 'while' to test if the first 2 are the same then if the 3rd is same as 1 or 2 etc. I've tried this to no avail so any pointer are welcome.
color cr=color(255,0,0); //defines RED
color cg=color(0,255,0); //defines GREEN
color cb=color(0,0,255); //defines BLUE
color cy=color(255,255,0); //defines YELLOW
color[] colors = { //random selects one of above colors
cr,cg,cb,cy
};
color c1=(colors[int(random(0,4))]); //assigns a random color from above to c1-4
color c2=(colors[int(random(0,4))]);
color c3=(colors[int(random(0,4))]);
color c4=(colors[int(random(0,4))]);
void setup() {
size (420,100); //screen size
}
void draw() { //draws 4 ellipses with colors above
fill (c1);
ellipse (60,height/2,80,80);
fill (c2);
ellipse (160,height/2,80,80);
fill (c3);
ellipse (260,height/2,80,80);
fill (c4);
ellipse (360,height/2,80,80);
}
the area I'm stuck with and would appreciate some pointers is I've created the following code to generate 4 colors, then assign a random color to draw 4 blobs. The code is working as I expected but I want to use each of the colors ONLY ONCE hence to have all 4 colors in a different pattern.
I suspect I need to use 'if' or 'while' to test if the first 2 are the same then if the 3rd is same as 1 or 2 etc. I've tried this to no avail so any pointer are welcome.
color cr=color(255,0,0); //defines RED
color cg=color(0,255,0); //defines GREEN
color cb=color(0,0,255); //defines BLUE
color cy=color(255,255,0); //defines YELLOW
color[] colors = { //random selects one of above colors
cr,cg,cb,cy
};
color c1=(colors[int(random(0,4))]); //assigns a random color from above to c1-4
color c2=(colors[int(random(0,4))]);
color c3=(colors[int(random(0,4))]);
color c4=(colors[int(random(0,4))]);
void setup() {
size (420,100); //screen size
}
void draw() { //draws 4 ellipses with colors above
fill (c1);
ellipse (60,height/2,80,80);
fill (c2);
ellipse (160,height/2,80,80);
fill (c3);
ellipse (260,height/2,80,80);
fill (c4);
ellipse (360,height/2,80,80);
}
1