  Phyllotaxis Pattern in Python | Algorithmic Botany Block

Counters | Python Methods and Functions

Introduction | Phyllotaxis

Phyllotaxis / phyllotaxia — it is the arrangement of leaves on the stem of a plant, and phyllotaxic spirals form a special class of patterns in nature. The word itself comes from the Greek phullon, which means leaf, and taxi means arrangement. Major floral phyllotaxic arrangements include:
1. Spiral phyllotaxis — with spiral phyllotaxy, individual flower organs are created at regular intervals with the same flow angle. The divergence angle in the spiral phyllotaxy flower approaches 137.5 degrees, indicating a pattern following the Fibonacci series . The figure below shows phyllotaxy spiral patterns having both clockwise and counterclockwise spiral patterns. Important points for which Note:

1. Fibonacci series usually describe spirals found in nature. It is calculated as a row in which the previous pair of numbers is added to the next number in the row. The series is 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89 ....
2. Actually there is one set of spirals in a clockwise direction and one set in an anti clockwise.
3. Spirals of flower organs follow a set of numbers and denominators of shifted Fibonacci numbers (1/2, 1/3, 2/5, 3/8, 5/13, 8/21, 13/34 ...). Numerator — this is the number of times or rotations around the axis to return to the beginning of initiation. The denominator indicates the number of organs triggered during turns. Therefore, 2/5 will mean 2 rotations around the axis and 5 organs to return to the starting point.
4. For example, in pine (2, 3), (5, 3) and (5, 8) phyllotaxis, the chapters contain pairs (21, 34), (55, 34), (55, 89) and (89, 144), while pineapples with hexagonal scales contain triplets (8, 13, 21) or (13, 21, 34 ) depending on the size of the specimen.
5. The prevalence of the Fibonacci sequence in phyllotaxis is often called the “secret of phyllotaxis.”

Other types of floral phyllotaxic arrangements: 2. Phylotaxis with a whorl, 3. Phyllotaxis with a simple whorl, 4. Phyllotaxis with a complex mutation, and 5. Incorrect phyllotaxis.

Template formation: resume

The excellent arrangement of leaves in some plants, called phyllotaxis, obeys a number of subtle mathematical relationships. For example, the inflorescences in the head of a sunflower form two oppositely directed spirals: 55 of them clockwise and 34 counterclockwise. Oddly enough,

1. These numbers are sequential Fibonacci numbers.
2. Ratios of alternative Fibonacci numbers are set converging to φ ^ (- 2), where φ — Golden Ratio , and they are said to measure the proportion of turnover between successive leaves on a plant stem:
3. for example: 1/2 for elm and linden, 1/3 for beech and hazel, 2/5 for oak and apple, 3/8 for poplar and rose, 5/13 for willow and almond, etc.
4. Each new leaf on the stem of a plant is at a certain angle to the previous one, and this angle is constant between the leaves: usually about 137.5 degrees.

That is, if you look down at plant and measure the angle formed between the line drawn from stem to leaf and the corresponding line for the next leaf, you will find that there is usually a fixed angle called divergence. angle.
Here we are interested in Spiral Phyllotaxy and we will be coding to form the Spiral Phyllotaxy pattern in python using turtle graphics.

Code Development

1. We will code two functions, one to draw the phyllotaxial pattern, and the other to draw the petals.
2. The petals only need to be drawn after the phyllotaxis pattern is complete. So, we will call the drawPetal () function from the drawPhyllPattern () function with the last coordinates x & amp; y that were visited after drawing the phyllotaxis template.
3. The drawPetal () function will draw petals with functions and functions Programming turtles .

To code phyllotaxis pattern, we need to follow these equations:

x = r * cos (θ) y = r * sin (θ) r, θ can also vary - so the to form phyllotactic pattern we substitutethe cartesian form by polar form: r = c * sqrt (n) θ = n * 137.508 ° Reduces the problem to optimal packing on a disc, so r = c * sqrt (n) is from the area of ​​the circle Area = πr² and n fills the Area in some units c1 * n / π = r², c is 1 / sqrt (c1 / π) So, r = some constant c * sqrt (n)

Pseudocode: phyllotaxis

IMPORT MODULES (MATH , TURTLE) FUNCTION - DrawPhyllotaxisPattern (turtle, t length, petalstart, angle = 137.508, size, cspread) turtleColor ("Black") FillColor ('" Orange ") Convert angle to radians (Φ) initialize (xcenter, ycenter) = (0,0 ) Drawing the Pattern Starts: For n in Range (0, t): r = cspread * sqrt (n) θ = n * Φ x = r * cos (θ) + xcenter y = r * sin (θ) + ycenter TURTLE POSITION (x, y) START DRAWING (): if Drawing pattern ends: DrawFlowerPetals () FUNCTION - DrawFlowerPetals (Turtle, x coordinate, y coordinate) DRAW using Turtle methods Create Turtle = gfg Call DrawPhyllotaxisPattern (gfg, t length, petalstart, angle = 137.508, size, cspread) END

Python Pattern A

 import math  import turtle    def drawPhyllPattern (turtle, t, petalstart, angle = 137.508 , size = 2 , cspread = 4 ): "" " print a pattern of circles using spiral phyllotax data»«»   # initialize position # turtle.pen (outline = 1, pencolor = & quot; black & quot ;, fillcolor = & quot; orange & quot; ) turtle.color ( ' black' )   turtle.fillcolor ( "orange" ) phi = angle * (math.pi / 180.0 ) # convert to radians xcenter = 0.0   ycenter = 0.0   In this case # loop repeats from the first value to & lt; 4, so   for n in range ( 0 , t): r = cspread * math.sqrt (n) theta = n * phi   x = r * math.cos (theta) + xcenter   y = r * math.sin (theta) + ycenter   # move the turtle to this position and draw   turtle.up () turtle.setpos (x, y) turtle.down () # correctly orient the turtle turtle.setheading (n * angle) if n & gt; petalstart - 1 : turtle.color ( "yellow" )   drawPetal (turtle, x, y) else : turtle.stamp ()     def drawPetal (turtle, x, y): turtle.penup () turtle.goto (x , y) turtle.pendown () tur tle.color ( 'black' )   turtle.fillcolor ( ' yellow' ) turtle.begin_fill () turtle.right ( 20 ) turtle.forward ( 70 ) turtle.left ( 40 ) turtle.forward ( 70 )   turtle.left ( 140 ) turtle.forward ( 70 )   turtle.left ( 40 ) turtle.forward ( 70 ) turtle.penup () turtle.end_fill () # this is required to complete the last petal     gfg = turtle.Turtle () gfg.shape ( "turtle" ) gfg.speed ( 0 ) # make the turtle move as fast as possible drawPhyllPattern (gfg, 200 , 160 , 137.508 ) gfg.penup () gfg.forward ( 1000 )

Python Pattern B

 import math  import turtle    def drawPhyllotacticPattern (t, petalstart, angle = 137.508 , size = 2 , cspread = 4 ): “” “print a circle pattern using spiral phyllotax data” “” # initialize position   turtle.pen (outline = 1 , pencolor = "black" , fillcolor = "orange" ) # turtle.color ( & quot; orange & quot;) phi = angle * (math.pi / 180.0 ) xcenter = 0.0 ycenter = 0.0   In this case, # the loop repeats from the first value to & lt; 4, so   for n in range ( 0 , t): r = cspread * math.sqrt (n) theta = n * phi      x = r * math.cos (theta) + xcenter y = r * math .sin (theta) + ycenter    # move the turtle to this position and draw turtle.up () turtle.setpos (x, y) turtle.down () # correctly orient the turtle turtle.setheading (n * angle)   if n & gt; petalstart - 1 : # Turtle.color (& quot; yellow & quot;) drawPetal (x, y)   else : turtle.stamp ()      def drawPetal (x, y): turtle.up () turtle.setpos (x, y)   turtle.down () turtle.begin_fill ( )   # Turtle.fill (True)   turtle.pen (outline = 1 , pencolor = "black" , fillcolor = "yellow" ) turtle.right ( 20 ) turtle.forward ( 100 ) turtle.left ( 40 ) turtle.forward ( 100 )   turtle.left ( 140 ) turtle.forward ( 100 ) turtle.left ( 40 ) turtle.forward ( 100 ) turtle.up () turtle.end_fill () # this is required for completion of the last petal       turtle.shape ( "turtle " ) turtle.speed ( 0 ) # make the turtle move as fast as possible drawPhyllotacticPattern ( 200 , 160 , 137.508 , 4 , 10 ) turtle.exitonclick () # allows you to exit the window when it is idle

Conclusion: phyllotaxis patterns. Sources:

This article courtesy of Amartya Ranjan Saikia . If you are as Python.Engineering and would like to contribute, you can also write an article using contribute.python.engineering or by posting an article contribute @ python.engineering. See my article appearing on the Python.Engineering homepage and help other geeks.