Help with the draw function
in
Programming Questions
•
2 years ago
So I'm doing a project for my class and right now I have a looping function to draw a flower that switches between two petal types when you click on the flower's center. One petal type is longer and a different color. When I switch to the second petal type the original petals stay there and I want them to dissapear when I switch between the two. How do I erase what I've drawn? I can't use a background to cover it up, because it will keep drawing it after each individual petal.
My code:
float z;
boolean drawOn, drawNext;
void setup() {
smooth();
size(450, 450);
background(31, 199, 216);
fill(26, 113, 9);
rect(-5, 370, 500, 100);
fill(2, 26, 0);
rect(215, 170, 20, 245);
}
void draw() {
frameRate(30);
fill(152, 78, 8);
ellipse(225, 160, 80, 80);
if (drawOn) {
curlyB();
circle();
}
if (drawNext) {
curlyR();
circle();
}
}
void curlyR() {
float r, cx, cy, center_x, center_y, dimension, t, s;
pushMatrix();
translate(225, 160);
rotate(z);
z = z + PI/30;
dimension = 20;
r = 20;
center_x = center_y = 0;
t = 0;
s = 0;
fill(random(255), random(0), random(0));
for (int i=0; i<100; i++) {
t = t+1;
s = s+1.3;
cx = center_x +r*cos(t) +s;
cy = center_y +r*sin(t);
ellipse(cx, cy, dimension, dimension);
}
popMatrix();
}
void curlyB() {
float r, cx, cy, center_x, center_y, dimension, t, s;
pushMatrix();
translate(225, 160);
rotate(z);
z = z + PI/30;
dimension = 20;
r = 20;
center_x = center_y = 0;
t = 0;
s = 0;
fill(random(0), random(0), random(255));
for (int i=0; i<100; i++) {
t = t+1;
s = s+1;
cx = center_x +r*cos(t) +s;
cy = center_y +r*sin(t);
ellipse(cx, cy, dimension, dimension);
}
popMatrix();
}
void circle() {
fill(152, 78, 8);
ellipse(225, 160, 81, 81);
}
void mousePressed() {
if (mouseX > 185 && mouseX < 265 && mouseY > 120 && mouseY < 200) {
if (drawOn) {
drawOn = false;
drawNext = true;
}
else {
drawOn = true;
drawNext = false;
}
}
}
My code:
float z;
boolean drawOn, drawNext;
void setup() {
smooth();
size(450, 450);
background(31, 199, 216);
fill(26, 113, 9);
rect(-5, 370, 500, 100);
fill(2, 26, 0);
rect(215, 170, 20, 245);
}
void draw() {
frameRate(30);
fill(152, 78, 8);
ellipse(225, 160, 80, 80);
if (drawOn) {
curlyB();
circle();
}
if (drawNext) {
curlyR();
circle();
}
}
void curlyR() {
float r, cx, cy, center_x, center_y, dimension, t, s;
pushMatrix();
translate(225, 160);
rotate(z);
z = z + PI/30;
dimension = 20;
r = 20;
center_x = center_y = 0;
t = 0;
s = 0;
fill(random(255), random(0), random(0));
for (int i=0; i<100; i++) {
t = t+1;
s = s+1.3;
cx = center_x +r*cos(t) +s;
cy = center_y +r*sin(t);
ellipse(cx, cy, dimension, dimension);
}
popMatrix();
}
void curlyB() {
float r, cx, cy, center_x, center_y, dimension, t, s;
pushMatrix();
translate(225, 160);
rotate(z);
z = z + PI/30;
dimension = 20;
r = 20;
center_x = center_y = 0;
t = 0;
s = 0;
fill(random(0), random(0), random(255));
for (int i=0; i<100; i++) {
t = t+1;
s = s+1;
cx = center_x +r*cos(t) +s;
cy = center_y +r*sin(t);
ellipse(cx, cy, dimension, dimension);
}
popMatrix();
}
void circle() {
fill(152, 78, 8);
ellipse(225, 160, 81, 81);
}
void mousePressed() {
if (mouseX > 185 && mouseX < 265 && mouseY > 120 && mouseY < 200) {
if (drawOn) {
drawOn = false;
drawNext = true;
}
else {
drawOn = true;
drawNext = false;
}
}
}
1