Python 海龟螺旋

Python Turtle Spirals

我尝试在绘制递归方块 'k' 次后绘制螺旋,并获得我想要的输出 -

但是我对 spiral() 函数以及如何使用它来获得该输出感到困惑。

代码-

from turtle import Turtle, Screen
import math


def squareinsquare(x, y, side):
    square(x, y, side)
    half = side / 2
    b = math.sqrt(half**2 + half**2)

    tiltsquare(x, y - side/2, b)

#squareinsquare(0, 0, 200)

def fractal(x, y, startSide, k):  
    t.setpos(x, y)
    for i in range(k):
        square(*t.pos(), startSide)
        t.forward(startSide / 2)
        t.right(45)
        startSide /= math.sqrt(2) 

fractal(0, 0, 200, 5)

def spl(x, y, stLength, k):  
    # YOUR CODE BELOW THIS LINE

s.exitonclick()

您没有向我们提供有关该螺旋逻辑的任何信息。它与正方形等有何关系...

就其价值而言,这将或多或少地复制顶级图像。

from turtle import Turtle, Screen
import math

t = Turtle()
s = Screen()
t.speed(0)

def square(x, y, side):
    t.setpos(x,y)
    for i in range(4):
        t.forward(side)
        t.right(90)

def tiltsquare(x, y, side):
    t.left(45)
    square(x, y, side)

def squareinsquare(x, y, side):
    square(x, y, side)
    half = side / 2
    b = math.sqrt(half**2 + half**2)

    tiltsquare(x, y - side/2, b)

# squareinsquare(0, 0, 200)

def fractal(x, y, startSide, k):  
    t.setpos(x, y)
    for i in range(k):
        square(*t.pos(), startSide)
        t.forward(startSide / 2)
        t.right(45)
        startSide /= math.sqrt(2) 

fractal(0, 0, 200, 5)

#x,y are start point coordinates, stLength is len. of first move and k is number of moves  
def spiral(x, y, stLength, k): 
    t.up()
    t.setpos(x, y)
    t.seth(90)
    t.down()
    for i in range(k):
        t.forward(stLength)
        t.left(15)
        stLength -=0.2


spiral(250,-120,40,200)   

s.exitonclick()