The other day I discovered that the folks working on VPython have been very busy over the last couple of years. I had given up on VPython because the Mac distribution was so difficult to use. It required the X windows server and one point and lots of other extra junk at other times. But now things are easy! There is a great installer package and it just works.
For those of you who have not heard of VPython before it is a 3D graphics module called visual built on top of Python and OpenGL. You can do a lot of nice 3D graphics very simply with VPython. Its great for education in math and physics and introductory computer science.
So as an experiment I built a turtle graphics module on top of VPython. I use turtle graphics a lot in teaching but the TKinter turtles all suffer from some event loop problems when you use them with IDLE. VPython does not suffer from this problem plus gives you a lot of other cool benefits. One of the benefits is that the window resizes automatically for you based on the units you use in your application. Want to draw a picture at the atomic level of detail? No problem, want to draw planets circling each other? Again no problem.
Here's a screen capture of a fractal tree created in 3-D using my new turtle.
Here's the VPython turtle code:
def tree(t,trunkLength):
if trunkLength < 5:
return
else:
turnDz = random.randint(20,40)
turnDx = random.randint(20,40)
trunkShort = random.randint(10,20)
t.width(trunkLength/10.0)
if trunkLength < 25:
t.color(color.green)
else:
t.color((174/255.0,145/255.0,0))
t.forward(trunkLength)
# right trunk
t.right(turnDz,ZAXIS)
tree(t,trunkLength-trunkShort)
# left trunk
t.left(2turnDz,ZAXIS)
tree(t,trunkLength-trunkShort)
# front trunk
t.right(turnDz,ZAXIS)
t.right(turnDx,XAXIS)
tree(t,trunkLength-trunkShort)
# back trunk
t.left(2turnDx,XAXIS)
tree(t,trunkLength-trunkShort)
# restore
t.right(turnDx,XAXIS)
t.backward(trunkLength)
If you want to check out the turtle module and play with it, you are welcome to do so: hg clone http://bitbucket.org/bnmnetp/vturtle Or send me mail.
Comments
comments powered by Disqus