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,轨迹图
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
基于python实现MUI区域滚动
2023-11-04 02:38:52
![](https://img.aspxhome.com/file/2023/6/79396_0s.png)
谈谈设计师的发展
2009-03-17 18:20:00
Pytorch自定义CNN网络实现猫狗分类详解过程
2023-10-27 19:51:02
![](https://img.aspxhome.com/file/2023/3/72083_0s.png)
python连接mongodb操作数据示例(mongodb数据库配置类)
2023-05-01 17:21:17
IE8新特性及IE8安装使用 目录
2008-04-01 09:50:00
Python面向对象程序设计之类和对象、实例变量、类变量用法分析
2021-05-08 01:34:56
![](https://img.aspxhome.com/file/2023/8/75858_0s.png)
试试把xml和javascript写到同一个文件里面
2009-10-02 16:53:00
itchat-python搭建微信机器人(附示例)
2022-05-04 00:00:54
彻底弄懂CSS盒子模式之一(DIV布局快速入门)
2007-05-11 17:03:00
![](https://img.aspxhome.com/file/uploadpic/20075/200751117352545.gif)
JavaScript 在各个浏览器中执行的耐性
2009-02-06 15:26:00
JS实现拖动模糊框特效
2023-08-06 05:18:51
![](https://img.aspxhome.com/file/2023/0/55910_0s.gif)
FrontPage2003 中网页布局功能的应用
2007-11-18 14:43:00
![](https://img.aspxhome.com/file/uploadpic/200711/18/20071118144520978.jpg)
Pycharm之如何安装cv2 [python3.6]
2023-01-15 09:56:45
![](https://img.aspxhome.com/file/2023/7/69427_0s.png)
Python中的Pandas 时间函数 time 、datetime 模块和时间处理基础讲解
2022-08-01 05:18:41
![](https://img.aspxhome.com/file/2023/9/63559_0s.png)
python如何生成任意n阶的三对角矩阵
2021-09-18 07:41:37
Python爬取世界杯热搜榜并制作脚本自动发送信息到邮箱的过程
2021-01-21 20:35:53
![](https://img.aspxhome.com/file/2023/1/80401_0s.jpg)
ExecuteReader(),ExecuteNonQuery(),ExecuteScalar(),ExecuteXmlReader()之间的区别
2023-07-08 23:15:54
Python实现k-means算法
2023-07-01 22:06:53
![](https://img.aspxhome.com/file/2023/8/79588_0s.png)
Python 元组(Tuple)操作详解
2023-06-28 15:36:39
![](https://img.aspxhome.com/file/2023/3/59133_0s.png)
动态生成的IFRAME设置SRC时的,不同位置带来的影响
2008-03-06 13:56:00