python写的动画,用的是Vpython库,需要从VTK官网下载,目前3.x的python只有3.3有这个库,3.4还没有。依旧用的是欧拉折线法绘制电场线的轨迹,和上一篇的Fortran是一样的算法。
#!/usr/bin/python
from visual import *
import cmath as cm
import numpy
ball1=sphere(pos=(1.0,1.0,0.0),radius=0.3,color=color.red)
ball2=sphere(pos=(-1.0,1.0,0.0),radius=0.3,color=color.blue)
ball3=sphere(pos=(-1.0,-1.0,0.0),radius=0.3,color=color.red)
ball4=sphere(pos=(1.0,-1.0,0.0),radius=0.3,color=color.blue)
ball=[ball1,ball2,ball3,ball4]
q=numpy.array([1.0,-1.0,1.0,-1.0])
ground=box(pos=(0.0,0.0,0.0),size=(12.0,12.0,0.1),opacity=0.2)
dt=0.002
N=30
rate(5)
for i in range(len(q)):
for j in range(N):
p=0.32*cm.exp(j*cm.pi*(0.0+1.0j)*2./real(N))
line=sphere(pos=(ball[i].x+p.real,ball[i].y+p.imag,0.0),
radius=0.02,color=color.red)
line.trail=curve(color=line.color)
line.trail.append( pos = line.pos, color = (1, 0, 0))
while not(abs(line.pos)>15.0 or abs(line.pos-ball[1].pos)<0.32
or abs(line.pos-ball[2].pos)<0.32 or abs(line.pos-ball[3].pos)<0.32
or abs(line.pos-ball[0].pos)<0.32):
E=line.pos*0.0
if i in [0,2]:
for k in range(len(ball)):
E+=q[k]*(line.pos-ball[k].pos)/abs(line.pos-ball[k].pos)**3.0
else:
for k in range(len(ball)):
E-=q[k]*(line.pos-ball[k].pos)/abs(line.pos-ball[k].pos)**3.0
line.pos+=dt*E/abs(E)
line.trail.append( pos = line.pos, color = (1, 0, 0))
效果如图:
你好,不知道curve,sphere这些函数是哪个包里的呢?具体参数是什么意思?能详细给程序写一下注释吗?谢谢!