CODINGLISTINGS ★ ROLAND WADDILOVE illustrates more useful programming ideas ★

Rotating Polygons (Computing with the Amstrad)Coding Listings
★ Ce texte vous est présenté dans sa version originale ★ 
 ★ This text is presented to you in its original version ★ 
 ★ Este texto se presenta en su versión original ★ 
 ★ Dieser Text wird in seiner Originalfassung präsentiert ★ 

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.

CWTA

★ PUBLISHER: Computing With The Amstrad
★ YEAR: 1985
★ CONFIG: 64K + AMSDOS
★ AUTHOR: ROLAND WADDILOVE
 

★ AMSTRAD CPC ★ DOWNLOAD ★

Type-in/Listings:
» Rotating  Polygons    (Computing  with  the  Amstrad)    LISTINGDATE: 2013-09-20
DL: 272
TYPE: PDF
SiZE: 513Ko
NOTE: 9 pages/PDFlib v1.6

» Rotating  Polygons    (Computing  with  the  Amstrad)    LISTINGDATE: 2013-05-06
DL: 192
TYPE: text
SiZE: 3Ko
NOTE:

Je participe au site:
» Vous avez des infos personnel, des fichiers que nous ne possédons pas concernent ce programme ?
» Vous avez remarqué une erreur dans ce texte ?
» Aidez-nous à améliorer cette page : en nous contactant via le forum ou par email.

CPCrulez[Content Management System] v8.7-desktop/c
Page créée en 419 millisecondes et consultée 1507 fois

L'Amstrad CPC est une machine 8 bits à base d'un Z80 à 4MHz. Le premier de la gamme fut le CPC 464 en 1984, équipé d'un lecteur de cassettes intégré il se plaçait en concurrent  du Commodore C64 beaucoup plus compliqué à utiliser et plus cher. Ce fut un réel succès et sorti cette même années le CPC 664 équipé d'un lecteur de disquettes trois pouces intégré. Sa vie fut de courte durée puisqu'en 1985 il fut remplacé par le CPC 6128 qui était plus compact, plus soigné et surtout qui avait 128Ko de RAM au lieu de 64Ko.