用 Python 生成葉序圖案?


什麼是葉序圖案?

當我們回顧植物學課程和植物世界時,葉序是指植物莖上花朵、葉子或種子的排列方式,類似於斐波那契螺旋。根據斐波那契序列,斐波那契螺旋是一組數字,其規律類似於楊輝三角。斐波那契序列的數字類似於 - 1、1、2、3、5、8、13、21、34、55、89、144 等。因此,斐波那契序列的數字是其前幾個數字的和。

斐波那契螺旋

我們通常尋找對稱性和規律來了解周圍的事物。不知不覺中,我們的眼睛看到了斐波那契數列,或者在向日葵花盤的情況下,看到了斐波那契螺旋。

解決方案


向日葵螺旋

示例程式碼

import math
import turtle

def PhyllotacticPattern( t, petalstart, angle = 137.508, size = 2, cspread = 4 ):
   """print a pattern of circles using spiral phyllotactic data"""
   # initialize position
   turtle.pen(outline=1,pencolor="black",fillcolor="orange")
   # turtle.color("orange")
   phi = angle * ( math.pi / 180.0 )
   xcenter = 0.0
   ycenter = 0.0

   # for loops iterate in this case from the first value until < 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 that position and draw
      turtle.up()
      turtle.setpos(x,y)
      turtle.down()
      # orient the turtle correctly
      turtle.setheading(n * angle)
      if n > petalstart-1:
         #turtle.color("yellow")
         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(25)
   turtle.forward(100)
   turtle.left(45)
   turtle.forward(100)
   turtle.left(140)
   turtle.forward(100)
   turtle.left(45)
   turtle.forward(100)
   turtle.up()
   turtle.end_fill() # this is needed to complete the last petal

turtle.shape("turtle")
turtle.speed(0) # make the turtle go as fast as possible
PhyllotacticPattern( 200, 160, 137.508, 4, 10 )
turtle.exitonclick() # lets you x out of the window when outside of idle

解決方案

對你的上述程式進行一些小的更改,結果可能會出現這種現象(自定義顏色並更改一些值)

更新時間:30-7-2019

308 次瀏覽

開啟您的 職業生涯

完成該課程可獲得認證

入門
廣告