Python - 递归公式

问题描述 投票:-1回答:1

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

在下面的代码中,下面两个注释的#行显示了我想要完成的任务。我完全理解当N递增1时,添加到树的分支数是3的倍数,但我不明白如何利用函数count_num_branches()在每次递归调用时显示树上的分支数。

__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()
python recursion turtle-graphics
1个回答
0
投票

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

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()
© www.soinside.com 2019 - 2024. All rights reserved.