Assignment #6: OOT Graphics
Fall 1997
To be completed by Oct. 15/16.
Objective: To learn how to use OOT graphics.
The video display tube on a computer screen is composed of a regular pattern of small dots called pixels. The pixels are arranged in the fashion of a rectangular grid. Images that appear on the screen are created by controlling the intensity (and hue, if the display is color) of each pixel. For example, text characters are created in a character space by making a specific pattern of the pixels in that space black, and the other pixels white. When you type out text, you naturally don't have to specify this pixel pattern for every character, as characters are formed from standardized pixel arrangements that the monitor knows about. When you work with graphics, you usually do not have to specify intensities and hues for individual pixels (although, you can), but you will have to supply pixel coordinates to indicate where to place a graphics object.
On the NCD19 Xterminals in SF1012, the resolution is 1280x1024, meaning that there are over 1.3 million individually controllable pixels on the screen, arranged in a rectangular pattern of 1280 columns and 1024 rows. Therefore, in OOT, you can create graphics windows that are up to 1280x1024 pixels in size. The pitch of the display, which is the spacing between pixels, is 0.01 inches (also described as 100 dots per inch). By contrast, a TV image (NTSC) consists of 525 lines of 440 dots each (relatively poor quality, at about 30 dots per inch on a 21" set), while many laser printers offer 600 dots per inch (near photographic quality).
As mentioned in the notes on computer graphics, you will be able to draw simple graphics objects without manipulating individual pixels. This is done using some of the built-in graphics procedures such as drawline, drawbox, drawoval, and others that are listed in the OOT reference manual. In addition, you can create customized graphics procedures that will perform specific graphics tasks if you supply some information. Here is a simple program that will illustrate the use of custom-built graphics procedures. Type it in and run it:
include "/usr/copy/aps105/images.t" setscreen ("graphics:300;400,nocursor") put "Hello World" DrawCircle (100,100) DrawHappyFace (200,300) DrawSadFace (200,100)This example makes use of three custom-built graphics procedures: DrawCircle, DrawHappyFace, and DrawSadFace. The code for these procedures is located in the file /usr/copy/aps105/images.t hence the include statement at the beginning.
The setscreen statement is necessary to inform OOT to set up a run window capable of displaying graphics. The 300;400 in the argument of setscreen indicates that the graphics window is to be 300 pixels wide and 400 pixels high. When locating objects in the graphics window, you must use a pixel address, which is simply an x,y co-ordinate. The bottom left corner is always 0,0. For this example, the lower right corner is 299,0, the upper left corner is 0,399, and the upper right corner is 299,399.
As can be expected from their names, the procedures DrawCircle, DrawHappyFace, and DrawSadFace draw a circle, a happy face, and a sad face respectively. The arguments for these procedure calls are the pixel addresses at which the images are to be centered. The circle that is drawn is centered at 100,100; the happy face is centered at 200,300 and the sad face is centered at 200,100.
Use repetition statements to create the pattern illustrated on the left hand side of Figure 1. For the column of circles use a for loop, and for the row of happy faces, use a loop loop. You may want to make use of the maxx and maxy symbols, which return the maximum x co-ordinate and the maximum y co-ordinate respectively. See your Turing reference manual for details.
Adjust the program to produce the pattern shown on the right hand side of Figure 1. Two additional statements should be all that is required. When you are finished, each of your loops will be controlling both x and y co-ordinate variables.
Figure 1: Face pattern 1 and pattern 2
Design and test a Turing program to produce a ``checkerboard" pattern of happy and sad faces, as shown in Figure 2.
Figure 2: Happy and sad checkerboard.
The programs in this assignment have illustrated how to create patterns of circles and faces using repetition. Objects can be made to appear to move across the screen using repetition if, when a new object is drawn, the old one is erased. Experiment with the for loops and loop loops to create a ``movie" of a happy face moving across the window. Delays may have to be added in between iterations of the loop to slow down the image. You may want to look up the cls and delay statements in the Turing reference manual.
Once you have figured out how to get the happy face moving across the screen, modify your program so that the happy face will continue to move around the window indefinitely. Have the happy face bounce into another direction when it reaches the edge of the window.
You may want to try this late at night or early in the morning when the system is not very loaded so that movements are smooth; if the system is heavily loaded then the movement will tend to be jerky.
In this exercise the only new graphics statement you will have to know is drawline, which you can look up in the Turing reference manual. You are to design, implement and test a Turing program which can plot any function as a graph. The function will appear as code in your program as follows:
function y (x: real): real result code to calculate function here end y
For example, the function f(x) = sin(x)cos(2x) would be coded as
function y (x: real): real result sin(x)*cos(2*x) end yYour functions may be considerably more complex if you wish.
The general steps your program will follow are:
Figure 3: Mapping real coordinates onto pixel coordinates
In this section you will implement a version of the drawline procedure using the drawdot builtin procedure, i.e., your program must compute where to draw dots to construct the line. To avoid confusion with the existing drawline procedure, call your procedure lineto. It should be used just like drawline.
Show that your procedure works by using it in a program. In your program, you should demonstrate that your procedure works for under all conditions for the endpoints, including endpoints that are off the screen. In the latter case, you can implement what is called clipping, where you draw the visible portion of the line.