Python递归公式

Python递归公式,python,recursion,turtle-graphics,Python,Recursion,Turtle Graphics,在我正在学习的一个教程中,有一个用不同级别的分支绘制的树的示例代码。我试图编写一个函数,当树的“级别”更改为“由N表示”时,显示分支的数量 在下面的代码中,下面的两行注释显示了我正在努力实现的目标。我完全理解当N增加1时,添加到树中的分支数是3的倍数,但我不理解如何利用函数count\u num\u branchs()在每次递归调用时显示树上的分支数 __author__ = 'Python Tutotial' from math import cos, sin, radians, pi imp

在我正在学习的一个教程中,有一个用不同级别的分支绘制的树的示例代码。我试图编写一个函数,当树的“级别”更改为“由N表示”时,显示分支的数量

在下面的代码中,下面的两行注释显示了我正在努力实现的目标。我完全理解当N增加1时,添加到树中的分支数是3的倍数,但我不理解如何利用函数
count\u num\u branchs()
在每次递归调用时显示树上的分支数

__author__ = 'Python Tutotial'
from math import cos, sin, radians, pi
import turtle

""" Run with br = 1, 2, 3, 4, and 5 and put together a recursive formula
that can be used to determine the count)num_branches.
"""

def tree(t, n, x, y, a, branchRadius):
    global count
    count += 1
    bendAngle   = radians(15)
    branchAngle = radians(37)
    branchRatio = .65

    cx = x + cos(a) * branchRadius
    cy = y + sin(a) * branchRadius

    t.width(1 * n ** 1.2)
    if t.pensize() < .3:
        t.color('pink')
    else:
        t.color('black')

    t.goto(x, y)
    t.down()
    t.goto(cx, cy)
    t.up()
    if not n:
        return None

    tree(t, n-1, cx, cy, a + bendAngle - branchAngle, branchRadius * branchRatio)
    tree(t, n-1, cx, cy, a + bendAngle + branchAngle, branchRadius * branchRatio)
    tree(t, n-1, cx, cy, a + bendAngle,               branchRadius * (1 - branchRatio))

# def count_num_branches(br):

def main():
    global count
    count = 0
    N = 1  #Run with N = 1, 2, 3, 4, and 5
    t = turtle.Turtle()
    wn = turtle.Screen()
    wn.setworldcoordinates(0, 0, 1, 1)
    wn.bgcolor('cyan')
    wn.title("My Tree  N = "+str(N))
    #t.speed(0)
    turtle.tracer(15)
    t.ht()
    t.up()
    tree(t, N, .5, 0, pi/2, .3)
    # wn.title("My Tree  N = {0}, Recursive calls: {1:,} count_num_branches = {2:,}".format(N, count, count_num_branches(br)))
    wn.exitonclick()
\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu
从数学输入cos,sin,radians,pi
进口海龟
“”“使用br=1、2、3、4和5运行,并组合一个递归公式。”
可用于确定计数)num_分支。
"""
def树(t,n,x,y,a,branchRadius):
全局计数
计数+=1
bendAngle=弧度(15)
小枝=弧度(37)
branchRatio=.65
cx=x+cos(a)*布兰克拉迪乌斯
cy=y+sin(a)*branchRadius
t、 宽度(1*n**1.2)
如果t.Penize()小于0.3:
t、 颜色(“粉色”)
其他:
t、 颜色(“黑色”)
t、 后藤(x,y)
t、 向下()
t、 后藤(cx,cy)
t、 up()
如果不是n:
一无所获
树(t、n-1、cx、cy、a+bendAngle-枝梗、枝梗*枝梗)
树(t,n-1,cx,cy,a+bendAngle+Branchrangle,branchRadius*branchRatio)
树(t,n-1,cx,cy,a+bendAngle,branchRadius*(1-branchRatio))
#def计数分支数(br):
def main():
全局计数
计数=0
N=1#以N=1、2、3、4和5运行
t=海龟。海龟()
wn=tutle.Screen()
wn.SetWorld坐标(0,0,1,1)
wn.bgcolor('cyan')
wn.title(“我的树N=“+str(N))
#t、 速度(0)
海龟追踪器(15)
t、 ht()
t、 up()
树(t,N,.5,0,pi/2,.3)
#title(“我的树N={0},递归调用:{1:,}count_num_branchs={2:,}”。格式(N,count,count_num_branchs(br)))
wn.exitonclick()

以下内容与您描述的内容相同,但我仍然不确定它是否符合您的要求。它基本上以两种不同的方式计算递归/分支:一种是通过计数;一次通过递归公式:

from math import cos, sin, radians, pi
from turtle import Turtle, Screen

branchRatio = .65
bendAngle = radians(15)
branchAngle = radians(37)

def tree(t, n, x, y, a, branchRadius):
    global count
    count += 1

    cx = x + cos(a) * branchRadius
    cy = y + sin(a) * branchRadius

    t.width(1 * n ** 1.2)
    t.color('pink' if t.width() < 0.3 else 'black')

    t.goto(x, y)
    t.pendown()
    t.goto(cx, cy)
    t.penup()

    if n > 0:
        tree(t, n-1, cx, cy, a + bendAngle - branchAngle, branchRadius * branchRatio)
        tree(t, n-1, cx, cy, a + bendAngle + branchAngle, branchRadius * branchRatio)
        tree(t, n-1, cx, cy, a + bendAngle, branchRadius * (1 - branchRatio))

def count_num_branches(n):
    branches = 1

    if n > 0:
        branches += 3 * count_num_branches(n - 1)

    return branches

N = 3  # Run with N = 1, 2, 3, 4, 5 and 6

screen = Screen()
screen.setworldcoordinates(0, 0, 1, 1)
screen.bgcolor('cyan')
screen.title("My Tree:  N = {0}".format(N))
screen.tracer(15)

turtle = Turtle(visible=False)
#turtle.speed('fastest')
turtle.penup()

count = 0
tree(turtle, N, .5, 0, pi/2, .3)

screen.title("My Tree:  N = {0}, Recursive calls = {1:,}, # Branches = {2:,}".format(N, count, count_num_branches(N)))
screen.exitonclick()
从数学导入cos、sin、弧度、pi
从海龟进口海龟,屏幕
branchRatio=.65
bendAngle=弧度(15)
小枝=弧度(37)
def树(t,n,x,y,a,branchRadius):
全局计数
计数+=1
cx=x+cos(a)*布兰克拉迪乌斯
cy=y+sin(a)*branchRadius
t、 宽度(1*n**1.2)
t、 颜色(如果t.width()小于0.3,则为粉红色,否则为黑色)
t、 后藤(x,y)
t、 彭敦()
t、 后藤(cx,cy)
t、 彭普()
如果n>0:
树(t、n-1、cx、cy、a+bendAngle-枝梗、枝梗*枝梗)
树(t,n-1,cx,cy,a+bendAngle+Branchrangle,branchRadius*branchRatio)
树(t,n-1,cx,cy,a+bendAngle,branchRadius*(1-branchRatio))
def计数分支数(n):
分支=1
如果n>0:
分支数+=3*计数分支数(n-1)
返回分支
N=3#以N=1、2、3、4、5和6运行
screen=screen()
屏幕设置世界坐标(0,0,1,1)
screen.bgcolor('cyan')
screen.title(“我的树:N={0}”。格式(N))
屏幕跟踪器(15)
海龟=海龟(可见=假)
#乌龟。速度(“最快”)
乌龟
计数=0
树(海龟,N.5,0,pi/2,3)
标题(“我的树:N={0},递归调用={1:,},#分支={2:,}”。格式(N,count,count_num_分支(N)))
screen.exitonclick()

绘制的行数与递归调用计数相同。对于N=1,递归调用计数为4,屏幕上有4行,一个主干和三个分支。因此,可以计算分支计数——您想显示什么(例如,树外缘的“叶子”等)这一行:wn.title(“我的树N={0},递归调用:{1:,}count_num_分支={2:,}”。format(N,count,count_num_分支(br))应该使用函数count_num_分支来显示N的当前值,递归调用的计数和包括主干在内的分支总数。