python - 蟒蛇龟螺旋
问题描述
我试图在绘制递归平方'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()
解决方案
你没有给我们任何关于那个螺旋的逻辑的信息。它是如何与正方形等相关的......
对于它的价值,这将或多或少地复制那个顶部图像。
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()
推荐阅读
- html - Input/Set/Change HTML img src with Node JS
- excel - Change arrow color based on its label or value
- selenium - How to disable "Mark non-secure origins as non-secure" within selenium tests?
- postgresql - 无法将 SSL 安全数据库连接到 typeorm
- c# - KeyUp 事件处理程序中的信号量——锁定键盘输入
- android - Return Image from signature with navigator.pop
- java - How to change time zone in Linux Time?
- c++ - 将 JSON 对象作为 JNI 中的参数传递给 CPP,并在 CPP 中检索 JSON 对象的数据
- django - Change 5th number from list
- javascript - detect swipe left/right in React