用户:
yj皓月当空查看:4 回复:1 评论:4 创建时间:2020-07-19T12:19:01
【作品展示】

【作品介绍】
通过万有引力等物理知识点完成模拟测力计的过程,最终结果是10N
【作品源代码】
from vpython import helix, vector, text, color, sphere, box, rate, graph, gcurve, arrow # 导入模块
# 弹簧测力计的顶板与背板
fixed_box = box(pos=vector(0, 2.02, 0),
size=vector(0.8, 0.04, 0.5),
color=color.white)
range_box = box(pos=vector(0, 0.5, -0.23),
size=vector(0.8, 3, 0.04),
color=color.white)
# 搭建弹簧
spring = helix(
pos=vector(0, 2, 0),
axis=vector(0, -2, 0),
radius=0.1,
coils=50,
color=color.green,
stiffness=100
)
# 搭建弹簧末端
eq = spring.axis + spring.pos
pointer = arrow(pos=eq,
axis=vector(0, -0.2, 0),
shaftwidth=0.2,
headwidth=0.3,
headlength=0.1)
# 绘制刻度线及刻度值
mass = "012345"
for i in range(0, 6, 1):
length = eq.y - 0.1 * i
range_line = box(pos=vector(0, length, -0.23),
size=vector(0.3, 0.004, 0.04),
color=color.blue)
# 生成刻度值
range_mass = text(pos=vector(0.15, length-0.02, -0.22),
text=str(int(mass[i])*10) + "N",
color=color.blue,
height=0.05
)
# 创建小球
ball = sphere(pos=eq - vector(0, 0.3, 0),
radius=0.1,
color=color.red,
mass=1,
force=vector(0, 0, 0),
velocity=vector(0, -1, 0)
)
# 动画模拟弹簧测力计测量物体
d = 0.01
# 小球重力
ball.gravity = ball.mass*10
while ball.force.y < ball.gravity:
rate(100)
# 小球向下移动后 y 坐标的变化
ball.pos.y = ball.pos.y + d * ball.velocity.y
# 挂钩的坐标向量
pointer.pos = ball.pos + vector(0, 0.3, 0)
# 弹簧的坐标向量
spring.axis = ball.pos + vector(0, 0.3, 0) - spring.pos
# 小球所受到的拉力
spring.stretch = eq - ball.pos - vector(0, 0.3, 0)
ball.force = spring.stiffness * spring.stretch
【提示】
部分含有Python第三方库相关内容的作品,在海龟编辑器网页端无法运行哦!如遇到这种情况,可以打开下面的链接,下载海龟编辑器客户端:
https://python.codemao.cn