Python中的 turtle.distance()函数
turtle 模块以面向对象和面向过程的方式提供海龟图形原语。因为它使用 tkinter 作为底层图形,所以它需要安装一个支持 Tk 的Python版本。
海龟.距离()
此方法用于以海龟步长为单位返回海龟到 (x,y) 的距离。
Syntax : turtle.distance(x, y=None)
Parameters:
x: x coordinate of Vector 2DVec.
y: y coordinate of Vector 2DVec.
可以以不同的格式调用此方法,如下所示:
distance(x, y) # two coordinates
distance((x, y)) # a pair (tuple) of coordinates
distance(vec) # e.g. as returned by pos()
distance(mypen) # where mypen is another turtle
以下是上述方法的实现以及一些示例:
示例 1:
Python3
# importing package
import turtle
# print the distance
# before any motion
print(turtle.distance())
# forward turtle by 100
turtle.forward(100)
# print the distance
# after a motion
print(turtle.distance())
Python3
# importing package
import turtle
# print distance (default)
print(turtle.distance())
for i in range(4):
# draw one quadrant
turtle.circle(50,90)
# print distance
print(turtle.distance())
Python3
# importing package
import turtle
# print distance with arguments
# in different formats
print(turtle.distance(3,4))
print(turtle.distance((3,4)))
print(turtle.distance((30.0,40.0)))
输出 :
0.0
100.0
示例 2:
Python3
# importing package
import turtle
# print distance (default)
print(turtle.distance())
for i in range(4):
# draw one quadrant
turtle.circle(50,90)
# print distance
print(turtle.distance())
输出 :
0.0
70.7106781187
100.0
70.7106781187
1.41063873243e-14
示例 3:
Python3
# importing package
import turtle
# print distance with arguments
# in different formats
print(turtle.distance(3,4))
print(turtle.distance((3,4)))
print(turtle.distance((30.0,40.0)))
输出 :
5.0
5.0
50.0