uct logo PfP logo

Introductory examples

Example 1.

Uses the visual package to produce a simple, or even simplistic, animation.

This code just uses a simple algorithm to produce a planet moving around a sun:

from visual import *

r=5.0
theta=0.0
dtheta=0.01
sun=sphere(color=color.yellow)
earth=sphere(pos=(r,0,0),color=color.blue)
while 1:
    rate(20)
    theta=theta+dtheta
    x=r*cos(theta)
    y=r*sin(theta)
    earth.pos=(x,y,0)

Example 2.

Uses the pylab package to plot a simple graph.

Plotting a simple graph:

from pylab import *

angle=arange(0.0,2.0*pi,0.01)
plot(angle,sin(angle))
xlabel("angle")
ylabel("sine")
show()