POLYGONS is a fairly short and simple program which produces quite an impressive display and illustrates some useful programming techniques.

After selecting the number of sides, a polygon is drawn which tumbles and spins about its horizontal and vertical axes.

The polygon is constantly being drawn, erased, rotated and re-drawn to produce a good animated display. Unfortunately, the only way to achieve fast-enough animation is through the use of machine code, though this has been kept to a minimum.

The best way to explain how the program works is to show how it was developed from a very simple idea. There are several short programs to type in at each stage, so switch your Amstrad on and let's first try to draw an ellipse.

Casting my mind back to my school days I can dimly recall that the coordinates of any point on the circumference of an ellipse is minor*COS(theta), major*SIN(theta). Where major and minor are the axes and theta is the angle. Figure 1 shows it a bit more clearly.

Program 1 attempts to draw such an ellipse. Theta is used as a loop counter, running from 0 to 360 degrees and each point is joined using draw commands. Run it and see what happens.

It's not quite right is it? The problem is that it's drawn around the origin in the bottom left-hand corner of the screen.

Either the origin can be moved, or a constant can be added to all the coordinates to get round this.

Program 2 uses this second method to draw the ellipse in the centre of the screen. 320 is added to the x coordinate and 200 to the y.

It's better, but still not quite right as there's a line drawn from the origin to the ellipse. What we forgot to do is to move to the first point before we draw anything.

Program ill adds the necessary MOVE command. The program now draws a perfect ellipse and we can start working on it.

Try altering the size of the major and minor axes and see what happens. Notice that when they are equal you get a circle.

It's very slow at drawing the ellipse, so try altering the step size in the FOR ... NEXT loop. Add:

STEP 60

to the end of line 60 in Program 3 and run it again.

The ellipse has become a hexagon. It has six sides because 360, the loop limit, divided by 60, the step size is 6. The step size alters the number of sides so we can draw an n sided polygon using a step size of 360 divided by n.

Suppose we draw the polygon with the loop counter theta running from 0 to 360. Then if we erase it and draw it again with theta running from 6 to 366 it will appear to have rotated through six degrees.

Try altering the loop in line 60 of Program 3 and see what happens. Just add a constant on to the start and finish of the FOR ... NEXT loop.

Take a look at Program IV. What it does is to alter the start and finish of the loop which draws the polygon by the value of angle, clearing the screen every time the polygon is drawn. An outer loop is used to increment angle by 6 each time the polygon is drawn.

This short program now draws a rotating polygon. It's very flickery but we can get round that using machine code in the final program.

The polygon is only rotating about one axis at the moment. To make it appear to rotate about its other axis, what we do is to introduce another loop outside the two we've already got. In Program 5 this outer loop varies the size of the major axis. It now rotates about two axes.

We've got everything we need now to produce a fast animated display of a rotating polygon. Any polygon can be drawn at any angle with any degree of rotation using this last program. Program 6, Rotating Polygons, was developed from this.

The Basic part of Polygons calculates the coordinates of the corners of the polygon and stores them in the memory. Trigonometric calculations take a relatively long time, so working out the coordinates beforehand greatly improves performance.

The polygon is rotated through a small angle and the new coordinates of the corners calculated and stored after the first. This is repeated until the polygon is back in its original position. This takes a long time, so be patient!

Once all the coordinates are in the memory, a machine code routine runs through the data moving, drawing and erasing the polygon. It's very fast, displaying several frames a second, mainly because it doesn't need to do any calculations. It's a technique worth remembering. For those of you who'd like to unravel it, an assembler listing is given in Figure 2.

As a project, how about trying to write a routine that will rotate a prism such as a pyramid? Use the same method as I've described here. It's not as hard as it might seem - and quite interesting, too.