In this article, we will create a Rhodonea Curve and Maurer Rose pattern in Python! Before we proceed to look at what exactly is a rhodonea curve or maurer rose we need to get the basic structure of our program ready!
Basic Structure of the Program –
Before we move on to learn anything about Rhodonea curves or Maurer rose patterns, we first need to get the basic structure of our program ready. So that when we refactor our code we will only modify a function while the rest of the program will be the same. So here’s the basic structure of the program –
If you run the code, you will get an error that
drawPattern is not defined. We’ll define it later. But for now just let it be and let’s move on to understand what are Rhodonea curves!
What is a Rhodonea Curve?
Rhodonea Curve (also known as the rose in mathematics) is basically a curve which can be expressed in the form where is an integer defining the number of petals ( if is even and when odd)
Note that the coordinates in the equation above are in polar form. But since in computer graphics we express pixel locations in cartesian form rather than polar form, the curve has to re-described as a set of all points each in parametric equation form as shown below:
Getting our hands dirty with Rose Curves
The above was just the theory and in case you didn’t understand the concept completely you will after you see it in action. Just add the following in line 17 (right above where we invoked
- We calculate the vertices (each significant point on the rose which are later connected by edges which are simply line-segments) using the formula we discussed in the above section
- We store a list called
pointswhich has all the vertices of the rose and later this list of vertices is feeded to
pygame.draw.lineswhich draws a sequence of continuous straight lines and here we use it to draw polygonal chain (since a rose is just a polygonal chain)
- Note that we are using
radiansmethod here. This is because the angles are in degree format (we could have directly used radians but range expects only integers, also for maurer curves we’ll anyways have to use degrees so why not start from now) – hopefully we didn’t have to do any conversion ourselves as Python3 provides built-in functions degrees and radians.
- Also note that we are shifting the coordinates by
(width/2, height/2). This is because by default PyGame has its coordinates in Top-Left corner. But we want it in the center so we shift it by half the screen width and height. We could have translated the whole coordinate system but it’s easier this way!
A note about making the program more interactive:
The program currently isn’t that interactive but you can make it interactive by perhaps increasing the number of petals by a fractal amount every frame and depending on how fast/slow you increment you can get pretty awesome results. Making the program interactive is out of scope (since that’d’ significantly increase the LOC count and perhaps also increase the complexity of the program and violate the basic structure that we created in the beginning) but I experimented a bit and here‘s the result of first creating a variable
n and then incrementing it by 0.1 at every frame!
Now What is a Maurer Rose?
Now that we know about Rhodonea Curves let’s proceed to Maurer Rose:-
A Maurer rose is a rhodonea curve of the form consisting of 361 lines connected by 361 points – each of the form for every in the set , where is an integer.
Varying values of can create varying types of roses even if they all have the same number of petals (meaning same value of )
Now the cartesian cordinates will be of the form:-
Getting our hands dirty with Maurer Roses
So to make something out of what we just learned, we over-write the previous
drawPattern function with the new one:-
And now when you run the program you should get the following result –
Only the highlighted lines have been changed rest everything is same –
- The function name has been changed to
drawRhodoneaCurveand now expects an extra parameter
- We now introduce a new variable
kwhich actually comes from the formula. It’s value at each iteration is equal to
i*d, so for
d=1the function is exactly the same as
- We now use k instead of i and it should be noted that the line width has been changed to 2 from 5 (otherwise some lines would appear as one because of their high thickness).
- And the invoke statement has ofcourse been changed as well, it now calls the newly created
d=71. The values were copied from WikiPedia since random value of
dcan sometimes produce weird looking roses!
- Make filled polygons between two curves in Python using Matplotlib
- Python - Read blob object in python using wand library
- MySQL-Connector-Python module in Python
- Important differences between Python 2.x and Python 3.x with examples
- Python | Merge Python key values to list
- Python | Index of Non-Zero elements in Python list
- Python | Convert list to Python array
- Python | PRAW - Python Reddit API Wrapper
- Reading Python File-Like Objects from C | Python
- Python | Set 4 (Dictionary, Keywords in Python)
- Python | Add Logging to a Python Script
- Python | Sort Python Dictionaries by Key or Value
- Python | Add Logging to Python Libraries
- Python | Visualizing O(n) using Python
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.