用 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解决方案

对你的上述程序进行一些小的更改,结果可能会出现这种现象(自定义颜色并更改一些值)

广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP