[Top] [Prev] [Next] [Bottom]

Animation2d Objects

An Animation2d object is a container for the controls for a two dimensional animation. The user supplies these controls, which are functions (written in Python) that initialize internal variables in the object, compute the coordinates for each frame, and update the internal variables. To see the animation performed, give the object to a Graph2d and ask the Graph to plot itself.

Currently Animation2d is not implemented in PyNarcisse.


from animation2d import *
anim = Animation2d ( <keylist>)


Animation2d accepts the following keyword arguments:

initialize, calculations, update, animation, nsteps, color

It also has methods new and set, which work the same as the methods with the same names in other 2d objects. See "Description" on page 9, for instance.

Keyword Arguments

The following keyword arguments can be specified with Animation2d:

initialize = <name of an initialization function>. This function should have one argument, the name of an Animation2d instantiation, say `anim', and when called should initialize any of anim's internal variables needed before beginning to compute the animation.
calculations = <calculation function(s) for coordinates>: the value of this keyword is the name of a function, or a list of names of functions. Each of the calculations routines should have `anim' as the argument. This routine (or these routines) are called from within a loop in the Plotter(s) associated with anim. They should compute the current values of anim.x and anim.y, the coordinates of the curve(s) in this step of the animation. The first frame starts with the results of initialize, then in subsequent calls, use the results of update (below). If more than one calculation is specified, then a plot command will be issued after each one.
update = <function to update the variables used in calculations>. This function, when called with `anim' as its sole argument, updates (increments, decrements) variables used in calculating the frames.
animation = 0/1 (If 1, supplies a smoother, less jerky animation. Default value 1.)
nsteps = number of animation steps desired. Default: 100.
color = <value> where <value> is an integer representing an index into a color chart, or a common color name like "red", "blue", "background", etc. In the interest of speed, other keywords relating to curve type, thickness, etc., are currently not allowed.


The following is an interesting example of ``dancing curves'', sine waves which appear to jump up and down and go around in circles.

def init ( self ) :
self.t = 2*pi*arange (400, typecode = Float) / 399.0
self.na1 = 1
self.nb1 = 5
self.na2 = 2
self.nb2 = 7
self.rc1 = 40.
self.rc2 = 160.
self.size = 40.
self.phase = self.theta = 0.
self.dtheta = pi / (self.nsteps - 1)
self.dphase = 2 * pi / (self.nsteps - 1)
def calc1 ( self ) :
self.cost = cos (self.theta)
self.sint = sin (self.theta)
self.x = self.rc1 * self.cost + \
self.size * cos (self.na1 * self.t)
self.y = self.rc1 * self.sint + \
self.size * sin (self.nb1 * self.t + self.phase)
def calc2 ( self ) :
self.x = self.rc2 * self.cost + \
self.size * cos (self.na2 * self.t)
self.y = self.rc2 * self.sint + \
self.size * sin (self.nb2 * self.t + self.phase)
def incr ( self ) :
self.theta = self.theta + self.dtheta
self.phase = self.phase + self.dphase
from animation2d import *
# instantiate an Animation2d without smoothness
anim = Animation2d ( initialize = init,
calculations = [calc1, calc2], update = incr,
animation = 0, nsteps = 200 )
g1 = Graph2d ( anim )
g1.plot ( )
# Now animate smoothly to see the difference.
anim.set (animation = 1)
g1.plot ( )

We have been unable to capture steps in the animation for this document; below is a pisture of the two curves after the animation has finished. You will have to try this example yourself to see the incredible effects!

[Top] [Prev] [Next] [Bottom]

Copyright © 1997,Regents of the University of California. All rights reserved.