用斐波那契数列绘画银杏树

本文通过结合斐波那契数列和turtle库,创作了一种独特的方法来绘制银杏树。代码中利用随机数和角度调整生成了形态各异的树枝,虽然初始版本的树显得较为稀疏,但通过改进,最终呈现了一棵栩栩如生的艺术作品。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

斐波那契数列的话我之前做过两期,现在做第三期了,若喜欢我的,请三连,谢谢!

import turtle
import random
from math import *  

引入turtle、random和math的所有东西

def Fibonacci_Recursion_tool(n):  #斐波那契数列方法
    if n <= 0:
        return 0
    elif n == 1:
        return 1
    else:
        return Fibonacci_Recursion_tool(n - 1) + Fibonacci_Recursion_tool(n - 2)
def Fibonacci_Recursion(n):     #生成斐波那契数列,并存入列表
    result_list = []
    for i in range(1, n + 3):
        result_list.append(Fibonacci_Recursion_tool(i))
    return result_list
yu = Fibonacci_Recursion(10)  #生成斐波契那数列
print(yu)

生成斐波那契数列,并打印出来(之前讲过)

def leaf(x, y, node):#定义画叶子的方法
    til = turtle.heading()
    i = random.random()
    an = random.randint(10, 180)
    ye = random.randint(6, 9)/10
    turtle.color(ye, ye*0.9, 0)
    turtle.fillcolor(ye+0.1, ye+0.05, 0)
    turtle.pensize(1)
    turtle.pendown()
    turtle.setheading(an + 90)
    turtle.forward(8*i)
    px = turtle.xcor()
    py = turtle.ycor()
    turtle.begin_fill()
    turtle.circle(7.5*i, 120)  # 画一段120度的弧线
    turtle.penup()  # 抬起笔来
    turtle.goto(px, py)  # 回到圆点位置
    turtle.setheading(an + 90)  # 向上画
    turtle.pendown()  # 落笔,开始画
    turtle.circle(-7.5*i, 120)  # 画一段120度的弧线
    turtle.setheading(an + 100)
    turtle.circle(10.5*i, 150)
    turtle.end_fill()  # 画一段150度的弧线
    turtle.penup()
    turtle.goto(x, y)
    turtle.setheading(til)
    turtle.pensize(node / 2 + 1)

til就是当前的角度,i就是一个0到1之间的随机数,an就是10到180之间的随机小数,ye是一个0.6到0.9之间的十位小数。

color就是使用RGB的(red,green,blue),fillcolor一样。

p.xcor就是当前turtle的x坐标,p.ycor 就是当前turtle的y坐标。

def draw(node, length, level, yu, button):  #定义画树的方法
    turtle.pendown()
    t = cos(radians(turtle.heading()+5)) / 8 + 0.25
    turtle.pencolor(t*1.6, t*1.2, t*1.4) #(r, g, b)颜色对应的RGB值
    turtle.pensize(node/1.2)  #画笔的尺寸
    x = random.randint(0, 10)  #生成随机数决定要画树枝还是画飘落的叶子

t = cos(radians(turtle.heading()+5)) / 8 + 0.25是生成一个小数

def draw(node, length, level, yu, button):  #定义画树的方法
    turtle.pendown()
    t = cos(radians(turtle.heading()+5)) / 8 + 0.25
    turtle.pencolor(t*1.6, t*1.2, t*1.4) #(r, g, b)颜色对应的RGB值
    turtle.pensize(node/1.2)  #画笔的尺寸
    x = random.randint(0, 10)  #生成随机数决定要画树枝还是画飘落的叶子
    
    if level == top and x > 10:  #此时画飘落的叶子,x范围太大会导致树太秃
        turtle.forward(length)  # 画树枝,length=120
        yu[level] = yu[level] - 1
        c = random.randint(2, 10)
        for i in range(1, c):
            leaf(turtle.xcor(), turtle.ycor(), node)
           # 添加0.3倍的飘落叶子
            if random.random() > 0.3:
                #print("随机数是",random.random())
                turtle.penup()  #可以用down()测试一下,当大于0.3的时候,会有和落叶的连线。
                #turtle.down()
               # 飘落
                t1 = turtle.heading()
                print("turtle.heading()",t1)# 可以用print看一下t1的值。
                an1 = -40 + random.random() * 40 #random.random()产生0,1之间的浮点数
                turtle.setheading(an1)
                dis = int(800 * random.random() * 0.5 + 400 * random.random() * 0.3 + 200 * random.random() * 0.2)
                 #上面的800 * random.random() * 0.5是
                turtle.forward(dis)
                turtle.setheading(t1)
                turtle.right(90)
               # 画叶子
                leaf(turtle.xcor(), turtle.ycor(), node)
                turtle.left(90)
               # 返回
                t2 = turtle.heading()
                turtle.setheading(an1)
                turtle.backward(dis)
       
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值