PW821040280143DGraphics.TXT
Issue #28, 28.Oct.82, page 15

3D GRAPHICS
Until a three dimensional television is available true 3D plotting is
impossible. By using the principles involved in simple geometry, however,
movement in three dimensions can be simulated. The functions involved are
scaling, perspective and rotation, all of which can be carried out using some
very simple equations and functions such as SIN and COS

    The example program given at the end of the article is written for a
ZX81. It may be used on any computer with PLOT or some such equivalent
statement such as SET. A number of variables are used in the program which
will need special alteration for other machines. They are:

MX and MY: the maximum x and y coordinates;
CX and CY: the centre about which the shapes are plotted. For convenience
these are set to the middle of the screen, at MX and MY.
X(n), Y(n) and Z(n): the X, Y and Z coordinates of the point. Only the X and
Y coordinates are plotted; the Z is used to calculate perspective.

    PLOT works with two parameters, X and Y; these allow two dimensions. The
Z axis can be imagined as being at right angles to the other axes, coming out
directly from the centre of the screen. A positive Z coordinate denotes a
position in front of the TV; a negative Z is behind the picture. The greater
the Z coordinate the closer the image will appear to be.

    Most computers have the zero points  on the axes at a corner of the
screen, which is not very convenient for functions. Therefore a false centre
must be set up, usually in the centre of the image. In the illustration
program CX and CY denote the artificial centre. To account for this a point
X,Y, where X and Y are relative to CX and CY, would be plotted as: PLOT
CX+X,CY+Y

    When a point is plotted in the program the array coordinates are not used
in the PLOT statement, X and Y are used instead; X(), Y() and Z() are used to
calculate the final position. If a number of complex shapes must be moved
around, it is simpler to have a separate centre for each shape.

    This is the process which determines whether a point lies within the
legal limits for the PLOT statement. A point is illegal if, with CX and CY
added, the point is greater than the MX and MY limits or less than zero.
Plotting outside these limits will normally cause an error.

    The Z coordinate is used as an offset to calculate for perspective. If
Z() is positive then the point is further away from the centre than if Z
would be zero. F is used to multiply a point to calculate the offset. I used
2 if Z was positive, 0.5 if it was negative or 1 if it was zero. You should
change this according to taste.

    If the entire shape is to be moved by a certain amount without the
relationship of individual points being changed, then it is simpler to alter
the centre about which the shape is plotted. If rotation, stretching or
shearing is needed then the following simple mathematical functions should be
used.

    If a shape needs rotating about any axis by N degrees or radians,
depending on your computer, the following functions need to be used:

Rotation about the Z axis
X=X*COS(N)-Y*SIN(N)
Y=Y*SIN(N)+X*COS(N)
Z=Z
Rotation about the X axis
Y=Y*COS(N)-Z*SIN(N)
Z=Z*SIN(N)+Y*COS(N)
X=X
Rotation about the Y axis
X=X*COS(N)-Z*SIN(N)
Z=Z*SIN(N)+X*COS(N)
Y=Y
    Objects may be stretched in any direction by increasing the X, Y or Z
coordinates by any amount.

    Shearing involves adding the Y coordinate to the X or Z coordinate and
stretching by 45.
