Python Methods and Functions

## Basic structure of the program —

## What is Rodonea Curve?

* *

## Dirty hands with Rose Curves

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!

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 —

` ` |

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 **!

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:

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 `

):

` ` |

This will give the following result —

** explanation**

- We compute vertices (each significant point on a rose that is later connected by
*edges,*which are just line segments ) using the formula we discussed in the previous section - We keep a list named
`points`

which contains all the vertices of the rose, and later this vertex list is fed into`pygame.draw.lines`

which draws a sequence of continuous straight lines, and here we use it to draw a polygonal chain (since the rose is just a polygonal chain) - Note that we are using the
`radians here. This is because angles are in degree format (we could use radians directly, but the range expects only integers, also for Maurer curves we have to use degrees anyway, so why not start from here) — hopefully we didn`t have to do Any conversion ourselves as Python3 provides built-in functions`

## Now what is Maurer Rose?

Now that we know about the Rodonei Curves, let`s move on to Maurer Rose:

Rose Maurera — this is a rhodonean curve of the form consisting of 361 lines connected by 361 points — each of the forms for each in the set , where is an integer

Varying values can create different types of roses even if they have the same number of petals (which means the same value )

Now the Cartesian coordinates will look like this:

## Dirty hands with Maurer roses

So, to do something from what we just learned, we

`drawPattern`

the previous`drawPattern`

function is new: —`# Draws a Maurer rose with a value of n and d, which is `size``

`def`

`drawMaurerRose (n, d, size):`

`points`

`=`

`[]`

`for`

`i`

`in`

`range`

`(`

`0`

`,`

`361`

`):`

`# Maurer Rose equation`

`k`

`=`

`i < / code>`

`*`

`d`

`r`

`=`

`size`

`*`

`sin (radians (n `

`*`

`k))`

`# Convert to Cartesian coordinates`

`x`

`=`

`r`

`*`

`cos (radians (k))`

`y`

`=`

`r`

`*`

`sin (radians (k))`

`list`

`. append (points, (width`

`/`

`2`

`+`

`x, height`

`/`

`2`

`+`

`y))`

`# Draws a set of lines connected by many vertices`

`# Also, don`t close the path and paint it black and set the width to 5`

`pygame.draw.lines (screen, (`

`0`

`,`

`0`

`,`

`0`

`),`

`False`

`, po ints,`

`5`

`)`

`def`

`drawPattern ():`

`# Try changing these values to whatever you want`

`drawMaurerRose (`

`6`

`,`

`79`

`,`

`200`

`)`

And now when you run the program, you should get the following result —

**Explanation :**Changed only selected lines, everything else —

- Function name was changed to
`drawMaurerRose`

from`drawRhodoneaCurv e`

and now expects an additional parameter`d`

! - We will now introduce a new variable
`k`

which actually comes from a formula. Its value at each iteration is`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 from 2 to 2 (otherwise some lines would look like one because of their thickness).
- And the invoke operator also changed, it now calls the newly created function
`drawMaurerRose`

with`n = 6`

and`d = 71`

. The values were copied from WikiPedia as a random`d`

value can sometimes lead to strange looking roses!

**Links :** - Function name was changed to

This book introduces machine learning methods in finance. It features a unified treatment of machine learn...

12/08/2021

In Learn Python 3 the Hard Way PDF, you'll learn Python by working through 52 brilliantly crafted exercises. Read them. Enter your code exactly. (No copying and pasting!) Correct your mistakes. ...

05/09/2021

Computer languages have so far been of the ‘interpreted’ or the ‘compiled’ type. Compiled languages (like ‘C’) have been more common. You prepare a program, save it (the debugged version),...

23/09/2020

Learning Correct Cryptography by Example. The interconnected world of the current era has drastically changed everything, including banking, entertainment, and even statecraft. Despite difference...

23/09/2020

X
# Submit new EBook