""" Stuff to do with VPython vpython.org by Kirby Urner 4D Solutions 4dsolutions.net Started: May 2, 2008 Last upgraded: May 3, 2008 If you're on the Internet, why not keep a link to the documentation handy in your browser. http://vpython.org/webdoc/visual/index.html """ from visual import * # get a lot of stuff import math # definitely wanna have math in the picture from random import random, randint # don't use randint yet, but might def stage(): scene = display(title='VPython Window', width=600, height=600, center=(0,0,0), background=(0,1,1)) return scene def firsttry(): # no arguments here """ The cylinder Object """ s = stage() s.autoscale = 0 s.scale = (0.5, 0.5, 0.5) # we figure out a good view rod = cylinder(pos=(0,0,0), axis=(5,0,0), radius=0.1) rod.color = color.green # but let's make it go around in circles: for i in range(500): angle = math.radians(i) # increment in degrees rod.axis = (math.cos(angle), math.sin(angle), 0) rate(100) def clock(): # no arguments here """ two arrow objects, one going around more slowly, night happens note: there's also a way to do rotation in XY by multiplying complex numbers, thanks to various identities (see orbits.py) """ s = stage() s.autoscale = 0 s.scale = (0.3, 0.3, 0.3) rod1 = arrow(pos=(0,0,0), axis=(3,0,0), shaftwidth=0.1) rod2 = arrow(pos=(0,0,0), axis=(1,0,0), shaftwidth=0.1) rod1.color = color.green rod2.color = color.red # clockwise this time, shorter one at 1/10th speed: for i in range(360*4,-1,-1): # four circles of angle angle1 = math.radians(i) # decrement in degrees angle2 = angle1/10.0 if i == 360*2: # switch to night view half way s.background = color.black rod1.axis = (3 * math.cos(angle1), 3 * math.sin(angle1), 0) rod2.axis = (1 * math.cos(angle2), 1 * math.sin(angle2), 0) rate(100) def balls(): # no arguments here """ arrow object leaves trail of balls """ s = stage() s.autoscale = 0 s.scale = (0.3, 0.3, 0.3) rod1 = arrow(pos=(0,0,0), axis=(3,0,0), shaftwidth=0.1) rod1.color = color.green # clockwise again for i in range(360,-1,-1): # one circles of angle angle1 = math.radians(i) # increment in degrees position = (3 * math.cos(angle1), 3 * math.sin(angle1), 0) rod1.axis = position ball = sphere(pos=position, radius=0.2) rate(100) def spaceballs(): # no arguments here """ arrow object leaves trail of balls, spaced by 10 degrees """ s = stage() s.autoscale = 0 s.scale = (0.3, 0.3, 0.3) rod1 = arrow(pos=(0,0,0), axis=(3,0,0), shaftwidth=0.1) rod1.color = color.green # clockwise again for i in range(360,-1,-1): # one circles of angle angle1 = math.radians(i) # increment in degrees position = (3 * math.cos(angle1), 3 * math.sin(angle1), 0) rod1.axis = position if not (i % 10): ball = sphere(pos=position, radius=0.2, color = (random(), random(), random()) ) rate(100) def rings(): """ Have a ball travel around on a ring """ s = stage() s.autoscale = 0 s.scale = (0.3, 0.3, 0.3) thering = ring(pos=(0,0,0), axis=(0,0,1), # OK to comment this way radius=2.5, thickness=0.1, color=color.green) theball = sphere(pos=(2.5,0,0), radius=0.2, color = color.orange) # clockwise again for i in range(360*5,-1,-1): # five circles of angle angle1 = math.radians(i) # increment in degrees position = (2.5 * math.cos(angle1), 2.5 * math.sin(angle1), 0) theball.pos = position rate(100) def pyramids(): """ Desert plane, some Egyptian pyramids """ s = stage() s.backcolor = color.blue # dark blue sky s.autoscale = 0 s.scale = (0.1, 0.1, 0.1) mybox = box(pos=(0,0,0), length=10, height=0.1, width=10, color = color.yellow) p1 = pyramid(pos=(0,0,0), axis = (0,1,0), size=(4,2,2), color = color.red) p2 = pyramid(pos=(-4,0,-3), axis = (0,1,0), size=(5,1,1), color = color.green) p3 = pyramid(pos=(2,0,4), axis = (0,1,0), size=(6,2,2), color = color.orange) def lissa(): # you could make a, b arguments if you want """ Lissajous curves: Other ideas of a, b http://www.2dcurves.com/higher/higherli.html """ s = stage() s.autoscale = 0 s.scale = (0.5, 0.5, 0.5) a = 3.0 b = math.pi/2 for t in range(360 * 4): # 0-360 degrees * cycles x = math.sin(math.radians(t)) y = math.sin(a * math.radians(t) + b) ball = sphere(pos=(x, y, 0), radius=0.1, color = color.orange) rate(100) def olympics(): s = stage() s.autoscale = 0 s.scale = (0.1, 0.1, 0.1) s.background = (1,1,1) # intersecting, not chained -- check the actual logo thering = ring(pos=(-5,1,0), axis=(0,0,1), radius=2.3, thickness=0.1, color=color.blue) thering = ring(pos=(0,1,0), axis=(0,0,1), radius=2.3, thickness=0.1, color=color.black) thering = ring(pos=(5,1,0), axis=(0,0,1), radius=2.3, thickness=0.1, color=color.red) thering = ring(pos=(-2.5,-1,0), axis=(0,0,1), radius=2.3, thickness=0.1, color=color.yellow) thering = ring(pos=(2.5,-1,0), axis=(0,0,1), radius=2.3, thickness=0.1, color=color.green) if __name__ == '__main__': # firsttry() clock() # balls() # spaceballs() # rings() # pyramids() # lissa() # olympics()