def sand(self): self.top = cone(pos=(0, self.time * 1.1, 0), axis=(0, -1, 0), color=(1, 1, 0)) self.bottom = cone(pos=(0, -self.time * 1.1, 0), axis=(0, 1, 0), color=(0.5, 0.5, 0)) self.falling = cylinder( pos=(0, self.time * 0.125, 0), axis=(0, -1, 0), color=(0.5, 0.5, 0), radius=self.time * 0.035 ) self.countdown = sphere(pos=(0, self.time * 1.5, 0), radius=0)
def animate_motion(x, k): # Animate using Visual-Python CO = zeros((n, 3)) B2 = zeros((n, 3)) C1 = zeros((n, 3)) C3 = zeros((n, 3)) CN = zeros((n, 3)) for i, state in enumerate(x[:,:5]): CO[i], B2[i], C1[i], C3[i] = rd.anim(state, r) # Make the out of plane axis shorter since this is what control the height # of the cone B2[i] *= 0.001 C1[i] *= r C3[i] *= r CN[i, 0] = state[3] CN[i, 1] = state[4] from visual import display, rate, arrow, curve, cone, box black = (0,0,0) red = (1, 0, 0) green = (0, 1, 0) blue = (0, 0, 1) white = (1, 1, 1) NO = (0,0,0) scene = display(title='Rolling disc @ %0.2f realtime'%k, width=800, height=800, up=(0,0,-1), uniform=1, background=white, forward=(1,0,0)) # Inertial reference frame arrows N = [arrow(pos=NO,axis=(.001,0,0),color=red), arrow(pos=NO,axis=(0,.001,0),color=green), arrow(pos=NO,axis=(0,0,.001),color=blue)] # Two cones are used to look like a thin disc body1 = cone(pos=CO[0], axis=B2[0], radius=r, color=blue) body2 = cone(pos=CO[0], axis=-B2[0], radius=r, color=blue) # Body fixed coordinates in plane of disc, can't really be seen through cones c1 = arrow(pos=CO[0],axis=C1[0],length=r,color=red) c3 = arrow(pos=CO[0],axis=C3[0],length=r,color=green) trail = curve() trail.append(pos=CN[0], color=black) i = 1 while i<n: rate(k/ts) body1.pos = CO[i] body1.axis = B2[i] body2.pos = CO[i] body2.axis = -B2[i] c1.pos = body1.pos c3.pos = body1.pos c1.axis = C1[i] c3.axis = C3[i] c1.up = C3[i] c3.up = C1[i] trail.append(pos=CN[i]) i += 1
def sand(self): self.top = cone(pos=(0, self.time * 1.1, 0), axis=(0, -1, 0), color=(1, 1, 0)) self.bottom = cone(pos=(0, -self.time * 1.1, 0), axis=(0, 1, 0), color=(.5, .5, 0)) self.falling = cylinder(pos=(0, self.time * 0.125, 0), axis=(0, -1, 0), color=(.5, .5, 0), radius=self.time * 0.035) self.countdown = sphere(pos=(0, self.time * 1.5, 0), radius=0)
def __init__(self, w): """Store walker being displayed. Set up stanceLeg and swingLeg to be cylinders. stanceLeg extends from stance foot position to body position. swingLeg extends from body position to swing foot position.""" self.w = w # the walker being displayed d = 0.06 # thickness of legs self.stanceLeg = V.cone(pos=w.GetBodyPos(), radius = d, color = V.color.red, axis = -w.GetBodyPos()+w.GetStanceFootPos()) self.swingLeg = V.cone(pos=w.GetBodyPos(), radius = d, color = V.color.orange, axis = w.GetSwingFootPos()-w.GetBodyPos())
def draw_at(self, P, step): """Draw the field vector at the given point.""" B = self[P] if self.avg_mag == 0: val = 0 else: #get value in between smallest and largest val = 1-((B.mag-self.smallest_mag)\ /(self.largest_mag-self.smallest_mag)) #set size of pointers, radius = step/rad rad = 15 cone(pos = P, axis = B.norm(), radius=step/rad, length=step, display = self.scene, color = interpolate(self.color, val), opacity = val)
def __init__(self, w): """Store walker being displayed. Set up stanceLeg and swingLeg to be cylinders. stanceLeg extends from stance foot position to body position. swingLeg extends from body position to swing foot position.""" self.w = w # the walker being displayed d = 0.06 # thickness of legs self.stanceLeg = V.cone(pos=w.GetBodyPos(), radius=d, color=V.color.red, axis=-w.GetBodyPos() + w.GetStanceFootPos()) self.swingLeg = V.cone(pos=w.GetBodyPos(), radius=d, color=V.color.orange, axis=w.GetSwingFootPos() - w.GetBodyPos())
def my_arrow(pos, axis, proportion=True, shaftwidth=0.1, headwidth=2, headlength=3, color=[1.0, 1.0, 1.0]): px, py, pz = pos x, y, z = axis sw = shaftwidth length = math.sqrt(x*x + y*y + z*z) if proportion: sw *= length hl = sw*headlength/length l = 1.0 - hl lx = l*x; ly = l*y; lz = l*z cylinder_axis = [lx, ly, lz] cone_pos = [px+lx, py+ly, pz+lz] cone_axis = [hl*x, hl*y, hl*z] cone_radius = sw*headwidth visual.cylinder(pos=pos, axis=cylinder_axis, radius=sw, color=color) visual.cone(pos=cone_pos, axis=cone_axis, radius=cone_radius, color=color)
def my_arrow(pos, axis, proportion=True, shaftwidth=0.1, headwidth=2, headlength=3, color=[1.0, 1.0, 1.0]): px, py, pz = pos x, y, z = axis sw = shaftwidth length = math.sqrt(x * x + y * y + z * z) if proportion: sw *= length hl = sw * headlength / length l = 1.0 - hl lx = l * x ly = l * y lz = l * z cylinder_axis = [lx, ly, lz] cone_pos = [px + lx, py + ly, pz + lz] cone_axis = [hl * x, hl * y, hl * z] cone_radius = sw * headwidth visual.cylinder(pos=pos, axis=cylinder_axis, radius=sw, color=color) visual.cone(pos=cone_pos, axis=cone_axis, radius=cone_radius, color=color)
def draw_at(self, P, step): """Draw the field vector at the given point.""" B = self[P] if self.avg_mag == 0: val = 0 else: #get value in between smallest and largest val = 1-((B.mag-self.smallest_mag)\ /(self.largest_mag-self.smallest_mag)) #set size of pointers, radius = step/rad rad = 15 cone(pos=P, axis=B.norm(), radius=step / rad, length=step, display=self.scene, color=interpolate(self.color, val), opacity=val)
def setup_body(self): frame = visual.frame() self.parts = Bunch() self.parts.body = visual.cone( color=visual.color.white, length=.8, radius=.2, pos=(-.4, 0, 0), frame=frame) self.parts.l_ear = visual.cone( color=visual.color.white, length=.1, radius=.02, pos=(.05, .04, .06), axis=self.zaxis, frame=frame) self.parts.r_ear = visual.cone( color=visual.color.white, length=.1, radius=.02, pos=(.05, -.04, .06), axis=self.zaxis, frame=frame) self.parts.l_eye = visual.sphere( color=visual.color.red, radius=.03, pos=(.1, .04, .06), frame=frame) self.parts.r_eye = visual.sphere( color=visual.color.red, radius=.03, pos=(.1, -.04, .06), frame=frame) self.rat = frame
def show(self): """ Create and show the blob """ c = (1, 0, 0) if self.color == 1 else ( 1, 0.5, 0) #change colors here: (1, 0, 0) for red, (1, 0.5, 0) for gold if self.shape == 1: blob = sphere(pos=(self.x, self.y, 0.0), radius=object_side / 2, color=c) else: blob = cone(pos=(self.x, self.y, -object_side / 2), axis=(0, 0, object_side), radius=object_side / 2, color=c) blob.visible = True self.b = blob self.visible = True
velocity += h*Acc_A1(t) height += h*velocity temp = Temp(height) g = Grav(height) press = Press(temp,g) air_resis = Air_Resistance(press, temp) else: # if t is greater than 100s, the fuel is used up; velocity does not change. height_points.append(height) velocity_sync = velocity + 0.5*h*Acc_A2(t) # velocity is half-step off, so to plot the correct velocity for the correct times we need to append the synced velocity velocity_points.append(velocity_sync) velocity += h*Acc_A2(t) height += h*velocity temp = Temp(height) g = Grav(height) press = Press(temp,g) air_resis = Air_Resistance(press, temp) #----------------------------- Part 4: VPython ----------------------------------------------------------------------------------------------------------------------------------------------------------- # Create an animation for the launched rocket reaching the ISS scene1 = vis.display (title = 'Animation of Launched Rocket to the ISS: Max Payload') # create three objects: Earth, the International Space Station, and the rocket. Earth = vis.sphere(pos=[0.0, -250.0, 0.0], radius = 50.0, color=vis.color.green) ISS = vis.sphere(pos = [0.0, 160.0, 0.0], radius = 5.0, color=vis.color.magenta) rocket = vis.cone(pos=[0.0, -200.0, 0.0], axis = (0.0, 10.0, 0.0), radius = 2.0, color=vis.color.cyan) # define the rate of the animation and the height of the rocket using the data from the previous parts (heigh_points) for item in height_points: vis.rate (500) rocket.pos.y = (item/1000.0) - 200.0
def __init__(self,spos,sColor, fr): self.base = vis.cone(frame=fr, pos=spos,radius=0.4,axis=(0,1,0),color=sColor)
def __init__(self,spos,sColor, fr): self.base = vis.frame(pos=spos, frame=fr) vis.cylinder(frame=self.base,pos=(0,0,0),radius=0.2,length=0.8,axis=(0,1,0),color=sColor) vis.cone(frame=self.base,pos=(0,0.8,0),radius=0.2,axis=(0,1,0),color=sColor)
def __init__(self,spos,sColor, fr): self.base = vis.frame(frame = fr, sepos=spos) vis.box(frame=self.base,pos=(0,0.4,0),width=0.4,length=0.8,height=0.4,axis=(0,1,0),color=sColor) vis.cone(frame=self.base,pos=(0,0.6,0),radius=0.2,axis=(0,1,0),color=sColor)
# # See the exercise "Pendulum.pdf" from Pendulum.html # in http://www.physics.cornell.edu/sethna/StatMech/ComputerExercises/ # """Simple example animating the motion of a pendulum.""" # The "visual" module can create 3D objects (such as spheres, curves, etc.) # and animate their motions in space. # See www.vpython.org import visual # kludge required to get visual window up and running before import scipy/numpy scene2 = visual.display() c = visual.cone(radius=1.0e-10) c.visible = 0 # numpy allows us to use "array", "sin", "cos" etc. # numpy is also imported in "visual" from numpy import * # Physical properties and initial conditions for pendulum g = 9.8 L = 1.0 # physical length of bar d = 0.02 # thickness of bar: needed for graphics theta = 2. * pi / 3. # initial upper angle (from vertical) thetaDot = 0. # start pendulum at rest # Set up graphics display visual.scene.title = 'Pendulum' visual.scene.height = visual.scene.width = 800
################################ ######################################### # Import the library(s) ######################################### # Windows #import pylab #pylab.plot([1,2],[3,4]) #pylab.show() #import visual as vi #import numpy # Linux import visual as vi # kludge required to get visual window up and running before import scipy/numpy c = vi.cone(radius=1.0e-10) c.visible = 0 import numpy import pylab import RandomArray #import psyco #psyco.full() ######################################### # ListOfAtoms ######################################### class ListOfAtoms: #
def __init__(self, *a): super().__init__(*a) self.cone = v.cone(pos=self.pos, axis=(self.get_end(10)-self.pos)) self.trail = v.points(pos=[(0, 0, 0)])
print 'Using default v0 =', v0, 'm/s.' # Build 3d world # Define the line of sight sight = V.vector(cos(theta), sin(theta), 0) # The floor is just a thin box floor = V.box(pos=(D / 2., 0, 0), length=D, height=0.5, width=D, color=V.color.blue) # Use a cone for our 'arrow' arrow = V.cone(pos=(0, 0, 0), radius=0.9, axis=sight, color=V.color.red) arrow.velocity = v0 * sight # The target is a sphere target = V.sphere(pos=(D, H, 0), radius=1, color=V.color.yellow) target.velocity = V.vector(0, 0, 0) # The 'dart gun' is just a cylinder. gun = V.cylinder(pos=(0, 0, 0), axis=gun_len * sight, radius=1, color=V.color.green) # Run simulation print 'Starting simulation...'
def show(self, window): self.object = visual.cone(pos = tuple(self.point2), axis = tuple(self.point1-self.point2), radius = self.radius)
# See the exercise "Walker.pdf" from Walker.html # in http://www.physics.cornell.edu/~myers/teaching/ComputationalMethods/ComputerExercises/ # import visual as V V.scene.title = 'Walker' V.scene.height = V.scene.width = 600 # Follow walker as it moves forward V.scene.autocenter = 1 # Scale set automatically by system, but don't keep rescaling as walker moves V.scene.autoscale = 1 # Slow down walker to reasonable speed framesPerSecond = 120 # kludge required to get visual window up and running before import scipy/numpy c = V.cone(radius=1.0e-10) c.visible = 0 import Walker as W reload(W) # for ipython to reload properly after changes in Walker import scipy # Walker Display class WalkerDisplay: """Defines VPython cylinders for stanceLeg, swingLeg.""" def __init__(self, w): """Store walker being displayed. Set up stanceLeg and swingLeg to be cylinders. stanceLeg extends from stance foot position to body position. swingLeg extends from body position to swing foot position."""
height_points.append(height) velocity_sync = velocity + 0.5*h*Acc_A2(t) # velocity is half-step off, so to plot the correct velocity for the correct times we need to append the synced velocity velocity_points.append(velocity_sync) velocity += h*Acc_A2(t) height += h*velocity temp = Temp(height) g = Grav(height) press = Press(temp,g) air_resis = Air_Resistance(press, temp) print np.max(height_points) ''' #----------------------------- Part 4: VPython ----------------------------------------------------------------------------------------------------------------------------------------------------------- # Create an animation for the launched rocket reaching the ISS scene1 = vis.display (title = 'Animation of Launched Rocket to the ISS (payload = 0)') # create three objects: Earth, the International Space Station, and the rocket. Earth = vis.sphere(pos=[0,-250,0], radius = 50, color=vis.color.green) ISS = vis.sphere(pos = [0, 160, 0], radius = 5, color=vis.color.magenta) rocket = vis.cone(pos=[0, -200, 0], axis = (0, 10, 0), radius = 2, color=vis.color.cyan) # define the rate of the animation and the height of the rocket using the data from the previous parts (heigh_points) for item in height_points: vis.rate (500) rocket.pos.y = (item/1000) - 200 if rocket.pos.y >= 160: break
try: v0 = float(sys.argv[1]) except: v0 = v0_default print 'Using default v0 =',v0,'m/s.' # Build 3d world # Define the line of sight sight = V.vector(cos(theta),sin(theta),0) # The floor is just a thin box floor = V.box(pos=(D/2.,0,0), length=D, height=0.5, width=D, color=V.color.blue) # Use a cone for our 'arrow' arrow = V.cone(pos=(0,0,0), radius=0.9, axis=sight,color=V.color.red) arrow.velocity = v0*sight # The target is a sphere target = V.sphere(pos=(D,H,0), radius=1, color=V.color.yellow) target.velocity = V.vector(0,0,0) # The 'dart gun' is just a cylinder. gun = V.cylinder(pos=(0,0,0), axis=gun_len*sight, radius=1, color = V.color.green) # Run simulation print 'Starting simulation...' # Put a little delay to give all the OpenGL stuff time to initialize nicely.
# # See the exercise "Pendulum.pdf" from Pendulum.html # in http://www.physics.cornell.edu/~myers/teaching/ComputationalMethods/ComputerExercises/ # """Simple example animating the motion of a pendulum.""" # The "visual" module can create 3D objects (such as spheres, curves, etc.) # and animate their motions in space. # See www.vpython.org import visual # kludge required to get visual window up and running before import scipy scene2 = visual.display() c = visual.cone(radius=1.0e-10) c.visible = 0 # scipy allows us to use "array", "sin", "cos" etc. # scipy is also imported in "visual" from scipy import * # Physical properties and initial conditions for pendulum g = 9.8 L = 1.0 # physical length of bar d = 0.02 # thickness of bar: needed for graphics theta = 1.*pi/6. # initial upper angle (from vertical) thetaDot = 0. # start pendulum at rest # Set up graphics display visual.scene.title = 'Pendulum' visual.scene.height = visual.scene.width = 800
def game(): global player global target global missile global planets global barriers global scene global arrow global win global wintext ConfigureLevel() ### Player planet player = visual.sphere( pos = player_position, radius = player_radius, mass = player_mass, color = visual.color.green ) target = visual.sphere( pos = target_position, radius = target_radius, mass = target_mass, color = visual.color.red ) planets = [ visual.sphere(**planet ) for planet in planets_properties ] barriers = [ visual.curve (**barrier) for barrier in barriers_properties] scene.center = tuple(ComputeCenter( player, target, *(planets + barriers) ) ) scene.range = ComputeRange( player, target, *(planets + barriers) ) arrow = visual.arrow( pos = player_position, axis = (target.pos-player.pos).norm() * player_radius * 10, shaftwidth = player_radius * 0.5, headwidth = player_radius, headlength = player_radius, color = visual.color.cyan) scene.bind('mousemove',ChangeArrow) click = scene.waitfor('click') missile_direction = click.pos - player.pos missile = visual.cone( pos = missile_position(target), axis = missile_direction.norm() * missile_length, radius = missile_radius, mass = missile_mass, p = missile_direction * missile_momentum, color = visual.color.yellow, make_trail = True) scene.unbind('mousemove',ChangeArrow) arrow.visible = False del arrow while not Game_over(): visual.rate(100) missile.p += ComputeForce( player, target, *planets ) * time_step missile.pos += missile.p * time_step missile.axis = missile.p.norm() * missile_length if win: wintext = visual.text( text='You win', pos = scene.center + visual.vector(0,0,1e8), height = 1e7, align='center', font = 'Times', color=visual.color.green) # wintext.up = True break
# See the exercise "Walker.pdf" from Walker.html # in http://www.physics.cornell.edu/sethna/StatMech/ComputerExercises/ # import visual as V V.scene.title = 'Walker' V.scene.height = V.scene.width = 600 # Follow walker as it moves forward V.scene.autocenter = 1 # Scale set automatically by system, but don't keep rescaling as walker moves V.scene.autoscale = 1 # Slow down walker to reasonable speed framesPerSecond = 120 # kludge required to get visual window up and running before import scipy/numpy c = V.cone(radius=1.0e-10) c.visible = 0 import Walker as W reload(W) # for ipython to reload properly after changes in Walker import scipy # Walker Display class WalkerDisplay: """Defines VPython cylinders for stanceLeg, swingLeg.""" def __init__(self, w): """Store walker being displayed. Set up stanceLeg and swingLeg to be cylinders. stanceLeg extends from stance foot position to body position.
def __init__(self, *a): super().__init__(*a) self.cone = v.cone(pos=self.origin, axis=(self.end-self.origin), radius=self.thickness, color=(0.6,0.4,0))
def animate_motion(x, k): # Animate using Visual-Python CO = zeros((n, 3)) B2 = zeros((n, 3)) C1 = zeros((n, 3)) C3 = zeros((n, 3)) CN = zeros((n, 3)) for i, state in enumerate(x[:, :5]): CO[i], B2[i], C1[i], C3[i] = rd.anim(state, r) # Make the out of plane axis shorter since this is what control the height # of the cone B2[i] *= 0.001 C1[i] *= r C3[i] *= r CN[i, 0] = state[3] CN[i, 1] = state[4] from visual import display, rate, arrow, curve, cone, box black = (0, 0, 0) red = (1, 0, 0) green = (0, 1, 0) blue = (0, 0, 1) white = (1, 1, 1) NO = (0, 0, 0) scene = display(title='Rolling disc @ %0.2f realtime' % k, width=800, height=800, up=(0, 0, -1), uniform=1, background=white, forward=(1, 0, 0)) # Inertial reference frame arrows N = [ arrow(pos=NO, axis=(.001, 0, 0), color=red), arrow(pos=NO, axis=(0, .001, 0), color=green), arrow(pos=NO, axis=(0, 0, .001), color=blue) ] # Two cones are used to look like a thin disc body1 = cone(pos=CO[0], axis=B2[0], radius=r, color=blue) body2 = cone(pos=CO[0], axis=-B2[0], radius=r, color=blue) # Body fixed coordinates in plane of disc, can't really be seen through cones c1 = arrow(pos=CO[0], axis=C1[0], length=r, color=red) c3 = arrow(pos=CO[0], axis=C3[0], length=r, color=green) trail = curve() trail.append(pos=CN[0], color=black) i = 1 while i < n: rate(k / ts) body1.pos = CO[i] body1.axis = B2[i] body2.pos = CO[i] body2.axis = -B2[i] c1.pos = body1.pos c3.pos = body1.pos c1.axis = C1[i] c3.axis = C3[i] c1.up = C3[i] c3.up = C1[i] trail.append(pos=CN[i]) i += 1