[Edu-sig] Working with Vpython
kirby urner
kirby.urner at gmail.com
Thu Sep 14 06:45:34 CEST 2006
On 9/13/06, kirby urner <kirby.urner at gmail.com> wrote:
> Given stickworks is provided, student creativity will be more in what
> they can *do* with it.
> I'll be thinking of some projects.
>
Probably a most obvious application for an Edge connecting pairs of
vectors, is to do simple plotting, either in xy or in xyz.
Now that we have generators, it's easy to think of a functions domain
as a kind of indefinite loop:
>>> def dgen(start, step):
while True:
yield start
start += step
>>> domain = dgen(-10, 0.1)
Similar to xrange, but spelled out. Could be fancier, including a stop point.
Then, if we wanted to graph something like cosine, would could pass
both the domain and the function as arguments to a plot generator:
>>> import math
>>> def f(x): return math.cos(x)
Like, if you wanted 200 points, you could just go:
>>> mkgraph = xyplotter(domain, f) # initialize a generator
>>> for i in xrange(200):
mkgraph.next()
This'd give you the sinusoidal wave in VPython, complete with zoom
in/out and rotate mouse action.
Picture:
http://www.4dsolutions.net/ocn/graphics/cosines.png
Of course you might also want some axes to make the graph more readable.
So... adding xyplotter and axes to stickworks.py:
====
def xyplotter(domain, f):
x0 = domain.next()
y0 = f(x0)
while True:
x1 = domain.next()
y1 = f(x1)
e = Edge( Vector((x0, y0, 0)), Vector((x1, y1, 0)) )
e.draw()
yield None # could yield something more explicit
x0, y0 = x1, y1 # keep last point, get next
def axes(x,y,z):
v0 = Vector((x,0,0))
v0.draw()
v0 = Vector((-x,0,0))
v0.draw()
v0 = Vector((0,y,0))
v0.draw()
v0 = Vector((0,-y,0))
v0.draw()
v0 = Vector((0,0,z))
v0.draw()
v0 = Vector((0,0,-z))
v0.draw()
====
see: http://www.python.org/ocn/python/stickworks.py
There's a lot more basic algebra to explore. Parametric equations,
various curves... All that stuff we do with TI calculators today, and
loosely call "precalculus".
I don't think the 'generator' concept, using yield instead of return,
is that difficult to understand, and so tend to introduce it early on.
By this time, generators are intrinsic to core Python, not esoteric
"advanced features" (although itertools may be considered esoteric).
Kirby
Note: One possible student enchancement might be to stick little balls
at the ends of the vector and edge cylinders, to make 'em more
rounded, less likely to show gaps.
More information about the Edu-sig
mailing list