python绘制铅球的运行轨迹代码分享 我们按照面向过程程序设计的思想,使用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编程实现蚁群算法详解 python中实现k-means聚类算法详解 Python内存管理方式和垃圾回收算法解析 如有不足之处,欢迎留言指出。