将代码从 javascript 转换为 python,尝试使用 Python Turtle
Converted a code from javascript to python, tryin to use Python Turtle
下面的代码不绘制任何图像:
"""
import turtle
import numpy as np
turtle = turtle.Turtle()
steps = 24000
size = 61
def walk(i):
turtle.penup()
term = 1.0357902468 * 2 * np.pi * i/steps
turtle.goto(size * np.sin(term), size * np.cos(term))
turtle.pendown()
len = 5+2.5*np.sin(0.5 * np.pi + term)*np.cos(0.5 * np.pi + term)
l = 0.02
if l < len:
turtle.right(np.sin(l + steps/8 * term))
turtle.forward(l)
l = l * 1.01
return i < steps
i=2
walk(i)
"""
它只是不起作用而且无法修复,还是我遗漏了什么?
在我看来,当您使用 while
时,您似乎使用了 if
。进行替换并整理代码,它确实绘制了一些东西:
from turtle import Screen, Turtle
from math import sin, cos, pi
STEPS = 24000
SIZE = 61
def walk(i):
turtle.penup()
term = 1.0357902468 * 2 * pi * i/STEPS
turtle.goto(SIZE * sin(term), SIZE * cos(term))
turtle.pendown()
length = 5 + 2.5 * sin(pi/2 + term) * cos(pi/2 + term)
l = 0.02
while l < length:
turtle.right(sin(l + STEPS/8 * term))
turtle.forward(l)
l *= 1.01
return i < STEPS
screen = Screen()
turtle = Turtle()
turtle.speed('fastest') # because I have no patience
walk(2) # return value ignored
screen.exitonclick()
下一个要考虑的问题是,您是否期望 right()
以度数(默认)或弧度(选项)运行,JavaScript 使用。
下面的代码不绘制任何图像: """
import turtle
import numpy as np
turtle = turtle.Turtle()
steps = 24000
size = 61
def walk(i):
turtle.penup()
term = 1.0357902468 * 2 * np.pi * i/steps
turtle.goto(size * np.sin(term), size * np.cos(term))
turtle.pendown()
len = 5+2.5*np.sin(0.5 * np.pi + term)*np.cos(0.5 * np.pi + term)
l = 0.02
if l < len:
turtle.right(np.sin(l + steps/8 * term))
turtle.forward(l)
l = l * 1.01
return i < steps
i=2
walk(i)
"""
它只是不起作用而且无法修复,还是我遗漏了什么?
在我看来,当您使用 while
时,您似乎使用了 if
。进行替换并整理代码,它确实绘制了一些东西:
from turtle import Screen, Turtle
from math import sin, cos, pi
STEPS = 24000
SIZE = 61
def walk(i):
turtle.penup()
term = 1.0357902468 * 2 * pi * i/STEPS
turtle.goto(SIZE * sin(term), SIZE * cos(term))
turtle.pendown()
length = 5 + 2.5 * sin(pi/2 + term) * cos(pi/2 + term)
l = 0.02
while l < length:
turtle.right(sin(l + STEPS/8 * term))
turtle.forward(l)
l *= 1.01
return i < STEPS
screen = Screen()
turtle = Turtle()
turtle.speed('fastest') # because I have no patience
walk(2) # return value ignored
screen.exitonclick()
下一个要考虑的问题是,您是否期望 right()
以度数(默认)或弧度(选项)运行,JavaScript 使用。