【问题标题】:How would I make this code into a fractal?我如何将这段代码变成分形?
【发布时间】:2014-03-10 02:18:56
【问题描述】:

我有一个起始星。现在,我怎么把它变成分形?

import turtle

turing = turtle.Turtle()

for i in range(5):
    turing.forward(110)
    turing.left(216)

【问题讨论】:

    标签: python turtle-graphics fractals


    【解决方案1】:

    遵循@mgkrebbs 的一般哲学,使用使线偏转的简单分形,我们可以使偏转的所有线更小地复制分形。你的星星很难处理,但由于它有顶点,我们可以递归地将较小的星星放在每个顶点:

    from turtle import Turtle, Screen
    
    def star(turtle, length, depth):
        turtle.left(90)
        for _ in range(5):
            turtle.forward(length)
            heading = turtle.heading()
            if depth > 1:
                star(turtle, length / 2, depth - 1)
            turtle.setheading(heading)
            turtle.left(216)
    
    turing = Turtle()
    turing.speed("fastest")
    
    star(turing, 180, 3)
    
    turing.hideturtle()
    
    screen = Screen()
    
    screen.exitonclick()
    

    随着深度的增加,您可以看到星星开始重叠——通过增加length 使图像更大,或者使递归成为length 的较小部分,可能会有所帮助。

    输出

    【讨论】:

      【解决方案2】:

      分形是重复出现一些变化的东西。因此,将您的星形循环代码放入一个循环中并重复几次。在做每个星环后改变一些东西。你可以改变乌龟的位置,或者它指向的角度,或者下一颗星星的边有多长,或者所有这些。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多