Python:3个分支树

时间:2019-03-22 01:10:48

标签: python drawing turtle-graphics

What i want it to look like

results

我想出了如何制作一棵树,但是它有时会产生随机的设计。相反,我想知道如何制作一棵均匀的3分支树。谢谢!

{{1}}

1 个答案:

答案 0 :(得分:0)

我不得不说,我喜欢您的程序当前生成的树,因为它们更像是真实的树,并且每棵都是不同的。如果我们从代码中删除随机元素,并添加一个中间分支,那么我们将获得所需的一棵统一树,而只有所需的一棵统一树:

from turtle import Screen, Turtle, Vec2D

THICKNESS = 30

def draw_line(position, angle, length, size, color):
    turtle.penup()
    turtle.goto(position)
    turtle.setheading(angle)
    turtle.color(color)
    turtle.pensize(size)
    turtle.pendown()
    turtle.forward(length)

def draw_tree(position, angle, length, size, color, n):
    if n == 0:
        return

    if n <= 3:
        color = 'green'

    draw_line(position, angle, length, size, color)

    position = turtle.position()
    length /= 1.4
    size *= 0.7

    draw_tree(position, angle - THICKNESS, length, size, color, n - 1)
    draw_tree(position, angle, length, size, color, n - 1)
    draw_tree(position, angle + THICKNESS, length, size, color, n - 1)

screen = Screen()
screen.tracer(False)

turtle = Turtle(visible=False)

draw_tree(Vec2D(0, -350), 90, 150, 10, 'brown', 9)

screen.update()
screen.tracer(True)
screen.exitonclick()

enter image description here