We are about to switch to a new forum software. Until then we have removed the registration on this forum.
I have this code that draws a flower-like object on the screen where ever you click. However, I only have four petals on it, so it's kind of a lame flower. My question is about adding more petals, but first here is the code:
int c_center;
int c_petal = 20;
int petalsize = int(random(60, 70));
color rcol;
void setup(){
fullScreen();
background(0);
}
void draw(){
//updates values
rcol = color(random(255), random(255), random(255));
c_center = int(random(10, 22));
}
void mouseClicked(){
flower();
}
void flower(){
//left petal
make_petal(mouseX - (petalsize - c_petal), mouseY, petalsize, c_petal);
//right petal
make_petal(mouseX + (petalsize - c_petal), mouseY, petalsize, c_petal);
//top petal
make_petal(mouseX, mouseY - (petalsize - c_petal), c_petal, petalsize);
//bottom petal
make_petal(mouseX, mouseY + (petalsize - c_petal), c_petal, petalsize);
//flower center
fill(random(255), random(255), random(255));
ellipse(mouseX, mouseY, c_center, c_center);
}
void make_petal(int a, int b, int c, int d){
fill(rcol);
ellipse(a, b, c, d);
}
I was wondering if it was possible to call the petals function again in the mouseClicked function but use some function to rotate it some amount of degrees around the flower center? That way instead of having to code more flower petals I can just rotate the original four (as many times as needed) to get more petals. If you have a better/easier way to achieve this feel free to give such suggestions as well.
Answers
Check out the regular polygon example. Rather than equidistant petals around the edge of a flower, it uses a for loop to create equidistant points around the edge of a shape.
If you use an approach like this -- a for loop -- then you can call flower(4) or flower(10) for four petals or ten petals.
That's cool.
I imagine you can make a star polygon, in which you would just need to draw the central ellipse over it and it would look flower-like as well.
Feel free to share your new code here if you arrive at a solution you like -- or if you get stuck and have further questions.
Here's an example code I did. I'm sure there's a way to achieve this with less lines of code, but it works nevertheless.
As you said, using this you can easily just change the number of points to get various flower-like shapes. I noticed also that you can get the polygon to rotate by some frame rate about an axis, but that it requires that the rotate function be in draw() so to continuously load that rotation. I was trying to figure out a way to do that here that will still allow me to click and have the shape appear, but also allow it to continuously rotate in space.
Like one line very long line?
Reading that makes my eyes hurt...
and this part is a little strange:
rotate(i+=t/((s=(s)?false:true)?8:4))
but it appears to check out.
haha yup, the ? is an if(), gives a warning but no error
Another question on this one about conditional statements:
This outputs a random flower where ever you click, and the randomness (or sort of randomness) is obtained via the variable r. However, occasionally the flowers will overlay, so something about the logic isn't quite right. If I comment out the last else if statement and leave the other three I don't have that problem, so it's something to do with the last one. It's likely something simple, but I'm not seeing it.
You got f1,f2,f3,f4 as objects??? And in each of them draw_flower_1 and draw_flower_2 and draw_flower_3 and draw_flower_4. This in itself is confusing.
Why not only have one function draw_flower ?
And flowers with different positions or types?
What is the difference btw draw_flower_#() functions? It is not possible to reproduce the problem with the code you have provided.
Kf
Post your entire code
The whole code is over multiple tabs. I have four classes, each representing a different shaped flower. f1, f2, f3, & f4 are just instances of each of those flowers. I mean I could put all of the code in one tab, but it's easier to keep up with across multiple tabs.
To address your question, we will need to have a look at the code in the other tabs. As it is right now, the code you presented so far seems all right.
Kf
Okay.
Class 1:
Class 2:
Class 3:
Class 4:
It's likely that there are more efficient and condensed ways to get these shapes, but I'm still new to this...so forgive my novice. They all work nevertheless and give the shapes I desire. Sorry about having to post so much code, but if you find the issue or just have some advice on shortening the code let me know.
I don't see any problem.
What are you referring to? What is not quite right?
Kf
Here's a example. Most of the flowers are fine, but some of the star shaped ones look like they're overlapped by other flowers...or that other flowers are showing through. I'm not sure what it is exactly. That would be in Flower 4.
Oh, jeez I'm stupid. I forgot I set the transparency value for the lines overlaying the flower.
.
Actually, it didn't really help changing it. This is what they're supposed to look like, and it works when I output Flower 4 by itself. But when I output it randomly with the others it looks like the other picture. I don't really know what's going on.
I can see the problem now. Use pushStyle() and popStyle() inside each of your classes before you start drawing the flower and right before you leave the function. Alternatively, you can place it at the beginning and end of your mouse event.
Kf
Works great!
And it's always nice to learn about new functions. I didn't know anything about those. Thank you.
I wanted to add this extra class that has to do with the very first problem I asked about rotating petals around a center point. It was a simple fix, I was just drawing the flower using mouseX and mouseY and then translating with both as well, which made the flower petals off center.
Please provide a MCVE: https://stackoverflow.com/help/mcve
Although a change the constructor, that change is not needed in your case. In fact, you only need function calls to create the flowers unless you want to store the flowers and keep track of them. for example, if you want them to move around.
I added an extra set of push/pop matrices as rotations are accumulative: https://processing.org/tutorials/transform2d/
I also changed the rotation step as that gives me a better result. You might also want to adjust the range of w and p_s when they are initialized.
Kf
Looks good. I left the w and p_s variables in the draw_flower function as it allows the petal size to randomly change every time you draw one.
Sorry, I'm not sure what your first comment is referring to. I was just adding this as a solution to the original question I asked at the top of this post. I made it an extra class to add to the other 4 classes and main function I posted above.