简体   繁体   中英

Python Turtle Fibonacci Spiral

I want to draw a fibonnaci spiral in the output instead of a regular spiral. However, I'm confused about how to change the spiral function to get my desired output of a spiral like this - 斐波那契

code -

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)

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, 15)

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

The output of this code would be -

在此处输入图片说明

I did this for hour and I made shape and this spiral, but I am still stuck on this fibonacci spiral. It doesn't works that good. But it works good as you paint.

from turtle import Turtle, Screen

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

def rectangle(x, y, side_a, side_b):
    t.setpos(x, y)
    t.forward(side_b)
    t.right(90)
    t.forward(side_a)
    t.right(90)
    t.forward(side_b)
    t.right(90)
    t.forward(side_a)
    t.right(90)

def squares_in_rectangle():
    t.penup()
    t.goto(200, 0)
    t.pendown()
    t.right(90)
    t.forward(200)
    t.penup()
    t.goto(200, -110)
    t.left(90)
    t.pendown()
    t.forward(100)
    t.penup()
    t.goto(230, -110)
    t.right(90)
    t.pendown()
    t.forward(90)
    t.penup()
    t.goto(200, -140)
    t.left(90)
    t.pendown()
    t.forward(30)
    t.penup()
    t.goto(220, -110)
    t.right(90)
    t.pendown()
    t.forward(30)
    t.penup()
    t.goto(220, -120)
    t.left(90)
    t.pendown()
    t.forward(10)


def spiral(x, y, stLength, k):
    t.up()
    t.setpos(x, y)
    t.seth(45)
    t.down()
    for i in range(k):
        something = 60
        something += 10
        t.left(something)
        something += 3
        stLength /= 0.7
        t.forward(stLength)

rectangle(0, 0, 200, 300)
squares_in_rectangle()
spiral(225, -120, 35, 5)


s.exitonclick()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM