iamjr15
iamjr15

Reputation: 43

Python Turtle Spirals

I'm trying to draw a spiral after it has drawn the recursive squares 'k' number of times, and get my desired output -

however I'm confused with the spiral() function, and how to go about it to get that output.

code -

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

Upvotes: 0

Views: 220

Answers (1)

symmetry
symmetry

Reputation: 529

You haven't given us any information on the logic of that spiral. How it is relates to the squares, etc ...

For what it's worth, this will replicate that top image, more or less.

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

enter image description here

Upvotes: 2

Related Questions