[Edu-sig] More code for VPython (beginner)
kirby urner
kirby.urner at gmail.com
Fri May 2 22:42:00 CEST 2008
Below (appended code) is the kind of thing I use to introduce VPython,
some high school math hoped for, in that there's a little bit of trig,
an allusion to complex numbers.
The fun here is VPython lets us put colorful geometric objects on
screen then change their position at runtime, so it's not like some
ray tracing program. So you can have a ball traveling around in a
ring, or clock hands and stuff, all without any overwhelming amount of
code.
Six working examples in under 200 lines, including blank ones, ain't
bad. But then I don't try out all the objects. This is a genre
nothing exhaustive -- one could imagine thousands of such examples in
a database somewhere.
Another example on YouTube:
http://www.youtube.com/watch?v=QXCPDsBN2Kc (looks like an obelisk,
thinking of the late Arthur C.C.).
Dr. Sonnenfeld in New Mexico provides the source underneath but
Youtube de-indents for us, so here it is again:
from visual import *
from random import randint
thecolors = [color.green, color.red, color.orange, color.magenta]
for i in range(10000):
place = (randint(-100,100), randint(-100, 100), randint(-10,10))
rcolor = randint(0,3)
ball = sphere(pos = place, radius=0.5, color = thecolors[rcolor])
You can just hit F5 in IDLE's text editor and it'll run. Likewise for
the examples below, I have students go to the bottom and progressively
uncomment each test, hit F5 to run, always in the same IDLE session
(shell restarts by itself, upon close of VPython window, very smooth).
Dunno who many of you are using VPython in classes. I've got a
Windows lab going at the local university with Python 2.5, VPython,
POV-Ray on every workstation.
For those on this list for a long time: Arthur Siegel wrote the most
ambitious projective geometry package in Python, starting from his own
fascination with that subject (not programming per se, he came from
financial circles), and we used to argue about whether VPython should
just be a part of the standard library, given how powerful it is (we
argued about a lot of things -- miss the guy, he died very suddenly,
the website is here: http://pygeo.sourceforge.net/ ).
I do find that easy access to an OpenGL type space does marvels for
language appeal, and given I'm recruiting in the open market, I'm glad
to have VPython in my toolkit, really helps make my Saturday Academy
class a viable elective. All the buzz about Python in general also
helps of course.
Kirby
4D
Portland, Oregon
"""
Stuff to do with VPython
vpython.org
by Kirby Urner
4D Solutions
4dsolutions.net
May 2, 2008
If you're on the Internet, why not keep a
link to the documentation handy in your
browser.
"""
from visual import * # get a lot of stuff
import math # definitely wanna have math in the picture
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
s.select()
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)
s.select()
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 = color.orange)
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)
s.select()
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)
if __name__ == '__main__':
# firsttry()
# clock()
# balls()
# spaceballs()
# rings()
pyramids()
More information about the Edu-sig
mailing list