在Python中绘制分形树,不知道如何继续

时间:2016-10-04 12:59:22

标签: python turtle-graphics fractals

到目前为止我在python

中有这个
import turtle
import math
t = turtle.Turtle()
t.shape("turtle")
t.lt(90)

lv = 11
l  = 100
s  = 17

t.penup()
t.bk(l)
t.pendown()
t.fd(l)

def draw_tree(l, level):
    l = 3.0/4.0*l
    t.lt(s)
    t.fd(l)
    level +=1
    if level<lv:
        draw_tree(l, level)

    t.bk(l)
    t.rt(2*s)
    t.fd(l)
    if level<=lv:
        draw_tree(l, level)
    t.bk(l)
    t.lt(s)
    level -=1

t.speed(100)        
draw_tree(l, 2)

但是我有点坚持如何投资,因为我需要建立这棵树。这就是我想要制作的东西:

Fractal tree

任何人都可以告诉我我做错了吗?

3 个答案:

答案 0 :(得分:4)

您的代码基本上是正确的,您通常需要调整参数。您尝试匹配的示例树大于您绘制的示例树(可能在该图像中缩小),因此请增加l参数。示例树有比您更多的递归级别,因此请增加lv参数。

最后,您需要根据递归级别重置笔的宽度(并在出路时取消设置)。下面的代码返工会做到这一点但需要进一步微调:

import turtle

t = turtle.Turtle(shape="turtle")

t.lt(90)

lv = 13
l = 120
s = 17

t.width(lv)

t.penup()
t.bk(l)
t.pendown()
t.fd(l)

def draw_tree(l, level):
    width = t.width()  # save the current pen width

    t.width(width * 3.0 / 4.0)  # narrow the pen width

    l = 3.0 / 4.0 * l

    t.lt(s)
    t.fd(l)

    if level < lv:
        draw_tree(l, level + 1)
    t.bk(l)
    t.rt(2 * s)
    t.fd(l)

    if level < lv:
        draw_tree(l, level + 1)
    t.bk(l)
    t.lt(s)

    t.width(width)  # restore the previous pen width

t.speed("fastest")

draw_tree(l, 2)

turtle.done()

答案 1 :(得分:2)

我真的很喜欢@cdlane回答,所以我玩了一段时间的代码。树现在看起来好多了,而且代码更具可读性,所以我觉得值得分享。

enter image description here

代码:

import turtle

WIDTH = 15
BRANCH_LENGTH = 120
ROTATION_LENGTH = 27


class Tree_Fractal(turtle.Turtle):
    def __init__(self, level):
        super(Tree_Fractal, self).__init__()
        self.level = level
        self.hideturtle()
        self.speed('fastest')
        self.left(90)
        self.width(WIDTH)
        self.penup()
        self.back(BRANCH_LENGTH * 1.5)
        self.pendown()
        self.forward(BRANCH_LENGTH)
        self.draw_tree(BRANCH_LENGTH, level)

    def draw_tree(self, branch_length, level):
        width = self.width()
        self.width(width * 3. / 4.)
        branch_length *= 3. / 4.
        self.left(ROTATION_LENGTH)
        self.forward(branch_length)

        if level > 0:
            self.draw_tree(branch_length, level - 1)
        self.back(branch_length)
        self.right(2 * ROTATION_LENGTH)
        self.forward(branch_length)

        if level > 0:
            self.draw_tree(branch_length, level - 1)
        self.back(branch_length)
        self.left(ROTATION_LENGTH)

        self.width(width)


if __name__ == '__main__':
    tree_level = 11  # choose
    tree = Tree_Fractal(tree_level)
    turtle.done()

答案 2 :(得分:0)

这是17行(无功能)的写法:

import turtle
for i in range(2048): #how many branches(2 ** depth)
turtle.goto(0,0) #beginning of tree
turtle.setheading(90) #direction(try 270 for root effect)
turtle.hideturtle() #MUCH faster drawing and better view
a = str(bin(i)).replace('0b','') #binary is for two branches('0b' ignored)
a = list(str('0' * (11 - len(list(a)))) + a) #cake filling
turtle.speed(0) #no animation
turtle.pendown() #you probably wanna draw it
for f in range(len(a)): #depth
    b = 200 #primary branch length
    for l in range(f + 1): #more depth less length
        b /= 1.5 #the difference of branch length(b = b / float)
    turtle.width(b / 10) #branch width(for better effect)
    turtle.forward(b) #drawing a branch
    turtle.right((int(a[f]) - 0.5) * 60) #change 60 to any angle
turtle.penup() #also try without this line