[英]how can i get turtle to draw a web spiral?
我試圖讓我的烏龜畫出一個螺旋狀的蜘蛛網,但我只是不能讓螺旋形螺旋形或循環,直到它碰到邊緣。 我嘗試了幾種不同的方法,但我無法解決。 我對編碼很陌生:)
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")
我需要它以這種方式螺旋查看屏幕截圖
https://gyazo.com/028228823b7aab611db144436cf93868
https://gyazo.com/5c9ca19cfa34be5559bdbc3365f65f0d
請幫助:(
在循環內部你必須改變p
但你總是使用相同的值
p = 10/n
如果您使用+=
而不是=
p += 10/n
然后你可以得到螺旋
示例代碼:
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()
n = 5
的結果:
n = 15
的結果:
編輯:要在長度為r
的行結束之前停止螺旋,您必須將spiraldistance+p
與 r` 進行比較 - 即
if spiraldistance+p >= r:
break
或者更好地使用while
循環
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
編輯:我添加了steps
來選擇每行螺旋“交叉”多少次。
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()
步驟 5 和 15:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.