# How do I change the random color for each petal of the flower?

please format code with </> button * homework policy * asking questions

//The constants needed in the equations for the “rose”
final float K = 4; //The factor that controls the number of “petals”
final float RATE = 0.02; //The change in t each frame. Controls speed and smoothness.
final float ROSE_RADIUS = 200; //The radius, in pixels, of the “rose”

//The “state” variables that remember what’s happening
float t = 0; //The “parameter” t which will slowly change from 0 to 2PI
float prevX; //What were x…
float prevY; //…and y last time?

boolean movingToward = true;//the line is moving toward the center, otherwise it’s moving away from center

void setup() {
size(500, 500); //Usual window size
background(0); //Use a black background, and…
strokeWeight(2); //nice thick lines

//Precalculate the first point (with t=0). This must be done
//after the size command because width and height are needed.
prevY=height/2;
}

void draw() {
//Calculate the new point
float y = cos(K

if (x > width/2 && y < height/2) {
movingToward = !movingToward;
}

if (movingToward) {
stroke(random(255), random(255), random(255));
}

line(prevX, prevY, x, y); //draw the next line

//Keep all the information needed for next time
prevX = x;
prevY = y;

t += RATE;
}//draw

prior to this say

`stroke(random(255),random(255),random(255));`

err… I just understood your question.

You need to change the color when you are in the center

``````
// Petal.
// Registers when drawing is at the center and changes color then

//The constants needed in the equations for the “rose”
final float K = 4; //The factor that controls the number of “petals”
final float RATE = 0.02; //The change in t each frame. Controls speed and smoothness.
final float ROSE_RADIUS = 200; //The radius, in pixels, of the “rose”

//The “state” variables that remember what’s happening
float t = 0; //The “parameter” t which will slowly change from 0 to 2PI
float prevX; //What were x…
float prevY; //…and y last time?

boolean changeColor = true;//the line is moving toward the center, otherwise it’s moving away from center
boolean allowed = true;

void setup() {
size(500, 500); //Usual window size
background(0); //Use a black background, and…
strokeWeight(2); //nice thick lines

//Precalculate the first point (with t=0). This must be done
//after the size command because width and height are needed.
prevY=height/2;
}

void draw() {
//Calculate the new point

// When we are at the center we set a marker
//if (x == width/2 && y == height/2) {
if (allowed && dist(x, y, width/2, height/2)<8.92) {
println("here");
changeColor = true;
allowed = false;
}

if (changeColor) {
if (random(1) > 0.9)
stroke(random(222, 255), random(0, 25), random(0, 5));
else
stroke(random(100, 255), random(90, 255), random(100, 255));
changeColor = false;
}

if (dist(x, y, width/2, height/2)>144.92) {
allowed = true;
}

//draw the next line
line(prevX, prevY, x, y);

//Keep all the information needed for next time
prevX = x;
prevY = y;

t += RATE;

}//draw

``````

I am a beginner and I stuck on this for a whole day. Now I understand how things worked! Thanks, my life saver.

1 Like

Yeah, the formula never gives you exact width/2,height/2 therefore I use the dist() command

Also I use “`allowed`” as a 2nd boolean var to avoid that the Sketch runs the random color statement multiple times during one movement through the center

1 Like

Found a question, why do you use if (random(1) >0.9), what’s the meaning by using this condition?

Good question!

I wanted to make your colors more beautiful.

First in the else part

makes for colors more bright (random starts at 100)

Then in 10% of the cases - that’s what the if clause does - we choose a color almost red (with only a small amount of randomness)

Can you please explain how did you calculate and compare the numbers (8.92 and 144.92) with distance. Thanks in advance.

The small number is used to test if the number is near the center (the values are never exactly 0)

The bigger number is to check if we are far outside of a petal

Both values are found by trial and error

There might be more elegant ways, maybe cos(t) is zero at one point

1 Like