Turtle 参数代码在 Python 3 中工作但在 Python2 中不工作
Turtle parametric code working in Python 3 but not in Python2
我有以下代码使用 turtle 模块在 python 中绘制参数曲线。我不明白为什么这适用于 python 3 而不是 python 2.
两种变体的代码
import turtle
import math
def line(x1,y1,x2,y2):
turtle.up()
turtle.goto(x1,y1)
turtle.down()
turtle.goto(x2,y2)
def plot():
turtle.up()
turtle.goto(0,150)
turtle.down()
for i in range(0, int(2*math.pi*1000),10):
turtle.goto(150*math.sin(2*(i/1000)),150*math.cos(5*(i/1000)))
def axes():
line(-200,0,200,0)
line(0,-200,0,200)
def main():
turtle.setup()
turtle.color("blue")
axes()
turtle.color("red")
plot()
turtle.done()
main()
Python 2(错误的一个)的乌龟输出曲线:-
Python 3(右边那个)的乌龟曲线:-
任何人都有任何想法。我认为 math.sin 接受弧度,我输入的弧度基于转换后的比例因子。
整数除法在版本 2 中使用截断。它在版本 3 中产生浮点结果。尝试更改
i/1000
至
i/1000.0
开头加PEP-0238:
from __future__ import division
我有以下代码使用 turtle 模块在 python 中绘制参数曲线。我不明白为什么这适用于 python 3 而不是 python 2.
两种变体的代码
import turtle
import math
def line(x1,y1,x2,y2):
turtle.up()
turtle.goto(x1,y1)
turtle.down()
turtle.goto(x2,y2)
def plot():
turtle.up()
turtle.goto(0,150)
turtle.down()
for i in range(0, int(2*math.pi*1000),10):
turtle.goto(150*math.sin(2*(i/1000)),150*math.cos(5*(i/1000)))
def axes():
line(-200,0,200,0)
line(0,-200,0,200)
def main():
turtle.setup()
turtle.color("blue")
axes()
turtle.color("red")
plot()
turtle.done()
main()
Python 2(错误的一个)的乌龟输出曲线:-
Python 3(右边那个)的乌龟曲线:-
任何人都有任何想法。我认为 math.sin 接受弧度,我输入的弧度基于转换后的比例因子。
整数除法在版本 2 中使用截断。它在版本 3 中产生浮点结果。尝试更改
i/1000
至
i/1000.0
开头加PEP-0238:
from __future__ import division