脚本之家,脚本语言编程技术及教程分享平台!
分类导航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服务器之家 - 脚本之家 - Python - python绘制铅球的运行轨迹代码分享

python绘制铅球的运行轨迹代码分享

2020-12-17 01:13快递小可 Python

这篇文章主要介绍了python绘制铅球的运行轨迹代码分享,具有一定参考价值,需要的朋友可以了解下。

我们按照面向过程程序设计的思想,使用python编写了程序,追踪铅球在运行过程中的位置信息。下面,修改程序代码,导入turtle模块,将铅球的运行轨迹绘制出来。

python3代码如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
from math
import pi, sin, cos, radians
from turtle
import Turtle
def main():
  angle = eval(input('Enter the launch angle(in degrees):'))
vel = eval(input('Enter the initial velocity(in meters/sec):'))
h0 = eval(input('Enter the initial height(in meters):'))
time = eval(input('Enter the time interval:'))# 设置铅球的起始位置
xpos = 0
ypos = h0
theta = radians(angle)# 将输入的角度值转换为弧度值
xvel = vel * cos(theta)# 铅球的初始速度在x轴上的分量
yvel = vel * sin(theta)# 铅球的初始速度在y轴上的分量# 创建Turtle对象, 刚创建的小乌龟对象, 位于坐标原点( 0, 0), 朝向x轴正方向
t = Turtle()
t.color('red')# 设置画笔的颜色
t.pensize(2)# 线条粗细
t.speed(2)# 调整速度
t.hideturtle()# 隐藏小乌龟# 绘制x轴和y轴
t.forward(350)# 绘制x轴
t.goto(0, 0)# 回到坐标原点, 准备绘制y轴
t.goto(0, 200)# 绘制y轴
print('the position:({0:.3f},{1:0.3f})'.format(xpos, ypos))
xScale = 25# x坐标放大倍数
yScale = 30# y坐标放大倍数# 画笔移到铅球的起始位置, 准备绘制铅球的运行轨迹
t.goto(xpos * xScale, ypos * yScale)# 通过while循环绘制铅球的运行轨迹, 每隔time秒, 取一个点, 将所有取到的点连起来
while ypos >= 0:
  xpos = xpos + time * xvel
yvel1 = yvel - time * 9.8
ypos = ypos + time * (yvel + yvel1) / 2.0
yvel = yvel1
print('the position:({0:.3f},{1:0.3f})'.format(xpos, ypos))
t.goto(xpos * xScale, ypos * yScale)
print('\nDistance traveled:{0:0.1f} meters.'.format(xpos))
if __name__ == '__main__':
  main()

运行程序,输入输出如下:

python绘制铅球的运行轨迹代码分享

python绘制铅球的运行轨迹代码分享

python绘制铅球的运行轨迹代码分享

绘制的铅球运行轨迹,如下:

python绘制铅球的运行轨迹代码分享

总结

有关turtle模块的使用,后面还会向大家专门介绍,这里暂不赘述。

以上就是本文关于python绘制铅球的运行轨迹代码分享的全部内容,希望对大家有所帮助。如有不足之处,欢迎留言指出。

原文链接:http://blog.csdn.net/sxingming/article/details/51261429

延伸 · 阅读

精彩推荐