# [Edu-sig] Working with Vpython

kirby urner kirby.urner at gmail.com
Thu Sep 14 02:01:58 CEST 2006

For just starting to explore with VPython window, getting one's feet
wet, I'm looking at such as stickworks.py as a simple place to begin
(excerpts below).

The stickworks namespace wants us to think of Vector objects as
radiating from (0,0,0), i.e. they're always tail anchored to the
origin.

class Vector (object):

"""
A wrapper for visual.vector that expresses a cylinder via draw(),
always pegged to the origin
"""

def __init__(self, xyz, color=(0,0,1)):
self.v = vector(*xyz)
self.xyz = xyz
self.color = color
self.cyl = None

def draw(self):
"""define and render the cylinder"""
self.cyl = cylinder(pos = (0,0,0), axis = self.v, radius =

def erase(self):
"""toss the cylinder"""
if self.cyl:
self.cyl.visible = 0
self.cyl = None

def __repr__(self):
return 'Vector @ (%s,%s,%s)' % self.xyz

# some vector ops, including scalar multiplication

def cross(self, other):
temp = cross(self.v, other.v)
return Vector((temp.x, temp.y, temp.z))

etc.

Edges, on the other hand, otherwise known as line segments, may
connect any two points in space, and as such are defined by the arrow
*tips* of two Vectors of the type defined above.

class Edge (object):

"""
Edges are defined by two Vectors (above) and express as cylinder via draw().
"""

def __init__(self, v0, v1, color=(1,0,0)):
self.v0 = v0
self.v1 = v1
self.color = color
self.cyl = None

def draw(self):
"""define and render the cylinder"""
temp = (self.v1 - self.v0).xyz
self.cyl = cylinder(pos = self.v0.xyz, axis = vector(*temp),

def erase(self):
"""toss the cylinder"""
if self.cyl:
self.cyl.visible = 0
self.cyl = None

def __repr__(self):
return 'Edge from %s to %s' % (self.v0, self.v1)

You'll find this dichotomy in some published linear algebra books e.g.
this one by Wayne Bishop and Stewart Venit ISBN: 087150300X (Wayne
being a Cal State prof and one of my sparring partners on math-teach).

I'm not using VPython's cylinder and vector objects directly because
I'm wanting an even  simpler namespace.  Vectors are my primary
constituent, followed by edges as pairs of vectors, and faces as
tuples of vectors defining three or more edges.

Of course in the Fuller School we quickly move to a small subset of
polyhedra organized in a particular way (or core mandala, source of
many dharmas).  We harp on Euler's V + F = E + 2.  We talk in an alien

But that doesn't mean every gnu math teachers needs to follow our lead
and/or sequence.  The curriculum is a network, and the many trailheads
go to many places (partially overlapping) in different orders.

Plus Arthur of Pygeo has a whole other toolset you could explore.
Save the Bucky stuff for later, if ever.  Tell 'em I said it was OK.

The sequence I'm developing here is:

intro to namespaces --> import stickworks as a namespace --> explore in VPython

Given stickworks is provided, student creativity will be more in what
they can *do* with it.
I'll be thinking of some projects.

Dissecting and tweaking source code will also be a focus, as fluency
gradually develops, plus writing new modules that assume the above as
all given.

Kirby