Previous page
Next page

In addition to the ability to define objects such as constants and variables, WGL recognizes seven kinds of instructions: assignment, color, draw, erase, increment, decrement, and loop.  These instructions manipulate the drawing environment and objects that are used to produce images.

Before describing each of these instructions, it is important to have an understanding of the world coordinate system in which objects will be drawn.  The lower left hand corner of the WGL drawing window is always fixed at the origin, (0, 0).  The upper right hand corner of the window is fixed at (299, 299).  Hence, the drawing window is a logically square region 300 units wide by 300 units tall.  

The assignment statement is used to place a value of a particular type into a variable of the same type.  Assignment statements in WGL are often used to specify the size, shape, location, or orientation of an object.  The format of an assignment statement in WGL is: VARIABLE = EXPRESSION  where VARIABLE is replaced by a variable of some particular type and EXPRESSION is replaced by an expression of the same type.  For example: p1=(100,100) is a valid assignment statement, assuming p1 is a variable of type point.  This statement defines the location of p1 to be (100, 100).  It does not actually draw anything in the drawing area – the statement simply places the constant (100, 100) into p1.  To actually see the point, p1, it would be necessary to render it using the draw command discussed below.

The color statement is used to select a drawing color.  This statement does not actually place anything on the screen.  It is analogous to picking up a pen of a particular color.  All subsequent draw commands will use the specified color until it is changed by issuing another color command.  The format of the color statement is color(COLOR_NAME) where COLOR_NAME is a constant of type color.  For example, color(blue) would set the drawing color to blue.  The default drawing color is red.

The draw statement is used to display an object of type point, line, circle, or polygon, in the drawing area.  An object must be completely defined before it can be drawn.  The format of the draw statement is draw(OBJECT) where OBJECT is a variable of type point, line, circle, or polygon.  Constants cannot be directly drawn; neither can distances, colors, and counters.  For example, draw(l2) would be a valid draw statement as long as l2 had been declared as a line variable and an appropriate value had been assigned to it.

The erase statement is used to erase or “undraw” an object.  Essentially, erase is just a draw statement where the object is drawn in white.  Since white is the background color of the drawing area used in the Watson Graphics Lab, an object that has been previously drawn appears to be “erased” by this statement.  Normally, an object is drawn before being erased, although technically in WGL it is not an error to erase an object before it has been drawn.  All that is required is that the object be completely defined.  The format of the erase statement is erase(OBJECT) where OBJECT is a variable of type point, line, circle, or polygon.  For example, erase(l2) would be a valid erase statement as long as l2 had been declared as a line variable and an appropriate value had been assigned to it.  Distances, colors, and counters cannot be erased, since they cannot be drawn.

The increment statement increases the value held in a distance variable by some constant amount.  The format of the increment statement is increment(DISTANCE, AMOUNT) where DISTANCE is replaced by a distance variable and AMOUNT is replaced by a distance constant.  For example, increment(d1,5) increases the value stored in d1 by 5 (e.g., if d1 had a value of 25 before this statement was executed, it would have a value of 30 afterwards).  Note that only distance variables can be incremented.  Points, lines, circles, polygons, counters, and colors cannot be incremented.

The decrement statement is similar to the increment statement, except that instead of increasing the value held in a distance variable, it decreases that value by some constant amount.  The format of this statement is decrement(DISTANCE, AMOUNT) where DISTANCE is replaced by a distance variable and AMOUNT is replaced by a distance constant.  For example, decrement(d1,5) decreases the value stored in d1 by 5 (e.g., if d1 had a value of 25 before this statement was executed, it would have a value of 20 afterwards).  As with increment, only distance variables may be decremented.  Points, lines, circles, polygons, counters, and colors cannot be decremented.

The loop statement is used to repeat a group of statements a fixed number of times.  Loops are useful for drawing a series of similar objects and for creating simple animation sequences.  The format of the loop statement is:

where COUNTER is replaced by a positive integer constant in the range 1 to 99 and <one or more statements> is replaced by some number of WGL statements.  The loop statement works by repeating the group of statements between loop and endloopCOUNTER number of times.  For example, given the following loop:

the three statements: assignment, draw, and increment; will be repeated 5 times.  The effect of this loop is to draw a series of five concentric circles centered at (100, 100).


Return to top