Movatterモバイル変換


[0]ホーム

URL:


Open In App
Next Article:
Creating start Menu in Pygame
Next article icon

In this article, we will create a Rhodonea Curve and Maurer Rose pattern in Python! Before we proceed to look at what exactly is arhodonea curve ormaurer 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 - 
 

Python3
frommathimportsinfrommathimportcosfrommathimportradiansimportpygame# The width and height of our program(width,height)=(800,600)# Setup the window and init the screen surfacescreen=pygame.display.set_mode((width,height))pygame.display.set_caption('Rose Curve in Python !')# The background color of the screenscreen.fill((250,250,205))# lemonChiffon color# Our function for drawing the rose patterndrawPattern()# Flip the drawn canvas with the newly created canvas (double-buffering)# Basically we're refreshing the screen surface after drawingpygame.display.flip()# Our Main LoopwhileTrue:# Poll the events in the event queueforeventinpygame.event.get():# if the user closed the windowifevent.type==pygame.QUIT:# deactivate pygame and quit the programpygame.quit()quit()# Draws the surface object to the screen.pygame.display.update()

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 areRhodonea curves!
 

What is a Rhodonea Curve?


Rhodonea Curve (also known as therose in mathematics) is basically a curve which can be expressed in the form r= cos(n\theta )   where n   is an integer defining the number of petals (2n   if n   is even and n   when odd)
Confused ? See this image comparing a rose with 12 petals versus a rose with 5 petals - 
 


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 (x, y)   each in parametric equation form as shown below: 
x= rcos(\theta ), y= rsin(\theta )   
 

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 yousee it in action. Just add the following in line 17 (right above where we invoked drawPattern):
 

Python3
# Draws a rose with n petals and of radius size about `size`defdrawRhodoneaCurve(n,size):points=[]foriinrange(0,361):# The equation of a rhodonea curver=size*sin(radians(n*i))# Converting to cartesian co-ordinatesx=r*cos(radians(i))y=r*sin(radians(i))list.append(points,(width/2+x,height/2+y))# Draws a set of line segments connected by set of vertices points# Also don't close the path and draw it black and set the width to 5pygame.draw.lines(screen,(0,0,0),False,points,5)defdrawPattern():# Try changing these values to what you wantdrawRhodoneaCurve(12,200)

This will produce the following result - 
 


Explanation
 


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 andhere'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 r= cos(n\theta )   consisting of 361 lines connected by 361 points - each of the form ( sin(nk), k)   for every k   in the set \left \{ 0, d, 2d, 3d, ..., 360d \right \}   , where d   is an integer.
Varying values of d   can create varying types of roses even if they all have the same number of petals (meaning same value of n   )
Now the cartesian coordinates will be of the form:- \left \( x= rcos(k ), y= rsin(k ) \right \)   
 

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:-
 

Python3
# Draws a maurer rose with value n and d it's size about `size`defdrawMaurerRose(n,d,size):points=[]foriinrange(0,361):# The equation of a maurer rosek=i*dr=size*sin(radians(n*k))# Converting to cartesian co-ordinatesx=r*cos(radians(k))y=r*sin(radians(k))list.append(points,(width/2+x,height/2+y))# Draws a set of line segments connected by set of vertices points# Also don't close the path and draw it black and set the width to 5pygame.draw.lines(screen,(0,0,0),False,points,5)defdrawPattern():# Try changing these values to what you wantdrawMaurerRose(6,79,200)

And now when you run the program you should get the following result -
 


Explanation:
Only the highlighted lines have been changed rest everything is same -
 

  • The function name has been changed to drawMaurerRose from drawRhodoneaCurve and now expects an extra parameter d!
  • We now introduce a new variable k which actually comes from the formula. It's value at each iteration is equal to i*d, so for d=1 the function is exactly the same as drawRhodoneaCurve!!
  • 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 drawMaurerRose function with n=6 and d=71. The values were copied from WikiPedia since random value of d can sometimes produce weird looking roses!


References: 
https://en.wikipedia.org/wiki/Rose_(mathematics) 
https://www.geeksforgeeks.org/python/degrees-and-radians-in-python/ 
https://en.wikipedia.org/wiki/Maurer_rose 
https://mathworld.wolfram.com/MaurerRose.html
 


Similar Reads

We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood ourCookie Policy &Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences

[8]ページ先頭

©2009-2025 Movatter.jp