from random import * from visual import * print """ Electromagnetism: Gauss Electric Law(v2.75) 2008-01-20 Rob Salgado (salgado@physics.syr.edu) Electric Field vectors are blue. """ scene=display( width=800,height=600, x=0, y=0) scene.ambient=0.24 colorBackground=[color.black,color.white] colorScheme=0 scene.background=colorBackground[colorScheme] scene.title="GAUSS: Radial E's are associated with ElectricPoint-Charges" scene.range=(2.5,2.5,2.5) scene.forward=(-0.162765,0.013403,-0.986574) #based on # # http://www.math.niu.edu/~rusin/known-math/97/spherefaq #then # http://astronomy.swin.edu.au/~pbourke/geometry/spherepoints/source1.c # p=[] sp=[] theta=[] phi=[] N=24 for k in arange(0, N): #print "k=",k, h = -1 + 2*k/(N-1.) #print "h=",h, theta.append( arccos(h) ) if k==0 or k==N-1: phi.append( 0 ) #print "q=0.00" else: phi_last=phi[-1] q=N*(1-h*h) #print "q=",q phi.append( phi_last + 3.6/sqrt(q)) for i in arange(0,N): p.append(vector( cos(phi[i])*sin(theta[i]),sin(phi[i])*sin(theta[i]), cos(theta[i]) )) # sp.append( sphere(pos=p[-1],radius=0.05) ) counter=0 countmax=100 n=len(p) print n while(counter maxd): maxd = d p1=p[minp1] p2=p[minp2] p[minp2] = norm( p1 + 1.1 * (p2 - p1) ) p[minp1] = norm( p1 - 0.1 * (p2 - p1) ) counter+=1 #################################################### sphere(radius=0.04,color=color.cyan) E=0.5*1/mag2(p[0]) for i in arange(0,N): A=arrow(pos=p[i],axis=E*p[i],shaftwidth=0.04,fixedwidth=1,color=color.blue) box(pos=p[i]+A.axis/4.,axis=A.axis,length=mag(A.axis)/2.,height=0.04,width=0.04,color=color.blue) E /=4. for i in arange(0,N): A=arrow(pos=2*p[i],axis=E*p[i] ,shaftwidth=0.04,fixedwidth=1,color=color.blue) box(pos=2*p[i]+A.axis/4.,axis=A.axis,length=mag(A.axis)/2.,height=0.04,width=0.04,color=color.blue) #Now... WHEN AN OBJECT IS PICKED, #TRANSLATE THE scene.center TO THE OBJECT'S POSITION while 1: rate(5) if scene.mouse.clicked: scene.mouse.getclick() newPick=scene.mouse.pick if newPick !=None: ### ANIMATE TO SELECTED POSITION tempcolor=newPick.color newPick.color=color.yellow target=newPick.pos step=(target-scene.center)/20. for i in arange(1,20,1): rate(10) scene.center +=step scene.scale *= 1.037 #(1.037**19=1.99) newPick.color=tempcolor if scene.kb.keys: # is there an event waiting to be processed? s = scene.kb.getkey() # obtain keyboard information if s=='z': print "scene.center=(%f,%f,%f)" % tuple(scene.center) print "scene.forward=(%f,%f,%f)" % tuple(scene.forward) print "scene.range=(%f,%f,%f)" % tuple(scene.range) if s=='n': colorScheme = (colorScheme+1)%2 #TOGGLE colorScheme scene.background=colorBackground[colorScheme]