Hello,
This seems like a cool project.
If by intermingle you mean 50 red and 50 dark-red randomly positioned across the entire screen.
you could use something similar to having a while loop using a counter till it reaches 50 and inside store a random value ranging from 0-100,There can be duplicate values so only increment the counter value if the color of the object is not red(assuming you choose to fill red first).this way you can be sure that you will have exactly 50 jelly beans which are red and are randomly located , filling the remaining 50 is easy, you just need to check if the object has a color already.This might help you.
Hello everyone!
I tried implementing the while loop suggestion from @Capt.Sloth above by placing it in the display() function in the class. But this didn’t work. Or rather I couldn’t figure out how to get this to work.
So next I am trying to divide and initialize my jellybean array into 2 halves. Then adding a color variable to the constructor for each half. This still is not working. But am I on the right train of thought?
Jellybean [] jellybeans = new Jellybean [100];
void setup() {
size(640, 360);
for (int i = 0; i < jellybeans.length/2; i++) {
jellybeans [i] = new Jellybean (random(width), random(height), random(20, 25), random(.15, 2.05), random(.75, 1.95), #E80F07);
}
for (int i = 50; i >= jellybeans.length/2 && i < jellybeans.length; i++) {
jellybeans [i] = new Jellybean (random(width), random(height), random(20, 25), random(.15, 2.05), random(.75, 1.95), #A71C18);
}
}
void draw() {
background(255);
for (int i = 0; i < jellybeans.length/2; i++) {
jellybeans[i].display();
jellybeans[i].spin();
jellybeans[i].move();
}
for (int i = 50; i >= jellybeans.length/2 && i < jellybeans.length; i++) {
jellybeans[i].display();
jellybeans[i].spin();
jellybeans[i].move();
}
}
class Jellybean {
float x;
float y;
float r;
float angle = random(0.95);
float angleSpeed = random(.01, .2);
float xSpeed;
float ySpeed;
color a;
Jellybean(float tempX, float tempY, float tempR, float tempXspeed, float tempYspeed, color tempA) {
x = tempX;
y = tempY;
r = tempR;
xSpeed = tempXspeed;
ySpeed = tempYspeed;
a = tempA;
}
void display() {
noStroke();
smooth();
//fill(red);
pushMatrix();
translate(x, y);
rotate(angle);
ellipse(0, 0, r*2, r);
popMatrix();
}
void spin() {
angle = angle + angleSpeed;
angle %= TWO_PI;
}
void move() {
x = x + xSpeed;
y = y + ySpeed;
if ((x > width) || (x < 0)) {
xSpeed = xSpeed * -1;
}
if ((y > height) || (y<0)) {
ySpeed = ySpeed * -1;
}
}
}
Thank you @Chrisir!
The if/else random solution is good for now. Though it’s good to know there’s a more precise solution to use when I’m further along in my coding knowledge.
Much appreciated!
Just to focus in on the shuffled color arrays issue (separate from the Jellybeans) – here is the basic idea of how to use shuffling on an ArrayList or IntList and convert it (if needed) to an array.
size(200, 200);
// make a shuffled list of 10 red, 10 blue
IntList colors = new IntList();
for (int i=0; i<10; i++) {
colors.append(color(255, 0, 0));
}
for (int i=0; i<10; i++) {
colors.append(color(0, 0, 255));
}
// draw from an array copy
int[] colorArray = colors.array();
for (int i=0; i<colorArray.length; i++) {
fill(colorArray[i]);
ellipse(i*10, width/2, 10, 10);
}
// draw from the original IntList, shuffled first
translate(0, 20);
colors.shuffle();
for (int i=0; i<colors.size(); i++) {
fill(colors.get(i));
ellipse(i*10, width/2, 10, 10);
}