Python turtle.distance()用法及代码示例

您所在的位置:网站首页 turtle函数用法fd Python turtle.distance()用法及代码示例

Python turtle.distance()用法及代码示例

2024-05-09 22:03| 来源: 网络整理| 查看: 265

turtle 模块以面向对象和面向过程的方式提供 turtle 图形基元。由于它使用tkinter作为基础图形,因此需要安装有Tk支持的Python版本。

turtle .distance()

此方法用于以 turtle 步长单位返回从 turtle 到(x,y)的距离。

用法:turtle.distance(x, y=None)

参数:

向量2DVec的x:x坐标。

矢量2DVec的y:y坐标。

可以使用以下不同格式调用此方法:

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())

输出:

0.0 100.0

范例2:

Python3 # importing package import turtle    # print distance (defalut) print(turtle.distance())    for i in range(4):            # draw one quadrent     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



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3