0

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

但是我对螺旋()函数以及如何获得该输出感到困惑。

代码 -

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()

4

1 回答 1

2

你没有给我们任何关于那个螺旋的逻辑的信息。它是如何与正方形等相关的......

对于它的价值,这将或多或少地复制那个顶部图像。

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()

在此处输入图像描述

于 2021-10-17T10:21:44.967 回答