Rodonei Curves and Rosa Maurer in Python

Python Methods and Functions

In this tutorial, we will create the Rhodonea Curve and Maurer Rose pattern in Python! Before we get into what exactly is a Rodoneus Curve or Maurer`s Rose, we need to prepare the basic structure of our program!




Basic structure of the program —

Before we continue exploring the Rodonei curves or the Maurer rose patterns, we first need to prepare the basic structure of our program. So when we refactor our code, we will only change the function, while the rest of the program will be the same. So here is the main structure of the program —

from math import sin

from math import cos

from math import radians

 

import pygame

 
# Width and height of our program

(width, height) = ( 800 , 600 )

 
# Customize the window and initialize the screen surface

screen = pygame.display.set_mode ((width, height))

  

pygame.display.set_caption ( `Rose Curve in Python!` )

 
# Screen background color

screen.fill (( 250 , 250 , 205 )) # lemonChiffon color

 
# Our function for drawing a picture of a rose < / code>
drawPattern ()

 
# Flip the drawn canvas with the newly created canvas (double buffering)
# Basically we update the screen surface after painting
pygame.display.flip ()

 
# Our main loop

while True

 

# Polling events in the event queue

for event in pygame.event.get (): 

 

  # if the user has closed the window

  if event. type = = pygame.QUIT: 

 

# deactivate Pygame and exit

  pygame.quit () 

quit () 

 

# Draws the surface of the object on the screen.

pygame.display.update ( ) 

If you run the code you get e error that drawPattern is undefined. We`ll define this later. But for now, just let it be and let`s move on to understand what Rodonea Curves are !




What is Rodonea Curve?

Rodonea Curve (also known like math in the form of rose ) — it is basically a curve that can be expressed as where an integer specifying the number of petals ( if even and when strange)

Confused? Take a look at this image comparing the 12 petal rose and the 5 petal rose —

Note that the coordinates in the equation above are in polar form. But since in computer graphics we express the arrangement of pixels in a Cartesian rather than a polar form, the curve must be rewritten as a set of all points each in the form parametric equation as shown below:




Dirty hands with Rose Curves

The above was just theory, and in case you don`t fully understand the concept, you will understand when you see it in action . Just add the following to line 17 (right above where we called drawPattern ):

# Draws a rose with n petals and a radius about `size`

def drawRhodoneaCurve (n, size):

points = []

  for i in range ( 0 , 361 ):

# Rodonei curve equation

  r = size * sin (radians (n ​​ * i))

 

# Convert to Cartesian coordinates

x = 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 many vertices

  # Also, do not close the path and do not draw its black and set the width to 5

pygame.draw.lines (screen, ( 0 , 0 , 0 ), False , points, 5 )

 

def drawPattern ():

# Try changing these values ​​to whatever you want

drawRhodoneaCurve ( 12 , 200 )

This will give the following result —

explanation

Get Solution for free from DataCamp guru