2D Graphics and Sprite Animation in Mathscript

Sometimes, a 2D simulation can make a point more clearly and directly than an essay or a calculation can. In two dimensions, the Viewport of a graph2D gadget represents a rectangle in the Cartesian Plane, and can be given an underlying coordinate system for reference. The coordinate system can be Cartesian, Polar, or Blank, and in the Cartesian case, each graph2D keeps track of its horizontal and vertical extents, the origin of its coordinates (where the axes cross), the rulings along the axes, its pen color and width, whether it is drawing connected or dotted curves, its background color, the color that it uses to fill polygons, and all of the objects that it currently displays, among other things.

When a Graph2D object displays its "trace bar," the coordinates of the current mouse position are printed whenever the mouse is moved. In that case, one can "zoom in" by pressing the zoom button and drawing a rectangle that becomes the new rectangle represented by the object. Each window maintains its own stack of zoom states, so that one can zoom in many times, then zoom out to a previous zoom state, continue, and so on.

In fact, many of these properties of the graph2D gadget are under script control, and can be set interactively by command or script. Among these are:

Commands:

ABSCISSA Sets the horizontal extent represented by the graph2D.
BACKGROUND Sets the background color (16 million possibilities) of the graph2D.
CURVECONNECTED Determines whether graphs and curves are drawn as dashed or solid curves
CURVEDOMAIN Determines the interval that is the domain for parametric curves.
CURVEREFINEMENT Determines the refinement for parametric curves.
FILLCOLOR Sets the current color used to fill polygons (16 million possibilities) of the graph2D.
GRAPHDOMAIN Determines the interval that is the domain for graphs of functions.
GRAPHREFINEMENT Determines the refinement for drawing graphs of functions.
ORDINATE Sets the vertical extent represented by the graph2D.
ORIGIN Determines where the axes cross (need not be [0,0] )
P
ENCOLOR Sets the current drawing color (16 million possibilities) of the graph2D.
PENWIDTH Determines the width of the pen
RULINGS Determines the spacing of tickmarks along the axes
SETWINDOWCOLOR Same as Background
WALLPAPER Places a Bitmap, GIF or JPG in the window

Functions (Arguments in parentheses, return values):

GETABSCISSA () Gets the horizontal extent represented by the graph2D
GETBACKGROUND() Gets the background color (16 million possibilities) of the graph2D
GETFILLCOLOR() Gets the current color used to fill polygons (16 million possibilities) of the graph2D
GETORDINATE() Gets the vertical extent represented by the graph2D.
GETPENCOLOR() Gets the current drawing color (16 million possibilities) of the graph2D
GETPENWIDTH() Gets the current width of the pen

In addition to these settable and gettable properties, are the Logo properties (that have to do with an invisible turtle associated with each window) and the Sprite Animation properties. Think of sprites actors that may glide and/or rotate in the 2-dimensional plane. There are two types of sprites: Bitmap sprites and PolySprites. The Bitmap sprites are "pictures" that may be translated under program control. The PolySprites may translate, rotate, or expand and shrink under program control.

The Logo functions keep track of an invisible "Logo Turtle" that at any time knows its position, its heading direction, and its pen state, which can be up or down. The sprites may be supplied with the window each time it opens, or they may be created and destroyed interactively under program control. Until they are destroyed, they remain on the screen, even when it is cleared of other drawing objects, such as graphs, curves, and so on. The commands and functions that manage this are:

Commands:

FORWARD Moves the Turtle forward (or backward) at its present heading
HIDESPRITE Hides the named sprite
INIT Resets the Turtle to the origin, heading East
MOVE Moves the Turtle to the specified point
MOVESPRITE Moves the named sprite to the specified point
PENDOWN Puts the Turtle pen down
PENUP Puts the Turtle pen up
POLYSPRITE Creates a PolySprite and places it in the graph2D at the specified location
REMOVESPRITE Removes (destroys) a sprite, either Bitmap or PolySprite
ROTATE Turns the heading of the turtle through the specified angle
SHOWSPRITE Expands or contracts a PolySprite by the supplied nonzero factor
SPRITE Creates a Bitmap Sprite and places it in the graph2D at the specified location
TURNSPRITE Turns the specified sprite to the specified angle
TURNTO Turns the Turtle to the specified angle

Functions (Arguments in parentheses, return values):

SPRITEAT() Reports the current position of the named sprite
TURTLEANG () Reports the current heading of the Turtle
TURTLEPOS () Reports the current position of the Turtle

Finally, there are many drawing utilities to support the usual mathematical exploration in graph2D gadgets. These are:

Commands:

CLEAR Clears the graph window that is currently selected, or named in the argument
CLEARLINES Erases the numbered line, polyline, polygon, etc
CURVE   Draws the image of a curve in 2 dimensions. Use Plot for speed.
ERASECURVE Erases the numbered curve
ERASEGRAPH Erases the numbered graph
GRAPH   Draws the graph of a function. Use Plot for speed.
LABEL Prints text at a specified point in the current graph window
LEVELS Draws the level curves: f(x,y) = c for several values of level c
LINE Draws a line segment between two specified points.
PLOT An all purpose speedy graphing command
POINT Draws a point in the current 2D
POLYGON Draws and fills a polygon in the selected 2D graph window.
POLYLINE Draws a sequence of lines in the selected graph window
SCATTERPLOT Makes a scatter plot, in the selected graph window
SOLUTION Draws the implicit (level) curve: f(x,y) = c
UNLABEL Removes the text displayed at a point in the current 2D Graph window
WRITEBITMAP Saves the image of the selected Graph2D window to disk

Functions (Arguments in parentheses, return values):

COLOR (Expression red, Expression green, Expression blue) Used in in drawing and painting routines such as: pencolor, fillcolor, pagecolor, background, etc.
GETPOINT (No argument or FALSE argument) Returns a two element vector (VECTOR #2) of the coordinates of a single point at the location of the cursor in a 2D graph window when the left mouse button is clicked on and off.
GETPOINTS (VA Boolean) (EX number) Returns an array of points drawn from the currently-selected graph window
PENCOLORGL (Ex color from 0 to 15)   Used in in drawing and painting routines such as: pencolor, fillcolor, pagecolor, background, etc.