简体   繁体   中英

how can i get turtle to draw a web spiral?

im trying to get my turtle to draw a spiralling spiderweb but i just cant get the spiral to spiral or to loop until it hits the edge. ive tried several different things but i cant work it out. im pretty new to coding:)

from turtle import *
from math import sin, cos, pi
from random import randint
shape("circle")
turtlesize(0.3)
speed(5)
n=int(input("give number of main lines: "))
r=int(input("give length of main lines: "))
spiraldistance=r/10
angle=360/n
rad=(pi*angle)/180
for i in range(n):
    forward(r)
    backward(r)
    right(hoek)
x=cos(rad*0)*spiraldistance
y=sin(rad*0)*spiraldistance
goto(x,y)
integers = []
for j in range(0, r):
    p = 10/n
    integers.append(j)
    integers.append(p)
    x=cos(rad*j)*(spiraldistance+p)
    y=sin(rad*j)*(spiraldistance+p)
    goto(x,y)

input("Press enter to finish")

i need it to spiral this way look at the screenshots

https://gyazo.com/028228823b7aab611db144436cf93868

https://gyazo.com/5c9ca19cfa34be5559bdbc3365f65f0d

pls help:(

Inside loop you have to change p but you always use the same value

p = 10/n

If you use += instead of =

p += 10/n

then you can get spiral


Example code:

from turtle import *
from math import sin, cos, pi

shape("circle")
turtlesize(0.3)
speed(0)

#---

#n = int(input("give number of main lines: "))
n = 5
#r = int(input("give length of main lines: "))
r = 200

#---

angle = 360/n
for i in range(n):
    forward(r)
    backward(r)
    right(angle)

#---

spiraldistance = r/10
rad = (pi*angle)/180
p = 0

for j in range(r):
    x = cos(rad*j) * (spiraldistance+p)
    y = sin(rad*j) * (spiraldistance+p)
    goto(x, y)
    p += 10/n

exitonclick()   

Result for n = 5 :

在此处输入图像描述

Result for n = 15 :

在此处输入图像描述

EDIT: To stop spiral before end of lines which have leght r you would have to compare spiraldistance+p with r` - ie

if spiraldistance+p >= r:
    break

Or better use while loop for this

spiraldistance = r/10
rad = (pi*angle)/180
p = 0
j = 0

while spiraldistance+p < r:
    x = cos(rad*j) * (spiraldistance+p)
    y = sin(rad*j) * (spiraldistance+p)
    goto(x, y)
    p += 10/n
    j += 1

EDIT: I added steps to choose how many times spiral "cross" every line.

from turtle import *
from math import sin, cos, pi

shape("circle")
turtlesize(0.3)
speed(0)

#--- settings ---

# number of main lines
#n = int(input("give number of main lines: "))
n = 15

# length of main lines
#r = int(input("give length of main lines: "))
length = 200

# number of steps on every main line
steps = 15

#--- main lines ---

angle = 360/n
for i in range(n):
    forward(length)
    backward(length)
    right(angle)

#--- spiral ---

p = (length/n)/steps
rad = (pi*angle)/180

spiraldistance = 0
j = 0

while spiraldistance < length:
    spiraldistance += p
    x = cos(j) * spiraldistance
    y = sin(j) * spiraldistance
    goto(x, y)
    j += rad

#--- keep open ---

#mainloop()
exitonclick()

Steps 5 and 15:

在此处输入图像描述

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