python绘制铅球的运行轨迹代码分享
作者:快递小可 时间:2021-06-12 06:39:03
我们按照面向过程程序设计的思想,使用python编写了程序,追踪铅球在运行过程中的位置信息。下面,修改程序代码,导入turtle模块,将铅球的运行轨迹绘制出来。
python3代码如下:
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()
运行程序,输入输出如下:
绘制的铅球运行轨迹,如下:
总结
有关turtle模块的使用,后面还会向大家专门介绍,这里暂不赘述。
Python编程实现蚁群算法详解
python中实现k-means聚类算法详解
Python内存管理方式和垃圾回收算法解析
如有不足之处,欢迎留言指出。
来源:http://blog.csdn.net/sxingming/article/details/51261429
标签:python,轨迹图


猜你喜欢
python获取一组汉字拼音首字母的方法
2023-04-08 16:43:02
python中将字典形式的数据循环插入Excel
2023-07-05 01:49:19

jQuery中$.get、$.post、$.getJSON和$.ajax的用法详解
2024-04-16 08:54:20
javascript实现计算器功能详解流程
2024-04-23 09:27:00

Python imutils 填充图片周边为黑色的实现
2021-04-13 04:06:32

解决pandas无法在pycharm中使用plot()方法显示图像的问题
2021-06-02 21:23:14
Python中字典创建、遍历、添加等实用操作技巧合集
2021-04-02 08:22:12
php常见的页面跳转方法汇总
2024-05-28 15:38:15
python通过ssh-powershell监控windows的方法
2021-07-24 03:55:16
网页版面布局的处理问题
2008-06-05 12:32:00
javascript+css3开发打气球小游戏完整代码
2024-05-02 16:15:54

解决pandas使用read_csv()读取文件遇到的问题
2021-08-29 18:31:02
python聊天室(虽然很简洁,但是可以用)
2021-05-21 01:10:46

python Flask框架之HTTP请求详解
2023-10-02 01:52:55

Golang 高效排序数据详情
2023-06-23 08:23:41

Python调用shell命令常用方法(4种)
2021-05-06 08:22:06

用tensorflow构建线性回归模型的示例代码
2022-04-12 03:41:47
PHP面向对象程序设计高级特性详解(接口,继承,抽象类,析构,克隆等)
2023-11-23 11:35:00
sqlserver2005 xml字段的读写操作
2024-01-16 23:00:37

VSCODE添加open with code实现右键打开文件夹
2022-02-06 05:09:43
