网上的电磁波图片,一般是静态的,本文尝试用vpython制作电磁波传播的动画
程序代码
from vpython import*
#设置参数
c = 3*1e8
n = 100
double_pi = 2*pi
t,dt = 0,1*e-15
#设置场景
scene = canvas(center=vec(0,0,0), width=1200, height=800,background=color.white)
x = arrow(axis=vec(1.5,0,0), color=color.black, shaftwidth=2*pi/100)
e = label(pos=vec(0,1.5,0),text='E', xoffset=20,yoffset=50, space=30,height=16, border=4,font='sans')
y = arrow(axis=vec(0,1.5,0), color=color.red, shaftwidth=2*pi/100)
b = label(pos=vec(0,0,1.5),text='B', xoffset=20,yoffset=50, space=30,height=16, border=4,font='sans')
z = arrow(axis=vec(0,0,1.5), color=color.blue, shaftwidth=2*pi/100)
# 电磁场
x_ = [(2*double_pi/n)*i for i in range(n)]
sin_ = [sin(x_[i]) for i in range(n)]
#print(sin_)
E = [arrow(axis=vec(0,0,0),shaftwidth=2*pi/500,color=color.red) for i in range(n)]
B = [arrow(axis=vec(0,0,0),shaftwidth=2*pi/500,color=color.blue) for i in range(n)]
scene.pause('暂停/继续')
run = True
def running(ev):
global run
run = not run
scene.bind('click',running)
# 动画
while True:
rate(70)
if run == True:
t += dt
for i in range(n):
E[i].pos.x = x_[i]
E[i].axis.y = sin(x_[i] - c*t)
B[i].pos.x = x_[i]
B[i].axis.z = sin( x_[i] - c*t)
评论 (0)