





    ***********************************************************************
      DDLG - A three dimensional surface plotter with hidden line removal
    ***********************************************************************

         written by laughlin in march 1994 for the great little hp48.
         it's much fun how easy it is to develop software on the hp48!

         please keep this document and the program code unchanged if
         you give it away, so every body has the ability to use this
         program in the the original state. note that this software
         is copyrighted but released to the public pool. selling and
         comercial use of this software in any form is prohibited.
         please let me know when you use any part of this software in
         your own programs just to see what is done with this code.
	 perhaps you send me a postcard to see where this software 
	 hs gone so far? I would like this very much:

	  >> Alexander Doser, Sichelstrasse 31, D-70372 Stuttgart <<

                                                 thanks,
                                                         lg



































COPYRIGHT (C) laughlin   MARCH 11, 1994,  ALL RIGHTS RESERVED



  DDLG - THE HP48 HIDDEN SURFACE GRAPHICS PACKAGE -- Page 2


0. CONTENTS OF DDLG - DOCUMENTATION

  1.     INITIAL INTRODUCTION (AND AN IMPORTANT NOTE) ..............   4
  2.     GENERAL OVERVIEW OF PROGRAMS USED IN THIS GRPHICS PACKAGE .   5
  2.1    SIMPLE FUNCTIONS ..........................................   5
  2.1.1  DIN - DDLG INITILIZE ......................................   5
  2.1.2  AVB - ADAPT VIRTUAL BOUNDARIES ............................   5
  2.1.3  TST - TEST DRAW ...........................................   5
  2.1.4  DLG - THE HIDDEN LINE SURFACE PLOTTER .....................   5
  2.1.5  ERASE - CLEAR THE GRAPHIC SCREEN ..........................   5
  2.1.6  INFO - INFORMATION ABOUT DDLG PARAMTERS IN USE ............   5
  2.1.7  SFB SET - FUNCTION BOUNDARIES .............................   5
  2.1.8  SRO - SET ROTATION ........................................   6
  2.1.9  SPA - SET PARALLEL PROJECTION .............................   6
  2.1.10 SPE - SET PERSPECTIVIC PROJECTION .........................   6
  2.1.11 SVI - SET VIRTUAL BOUNDARIES ..............................   6
  2.1.12 SDE - SET DEVICE BOUNDARIES ...............................   6
  2.2    MORE COMPLEX FUNCTIONS ....................................   6
  2.2.1  MINI - MAKE INITIAL MATRIX (NO OPERATION) .................   6
  2.2.2  MTRX - MULTIPLY ROTATION AROUND X-AXIS ....................   6
  2.2.3  MTRX - MULTIPLY ROTATION AROUND Y-AXIS ....................   6
  2.2.4  MTRX - MULTIPLY ROTATION AROUND Z-AXIS ....................   7
  2.2.5  MPAR - MULTIPLY PARALLEL PROJECTION .......................   7
  2.2.6  MPER - MULTIPLY PERSPECTIVIC PROJECTION ...................   7
  2.2.7  MTRA - MULTIPLY TRANSFOMATION .............................   7
  2.2.8  MSCA - MULTIPLY SCALEING ..................................   7
  2.2.9  SW>V - SET WORLD TO VIRTUAL TRANSFORMTION MATRIX ..........   7
  2.2.10 SW>D - SET WORLD TO DEVICE TRANSFORMTION MATRIX ...........   7
  2.2.11 MPRJ - MAKE PROJECTION PIPELINE ...........................   7
  2.2.12 M9S - MAKE 9 PLACES SIZED STRING ..........................   8
  2.3    SPECIAL ROUTINES ..........................................   8
  2.3.1  LDR - LINE DRAW ...........................................   8
  2.3.2  TDR - TEST DRAW ...........................................   8
  2.3.3  PDR - PIXELCOLLECTING DRAW ................................   8
  2.3.4  DOU - DEVICE OUTPUT .......................................   8
  2.3.5  LAK - LINE AKTUALIZATION ..................................   8
  2.3.6  TAK - TEST AKTUALIZATION ..................................   8
  2.4    OTHER FUNCTIONS ...........................................   9
  2.4.1  ->P4 - CONVERT TO HOMOGENOUS COORDINATES ..................   9
  2.4.2  ->P4 - CONVERT FROM HOMOGENOUS COORDINATES ................   9
  2.4.3  CLC - FULL PROJECTION CALCULATION (WORLD TO DEVICE) .......   9
  2.4.3  PRJ - PARTIOAL PROJECTION (WORLD TO VIRTUAL) ..............   9
  2.4.4  HIN - HORIZOON INITIALIZATION .............................   9
  2.4.5  F - SURFACE FUNCTION ......................................   9
  2.4.6  SSC - SCALE SCALEING ......................................   9
  2.4.7  PRSV - PRESERVE FLAGS .....................................   9
  2.5    FUNCTIONS AND VARIABLES USED WITHIN THIS PACKAGE ..........   9
  2.5.1  CST - CUSTOM MENU / PROGRAMS ORDERING LIST ................  10
  2.5.2  DEL - DELETABLE VARIABLES LIST / VARIABLES ORDERING LIST ..  10
  2.6    FLAGS USED WITHIN THIS PACKAGE ............................  10
  2.6.1  FLAG 1 - SQUARE DEVICE SURFACE ............................  10
  2.6.2  FLAG 2 - AUTOADAPT VIRTUAL TO DEVICE SCALING ..............  10
  2.7    FUTURE PLANS ..............................................  10






COPYRIGHT (C) laughlin   MARCH 11, 1994,  ALL RIGHTS RESERVED



  DDLG - THE HP48 HIDDEN SURFACE GRAPHICS PACKAGE -- Page 3


  3.     A CLOSER LOOK AT THE INSIDES OF THIS PACKAGE ..............  11
  3.1    BASIC INTRODUCTION TO THREEDIMENSIONAL GRAPHICS ...........  11
  3.1.1  ->P4 - CONVERT TO HOMOGENOUS COORDINATE ...................  11
  3.1.2  P4-> - CONVERT FROM HOMOGENOUS COORDINATE .................  11
  3.2    NO OPERATION (NOT A JOKE!) ................................  12
  3.3    ROTATION ..................................................  12
  3.4    TRANSLATION ...............................................  12
  3.5    SCALING ...................................................  12
  3.6    FUNCTIONS FOR NOOP, ROTATION, TRANSLATION AND SCALING .....  13
  3.6.1  MINI - GENERATE THE NOOP 4X4 IDENTITY MATRIX ..............  13
  3.6.2  MULTIPLY BY ROTATION AROUND X-AXIS ........................  13
  3.6.3  MULTIPLY BY ROTATION AROUND Y-AXIS ........................  13
  3.6.4  MULTIPLY BY ROTATION AROUND Z-AXIS ........................  13
  3.6.5  MULTIPLY BY TRANSLATION ALONG X-, Y- AND Z-AXIS ...........  13
  3.6.6  MULTIPLY BY SCALING ALONG X-, Y- AND Z-AXIS ...............  13
  3.7    PROJECTION ................................................  13
  3.7.1  PARALLEL PROJECTION .......................................  14
  3.7.1  PERSPECTIVIC PROJECTION ...................................  14
  3.8    PROJECTION FUNCTIONS ......................................  15
  3.8.1  MPAR - MULTIPLY PARALLEL PROJECTION .......................  15
  3.8.2  MPER - MULTIPLY PERSPECTIVIC PROJECTION ...................  15
  3.9    THE SETING FUNCTIONS ......................................  15
  3.9.1  SET FUNCTION BOUNDARIES ...................................  15
  3.9.2  SET ROTATION ANGLES .......................................  16
  3.9.3  SET PARALLEL PROJECTION ...................................  16
  3.9.4  SET PERSPECTIVIC PROJECTION ...............................  16
  3.9.5  SET VIRTUAL BOUNDARIES ....................................  16
  3.9.6  SET DEVICE BOUNDARIES .....................................  17
  3.10   GENERATE TRANSFORMATON PIPELINE USING MPRJ ................  17
  3.10.1 WORLD TO VIRTUAL MATRIX ...................................  17
  3.11   TWO DIMENSIONAL COORDINATES AND TRANSFORMATION FUNCTIONS ..  17
  3.11.1 CONVERT 2D TO HOMOGENOUS COORDINATES ......................  17
  3.11.2 CONVERT FROM HOMOGENOUS CFOORDINATES TO 2D ................  17
  4.     HIDDEN LINE REMOVAL .......................................  18
  4.1    HIN - HORIZOON INITIALIZATION .............................  18
  4.1.1  USING HIN TO INITIALIZE HORIZOONS .........................  18
  4.2    LDR, TDR AND PDR - DRAW PARTIALLY HIDDEN LINES ............  18
  4.2.1  USING LDR TO DRAW (HIDDEN) LINES ..........................  19
  4.3    LAK AND TAK - AKTUALIZE LOWER AND UPPER HORIZOON ..........  19
  4.3.1  USING LAK TO AKTUALIZE HORIZOONS ACCORDING LINE ...........  19
  4.4    TDR AND TAK AND THE DEVICE'S Y-BOUNDARIES .................  19
  5.     FINAL NOTES ...............................................  20
 (6.)    DDLG - THE GRAPHIC PACKAGE (HP48 BINARY, UUENCODED) .......  END 
















COPYRIGHT (C) laughlin   MARCH 11, 1994,  ALL RIGHTS RESERVED



  DDLG - THE HP48 HIDDEN SURFACE GRAPHICS PACKAGE -- Page 4


1. INITIAL INTRODUCTION (AND AN IMPORTANT NOTE)

This package consists of several fundamental functions which together form
the basis to perform two and three dimensional transformations such as
rotation, projection, scaleing, translation etc.

I hope that this package is easy to use but nevertheless expandable to fit
your own very special needs as desired...

Among the basic functions which can be done are (in 2d and 3d space):

 - rotation, translation, scaleing
 - parallel/perspectivic projection

Further functions are:

 - simple three dimensional surface plot (TST)
 - three dimensional surface plot with hidden line removal (DLG)


IMPORTANT NOTE:

 The hidden line algorithm used within this software works only if we draw
 the surface from front to back!  Upto now you have to make sure that
 yourself by exchanging the function boundaries if necessary. This will be
 automatically done in a future release...

 You can avoid this simply by using rotations that will be drawn from front
 to back. One of those rotations is the one used by DIN - Draw INitialize
 function. You may vary those angles by small amounts (say +/- 30 degrees)
 without problems. But you can test other rotations anyway. In some cases
 however the drawing will look a little strange caused by wrong lines
 assumed to be hidden. I'll keep my nose on that...


























COPYRIGHT (C) laughlin   MARCH 11, 1994,  ALL RIGHTS RESERVED



  DDLG - THE HP48 HIDDEN SURFACE GRAPHICS PACKAGE -- Page 5


2. GENERAL OVERVIEW OF PROGRAMS USED IN THIS GRPHICS PACKAGE
   ---------------------------------------------------------

  I'll begin with the more simple and more userfriendly functions and will
continue with the more complex functions which you can use to obtain
special effects and more control to produce your own ideas. In most times
it's sufficient to use the normal, easy to use functions though.


2.1 SIMPLE FUNCTIONS

These are the simpler functions in order as dislayed by the CST-Menu:

2.1.1 DIN - DDLG INITILIZE

  DIN - Ddlg INitialize: Initialize the graphics package to standard values.
        Flag 4 is set to indicate pre-phase of initaialisation. Otherwise
        some routines would error out because of missing variables. At the
        end of this routine however Flag 4 is cleared to indicate normal
        operation. NOTE: THIS ROUTINE MUST BE CALLED EVERY TIME TO INITALIZE
        THE GRAPHICS PACKAGE AFTER THE UNNEEDED VARIBLES ARE DELETED!
        (see also DEL - DELetable variables list below).

2.1.2 AVB - ADAPT VIRTUAL BOUNDARIES

  AVB - Adapt Virtual Boundaries: The full screen of the HP48 is used to
        display the graphic. Otherwise only part of the graphic might be
        displayed or the graphic will be very small. Thus this is in fact
        some kind of autoscale! Uses SVI, so it's influenced by Flag 2
        (see SVI - Set VIrtual boundaries below).

2.1.3 TST - TEST DRAW

  TST - TeST draw. This is a simple 3D surface plotter without hidden line
        removal. Seems this is reasonably fast.

2.1.4 DLG - THE HIDDEN LINE SURFACE PLOTTER

  DLG - Draw LG's surface plot. This is the famous but actually VERY SLOW 3d
        surface plotter with HIDDEN LINE REMOVAL! As said it's very, very slow.
        So you should have some time to generate such a plot...

2.1.5 ERASE - CLEAR THE GRAPHIC SCREEN

  ERASE - The usual PICT-ERASE command of the HP48. Placed here for
          convinience only.

2.1.6 INFO - INFORMATION ABOUT DDLG PARAMTERS IN USE

  INFO - INFOrmation. Displays all relevant parameters used in the standard
         transformation pipeline.

2.1.7 SFB - SET FUNCTION BOUNDARIES

  SFB - Set Function Boundaries. MinX, MaxX, DeltaX, MinY, MaxY and DeltaY
        are stored in the aproprate Variables (XI, XA, XD, YI, YA, YD).



COPYRIGHT (C) laughlin   MARCH 11, 1994,  ALL RIGHTS RESERVED



  DDLG - THE HP48 HIDDEN SURFACE GRAPHICS PACKAGE -- Page 6


2.1.8 SRO - SET ROTATION

  SRO - Set ROtation (3D). Stores the rotation angles (x-, y- and z-axis).

2.1.9 SPA - SET PARALLEL PROJECTION

  SPA - Set PArallel projection. Indicated by cleared flag 3.

2.1.10 SPE - SET PERSPECTIVIC PROJECTION

  SPE - Set PErspectivic projection. Indicated by set flag 3.

2.1.11 SVI - SET VIRTUAL BOUNDARIES

  SVI - Set VIrtual boundaries. MinVx, MaxVx, DeltaVx, MinVy, MaxVy and
        DeltaVy are stored in the apropriate Variables (VXI, VXA, DVX,
        VYI, VYA, DVY). If Flag 2 is set the boundaries are adapted to
        obtain the same aspect ration as the output device has. This is done
        by enlarging the dimension which is too small with respect to
        the aspect ration of the output device. (In fact this is done by
        a subroutine called SSC - Scale SCaleing).

2.1.12 SDE - SET DEVICE BOUNDARIES

  SDE - Set DEvice coordinates. MinDx, MaxDx, DeltaDx, MinDy, MaxDy and
        DeltaDy are stored in the apropriate Variables (DXI, DXA, DDX,
        DYI, DYA, DDY). If Flag 1 is set the boundaries are adapted to
        obtain an aspect ratio of 1, that is both dimensions of the output
        device have the same range. (So DXI=DYI, DXA=DYA, DDX=DDY states true,
        by reduceing the larger range to fit the smaller).


2.2 MORE COMPLEX FUNCTIONS

The following functions can be used to achive more complex operations but
are less easy to use. These are used by the 'simple functions' above. They
can be ignored if you don't want to do your own special operations on the
graphic.

2.2.1 MINI - MAKE INITIAL MATRIX (NO OPERATION)

  MINI - Simply creates a 4x4 identity matrix. This matrix can be multiplied
         with the results of the 3D graphics operations obtained by the
         following functions. (Also serves as NO OP)

2.2.2 MTRX - MULTIPLY ROTATION AROUND X-AXIS

  MRTX - Multiply with RoTation around X-axis. (x-axis from left to right
         in 3d!). If your thumb points in that direction the other fingers
         will show the rotation direction.  (We have a right-system: thumb
         in x-direction, index finger in y-direction, thus middle finger
         shows z-direction, see FIGURE - 1 below).

2.2.3 MTRX - MULTIPLY ROTATION AROUND Y-AXIS

  MRTY - Same as MTRX around Y-axis. (y-axis from bottom to top in 3d!).



COPYRIGHT (C) laughlin   MARCH 11, 1994,  ALL RIGHTS RESERVED



  DDLG - THE HP48 HIDDEN SURFACE GRAPHICS PACKAGE -- Page 7


2.2.4 MTRX - MULTIPLY ROTATION AROUND Z-AXIS

  MRTZ - Same as MTRX around Z-axis. (z-axis from back to front in 3d!).

2.2.5 MPAR - MULTIPLY PARALLEL PROJECTION

  MPAR - Multiply with PARallel projection. Called by MPRJ if Flag 3 cleared.

2.2.6 MPER - MULTIPLY PERSPECTIVIC PROJECTION

  MPER - Multiply with PERspectivic projection. Called by MPRJ if Flag 3 set.

2.2.7 MTRA - MULTIPLY TRANSFOMATION

  MTRA - Multiply with TRAnslation. Used to shift the graphic in 3D or 2D
         space. Expects the 4x4 matrix and tx, ty and tz, the shifts in
         x- y- and z-direction.

2.2.8 MSCA - MULTIPLY SCALEING

  MSCA - Multiply with SCAleing. Used to enlarge the graphic in 3D or 2D
         space. Expects the 4x4 matrix and sx, sy and sz, the factors to
         enlarge the graphic along x-, y-, and z-direction.

2.2.9 SW>V - SET WORLD TO VIRTUAL TRANSFORMTION MATRIX

  SW>V - Set World to Virtual translation pipeline. (The matrix obtaind by
         the above functions is somtimes called a transformation pipeline,
         because the operations done by those matrizes are done as if we push
         the origial 3D object into a pipeline and get a transformed object
         as a result). This matrix sould be used to get the transformation
         from 3D space into 2D virtual coordinates. (Both of them are real
         valued).

2.2.10 SW>D - SET WORLD TO DEVICE TRANSFORMTION MATRIX

  SW>D - Set World to Device translation pipeline. This one should be used
         to get the final transformation from 3D coordinates into the final
         2D device coordinates. The 3D space is real valued the 2D space is
         integer valued. (This might be interesting in optimization of the
         DLG - Draw LG's surface plot and it's subroutines.)

2.2.11 MPRJ - MAKE PROJECTION PIPELINE

  MPRJ - Make PRoJection standard pipeline. Uses the M...-Functions above to
         produce both transformation matrices. The standard pipeline is:

         - rotate around x-, y- and z-axis. (this order)
         - do parallel (Flag 3 cleared) or perspectivic (Flag 3 set) projection.

         (the matrix obtained up to here is set with SW>V because these are
          the operations to get virtual coordinates)

         - translate (MinVx, MinVy) to origin.
         - Scale so graphic lays within device's range.
         - translate (MinX, MinY) so graphic lays within device boundaries.



COPYRIGHT (C) laughlin   MARCH 11, 1994,  ALL RIGHTS RESERVED



  DDLG - THE HP48 HIDDEN SURFACE GRAPHICS PACKAGE -- Page 8


           (This is the final transformation matrix. The coordinates obtaind
            by this matrix might be plotted on the output device. The matrix
            obtained by all the above operations thus produce the matrix set 
            by SW>D).

2.2.12 M9S - MAKE 9 PLACES SIZED STRING

  M9S - Make at least 9 places sized string out of object. Reals are rounded
        to 3 decimal places after radix. A leading sign is added always and
        spaces are in front to fill the 9 places. Thus it's possible to get
        " +xxxx.xxx", where + is the sign and xxxx.xxx is the value of the
        real. Note the space in front to seperate several reals appended by
        simply concatenate them together using '+'.


2.3 SPECIAL ROUTINES

  The following routines might be of little use if used seperate from
  DLG - Draw LG's surface plot. These are the time consuming routines which I
  hope to optimize in the future. I think it would be best if they will be
  rewritten in ML (someone else interested?). They all operate with integer
  values, so it might be possible to speed them up a lot...

2.3.1 LDR - LINE DRAW

  LDR - Line DRaw. Draws a line if that line isn't hidden. (see more below!)

2.3.2 TDR - TEST DRAW

  TDR - Test DRaw. Test if the line shold be drawn or not. Used by
        LDR - Line Draw.

2.3.3 PDR - PIXELCOLLECTING DRAW

  PDR - Pixelcollecting DRaw. Everytime visibility switches from hidden to
        visible starts to collect those pixel. Everytime visibility changes
        from visible to hidden the collected distance is plotted as a whole
        because the machine's line draw command is mostly faster than plotting
        single pixels step by step. Used by TDR - Test DRaw.

2.3.4 DOU - DEVICE OUTPUT

  DOU - Device OUtput. This is the last step in the line-drawing process.
        Simply draws the line from (x1,y1) to (x2,y2).

2.3.5 LAK - LINE AKTUALIZATION

  LAK - Line AKtualization. After the line is drawn the horizoons have to be
        updated by this routine. (see below!).

2.3.6 TAK - TEST AKTUALIZATION

  TAK - Test AKtualization. Only Points inside the device bondaries are
        interesting. Those are used to update the horizoons. Used by
        LAK - Line AKtialization.




COPYRIGHT (C) laughlin   MARCH 11, 1994,  ALL RIGHTS RESERVED



  DDLG - THE HP48 HIDDEN SURFACE GRAPHICS PACKAGE -- Page 9


2.4 OTHER FUNCTIONS

2.4.1 ->P4 - CONVERT TO HOMOGENOUS COORDINATES

  ->P4 - convert to 4-dimensional homogenous coordinates. (see below).
         converts 3 reals representing x-, y- and z-coordinate to
         4-dimensional homogenous coordinate vector.

2.4.2 ->P4 - CONVERT FROM HOMOGENOUS COORDINATES

  P4-> - inverse of above. converts 4-dimensional homogenous coordinate
         vector to 3 reals representing x-, y- and z-coordinate.

2.4.3 PRJ - PARTIOAL PROJECTION (WORLD TO VIRTUAL)

  PRJ - partial PRoJection. Does World to Virtual coordinate projection.
        Virtual coordinates are real valued.

2.4.3 CLC - FULL PROJECTION CALCULATION (WORLD TO DEVICE)

  CLC - full projection CaLCulation. Does World to Device coordinate
        transformation. Device coordinates are integer valued.

2.4.4 HIN - HORIZOON INITIALIZATION

  HIN - Horizoons INitalization. Initializes lower and upper horizoon to the
        initial values. Later every point laying between lower and upper
        horizoon is considered invisible. (see chapter 3).

2.4.5 F - SURFACE FUNCTION

  F - the actual 3D surface F(x,y). Thus has the form << -> X Y FFF >>.
      FFF can be an algebraic such as 'SIN(X)*COS(Y)' or a program such as
      IF X Y * 0 > THEN X Y * ELSE X Y * - END ...

2.4.6 SSC - SCALE SCALEING

  SSC - Scale SCaling. Used by SDE - Set Device boundaries and
        SVI - Set VIrtual boundaries. If Flag 2 set adapts virtual boundaries
        so that virtual aspect ratio is the same as device aspect ratio. Thus
        we yield a non-distorted, natural scaled graphic.

2.4.7 PRSV - PRESERVE FLAGS

  PRSV - PReSerV Flags. Well known program just in case you haven't it. It's
         used such as << << 1 SF 3 FIX >> PRSV >>. After program execution
         the Flags have the same state as before program execution.


2.5 FUNCTIONS AND VARIABLES USED WITHIN THIS PACKAGE

2.5.1 CST - CUSTOM MENU / PROGRAMS ORDERING LIST

  CST - CuSTom menu. All programs are displayed in the custom menu. This list
        was used to order the programs (The ERASE entry has been deleted,
        then ORDER was used to sort the programs as desired).



COPYRIGHT (C) laughlin   MARCH 11, 1994,  ALL RIGHTS RESERVED



 DDLG - THE HP48 HIDDEN SURFACE GRAPHICS PACKAGE -- Page 10


2.5.2 DEL - DELETABLE VARIABLES LIST / VARIABLES ORDERING LIST

  DEL - DELetable variables. All variables contained in this list can be
        deleted. They are installed (with standard values) by using DIN -
        Drawing INitialize. (Use DEL PURGE to delete them to save space).


2.6 FLAGS USED WITHIN THIS PACKAGE

Finally here are the Flags and their meanings:

            FLAG        SET                      CLEAR
  USER:      1     adapt ddx=ddy                   -
             2     adapt virtual aspect            -

  INTERNAL:  3     perspectivic                 parallel
             4     pre-initialization           normal
             5     SSC called by SVI            SSC callded by SDE

NOTE: Only Flags 1 and 2 should be set by user. The others are used internally.

2.6.1 FLAG 1 - SQUARE DEVICE SURFACE

      Flag 1 set leads to an adaption of the device's x- or y-range. So we
      obtain a square output (centered at middle of x-range, left and right
      place is not used in this case (pixels from 1 to 33 and 95 to 131)
      and therefore are 'lost').

2.6.2 FLAG 2 - AUTOADAPT VIRTUAL TO DEVICE SCALING

      Flag 2 set leads to an adaption of the virtual's x- or y-range. This
      provides the ability to get naturally scaled, non-distorted graphic.
      If this option is not used always the full screen of the device (or
      the full square screen -> Flag 1 set) is used. Thus the graphic might
      be destorted e.g. if the graphic is very 'thin' in virtual
      x-direction and very thick in virtual y-direction the x-direction
      would be enlarged to fill the whole screen's x-range. Setting Flag 2
      prevents this unwanted magnification and thus prevents distortion in
      x-direction.


2.7 FUTURE PLANS

These are all programs in this graphics package so far. Others may follow
if I've got the time to work them out:

- First these ones need to be optimized... (especially the LDR/LAK family)

- The drawing direction should be corrected automatically. Up to now you
  have to make sure yourself that the drawing direction is from front to
  back. Otherwise the hidden line algorithm wouldn't work correctly. You
  may obtain very strange results if you wouldn't note this!

- Perhaps I'll add commands to display objects other than surfaces. That
  would make it possible to generate real 3D objects (described with lists
  of points, lines...) and generate 3D scenes and worlds. [ Ooooh.... :-o ]



COPYRIGHT (C) laughlin   MARCH 11, 1994,  ALL RIGHTS RESERVED



 DDLG - THE HP48 HIDDEN SURFACE GRAPHICS PACKAGE -- Page 11


3. A CLOSER LOOK AT THE INSIDES OF THIS PACKAGE
   --------------------------------------------

3.1 BASIC INTRODUCTION TO THREEDIMENSIONAL GRAPHICS

The following is a very short introduction into three dimensional graphics:
(for further information about this see the appropriate literature about
 graphics. Remember this is a basic introduction only due to limited space
 and a time!)

To describe a point in three dimensional space we have to give the three
coordinates which describe where the point lays. The coordinate system used
throughout this software can be seen in FIGURE - 1. The Term P(X,Y,Z) means
the Point P has the coordinates X (in x-direction) Y (in y-direction) and
Z (in z-direction).

                    y-axis   (index finger)
                    |
                    Y----------+
                    |      P / |
                    |      +   |
                    |      |   |         P(X,Y,Z)
                    |      |   |
                    o------|---X---- x-axis  (thumb)
                  /        | /
                Z----------+
              /
            z-axis   (middle finger)

                           FIGURE - 1

  To make rotation and other operations possible it's usefull to introduce a
fourth dimension which in most cases can be ignored. (We will later see how
these operations can be achieved). This forth dimension is normaly set to 1.
This four dimensional coordinate is called homogenous coordinate. Thus the
term P(X,Y,Z,1) describes the same point as P(X,Y,Z) in homogenous coordinates.

  The following two programs perform the necessary steps to convert
between three reals (3d coordinates) to the corresponding homogenous vector:

3.1.1 ->P4 - CONVERT TO HOMOGENOUS COORDINATE

  ->P4:          3:x
                 2:y
                 1:z            ->      1: [x y z 1]

3.1.2 P4-> - CONVERT FROM HOMOGENOUS COORDINATE

  P4->:                                 3:x
                                        2:y
                 1:[x y z 1]    ->      1:z

  (->P4 adds the 1 as forth dimension value and forms a 4-dimensional vector.
   P4-> divides by the value of the forth dimension, so if we havn't changed
   the forth value it's 1 which leaves the other values unchanged.)

Now we are able to perform several operations in 3d space:


COPYRIGHT (C) laughlin   MARCH 11, 1994,  ALL RIGHTS RESERVED



 DDLG - THE HP48 HIDDEN SURFACE GRAPHICS PACKAGE -- Page 12


3.2 NO OPERATION (NOT A JOKE!)

Just to complete the matrices. The following is a NO OP Function. If we
multiply the homogenous coordinates with this matrix the get the same
homogenous coordinates. So we use this one to start up the 'transformation
pipeline'. (By multiplying all operations 'into' this matrix we can operate
at a point by simply multiplying the homogenous coordinates with the
resulting matrix. All operations are performed within this multiplication!)

  [[ 1 0 0 0 ]
   [ 0 1 0 0 ]          This is a NO OP matrix.
   [ 0 0 1 0 ]
   [ 0 0 0 1 ]]


3.3 ROTATION

We can rotate around the three axes, x, y and z. To rotate around the x-axis
we have to multiply the homogenous coordinate with the following matrix:

  [[ 1     0       0    0 ]
   [ 0  COS(WX) SIN(WX) 0 ]
   [ 0 -SIN(WX) COS(WX) 0 ]         WX is the angle we want the graphic to
   [ 0     0       0    1 ]]        rotate around the x-axis.

Similarly we can rotate around the y- and z-axis with similar matrizes:

  [[  COS(WY) 0 SIN(WY) 0 ]
   [     0    1    0    0 ]         WX is the angle we want the graphic to
   [ -SIN(WY) 0 COS(WY) 0 ]         rotate around the y-axis.
   [     0    0    0    1 ]]

  [[  COS(WZ) SIN(WZ) 0 0 ]
   [ -SIN(WZ) COS(WZ) 0 0 ]         WZ is the angle we want the graphic to
   [     0       0    1 0 ]         rotate around the z-axis.
   [     0       0    0 1 ]]


3.4 TRANSLATION

We can translate a point in any direction by adding/subtracting from x, y
and z coordinates. To obtain this we have to multiply the homogenous
coordinates with the following matrix:

  [[  1  0  0 0 ]
   [  0  1  0 0 ]     tx, ty and tz are the values we want the point to
   [  0  0  1 0 ]     be shifted in x-, y- and z-direction.
   [ tx ty tz 1 ]]

3.5 SCALING

And we are able to scale along the x-, y- and z-axis.

  [[ sx  0  0 0 ]    sx, sy and sz are the factors we want the point
   [  0 sy  0 0 ]    to be scaled (streched away from/to origin) in
   [  0  0 sz 0 ]    x-, y- and z-direction
   [  0  0  0 1 ]]


COPYRIGHT (C) laughlin   MARCH 11, 1994,  ALL RIGHTS RESERVED



 DDLG - THE HP48 HIDDEN SURFACE GRAPHICS PACKAGE -- Page 13


3.6 FUNCTIONS FOR NO OP, ROTATION, TRANSLATION AND SCALING

All that's done by the following functions which need a 4x4-matrix to which
the operation is 'appended' by multiplication.

3.6.1 MINI - GENERATE THE NOOP 4X4 IDENTITY MATRIX

  MINI:                    ->   1:[[...]]   (the no op matrix to start with)

3.6.2 MULTIPLY BY ROTATION AROUND X-AXIS

  MRTX:   2:[[...]]
          1:x-angle        ->   1:[[...]]

3.6.3 MULTIPLY BY ROTATION AROUND Y-AXIS

  MRTY:   2:[[...]]
          1:y-angle        ->   1:[[...]]

3.6.4 MULTIPLY BY ROTATION AROUND Z-AXIS

  MRTZ:   2:[[...]]
          1:z-angle        ->   1:[[...]]

3.6.5 MULTIPLY BY TRANSLATION ALONG X-, Y- AND Z-AXIS

  MTRA:   4:[[...]]
          3:tx
          2:ty
          1:tz             ->   1:[[...]]

3.6.6 MULTIPLY BY SCALING ALONG X-, Y- AND Z-AXIS

  MSCA:   4:[[...]]
          3:sx
          2:sy
          1:sz             ->   1:[[...]]


Now that we can rotate, translate and scale 3d objects we will transform
the 3d coordinates to 2d coordinates which we can display on the HP48.


3.7 PROJECTION

The two major transformations are parallel and perspectivic transformations:













COPYRIGHT (C) laughlin   MARCH 11, 1994,  ALL RIGHTS RESERVED



 DDLG - THE HP48 HIDDEN SURFACE GRAPHICS PACKAGE -- Page 14


3.7.1 PARALLEL PROJECTION

Parallel projection is obtained by translation along a given vector
until the x-y plane is reached. These are the searched coordinates. See
FIGURE - 2.

                  y |                         ->
                    |   ->  /                 v = projection vector
                    *-- v / ------* P'        P = 3D point  P' = 2D point
                    |   /       / |           Q = 3D point  Q' = 2D point
                    |       P *   |
                    *---------|---+--------* Q'
                    |         |   |      / |
                    |         |   |  Q *   |
                    |         |   |    |   |
                    o---------|---*----|---*------------
                  /           | /      | /             x
              z /             *        *

                               FIGURE - 2

If the x- or y-value of the vector isn't zero we get a crooked projection
which is destorted. (the higher the value the more distorted). So normally
we only use the z-direction. (The length of the vector doesn't matter. it's
only the direction which is important!)

The matrix used to achieve this operation looks like:

  [[      1      0    0   0 ]
   [      0      1    0   0 ]     xp, yp and zp are the components of
   [ -xp/zp -yp/zp    1   0 ]     the vector of the parallel projection
   [      0      0    0   1 ]]


3.7.1 PERSPECTIVIC PROJECTION

Perspectivic transformation is obtained by extending the vector from the
projection center (C) (your eye) to that point until it reaches the x-y
plane. See also FIGURE - 3.

                  y |             * P'     C = Projection center
                    |           / |        P = 3D point  P' = 2D point
                    |         * P |        Q = 3D point  Q' = 2D point
                    |       / |   |
                    |     /   |   |
                    | C /     |   |    Q          Q'
                    | * ---------------*----------*
                    | |       |   |    |          |
                    o-|-------|---*----|----------*-----
                  /   |     / | /      | /             x
                /     |   /   *        *
           z  /       | /
            *---------*

(Hmmm, hard to display with ASCII!)

                               FIGURE - 3


COPYRIGHT (C) laughlin   MARCH 11, 1994,  ALL RIGHTS RESERVED



 DDLG - THE HP48 HIDDEN SURFACE GRAPHICS PACKAGE -- Page 15


If the projection center lays within the graphics boundaries the projection
produces at least a strange result. In some cases we will obtain a
division-by-zero error. This is caused if the point is near the projection
center.  The nearer they are the greater the resulting projection. Distance
zero leads to infinite values for the projection, something too big to be
handled by such a small calculator ;-)

The matrix used to for this operation looks like:

  [[      1      0    0       0 ]
   [      0      1    0       0 ]     xp, yp and zp are the components of
   [ -xp/zp -yp/zp    1   -1/zp ]     the location the projection center
   [      0      0    0       1 ]]


3.8 PROJECTION FUNCTIONS

The following functions can be used to achive both projection types:

3.8.1 MPAR - MULTIPLY PARALLEL PROJECTION

  MPAR:   4:[[...]]
          3:PXP
          2:PYP
          1:PZP             ->       1:[[...]]                    ->
  (remember: PXP,PYP and PZP describe the direction of the vector v).


3.8.2 MPER - MULTIPLY PERSPECTIVIC PROJECTION

  MPER:   4:[[...]]
          3:PXC
          2:PYC
          1:PZC             ->       1:[[...]]
  (remember: PXC,PYC and PZC describe the location of the observer's eye).


3.9 THE SETING FUNCTIONS

We can initalize the necessary variables by executing The S... functions
Then we can use the MPRJ function to generate the standard transformations.


3.9.1 SET FUNCTION BOUNDARIES

The x-value runs from XI to XA with steps of XD.  Similarly the y-value
runs from YI to YA in steps of YD.

  SFB:    6:XI
          5:XA
          4:XD
          3:YI
          2:YA
          1:YD             ->





COPYRIGHT (C) laughlin   MARCH 11, 1994,  ALL RIGHTS RESERVED



 DDLG - THE HP48 HIDDEN SURFACE GRAPHICS PACKAGE -- Page 16


3.9.2 SET ROTATION ANGLES

The x-, y- and z-angle can be set by the following function.

(NOTE: the order is fix: x- then y- then z-rotation. Could be changed in
 MPRJ function though):

  SRO:    3:x-angle
          2:y-angle
          1:z-angle        ->


3.9.3 SET PARALLEL PROJECTION

The x-, y- and z-value of the vector along which the transformation takes
place must be given. (Flag 3 is cleared to indicate parallel projection):

  SPA:    3:x-value
          2:y-value
          1:z-value        ->  (Flag 3 cleared)


3.9.4 SET PERSPECTIVIC PROJECTION

The x-, y- and z-value of the projection center must be given. (Flag 3 is
set to indicate perspectivic projection):

  SPE:    3:x-center
          2:y-center
          1:z-center       ->    (Flag 3 set)

(NOTE: DO NOT SET THE PROJECTION CENTER TOO NEAR TO THE GRAPHICS OBJECT.
       A DIVISION BY ZERO MAY RESULT. For example if the function lays
       within the box defined by P(-2,-2,-2) and Q(2,2,2) setting the
       projection center at P(-2,-2,-2) may cause a DIVISION BY ZERO ERROR
       or at least generates very large real-values which will disturb the
       graphic unexpectedly. Instead go away from the boundaries of the
       box, for example set the projection center at S(-4,-4,-4)!)


3.9.5 SET VIRTUAL BOUNDARIES

Set the boundaries to the desired values. If Flag 2 is set the values are
adapted to fit the aspect ratio of the output device's aspect ratio. This
leads to correctly scaled, thus non disturbed projections:

  SVI:    4:VXI
          3:VYI
          2:VXA                (DVX and DVY set accordingly
          1:VYA            ->   VXD and VYD updated also)









COPYRIGHT (C) laughlin   MARCH 11, 1994,  ALL RIGHTS RESERVED



 DDLG - THE HP48 HIDDEN SURFACE GRAPHICS PACKAGE -- Page 17


3.9.6 SET DEVICE BOUNDARIES

Set the boundaries of the output device. If Flag 1 is set the values are
adapted to give a sqare output surface. (The space on the left and right
side is left unused in this case).

  SDE:    4:DXI
          3:DYI
          2:DXA                (DDX and DDY set accordingly
          1:DYA            ->   VXD and VYD updated also)

(VXD and VYD are the scaling factors which adapt the virtual coordinates to
 device coordinates).


3.10 GENERATE TRANSFORMATON PIPELINE USING MPRJ

Finally MPRJ generates the two matrizes for the standard transformation after
the neceessary variables are set by the S... set  functions:

3.10.1 WORLD TO VIRTUAL MATRIX

W>V: World to Virtual rotation around x-, y- and z-axis (in this order) and
     projection (either parallel or perspectivic). We obtain virtual
     (real valued) coordinates.

3.10.1 WORLD TO DEVICE MATRIX

W>D: World to Device. Same as W>V plus scaling and translation to fit the
     device's x- and y-range. We get device (integer valued) coordinates.


3.11 TWO DIMENSIONAL COORDINATES AND TRANSFORMATION FUNCTIONS

Of course all those functions can be used with two dimensional values.
Simply set the z-value to zero and DROP it after the transformations. With
2D-coordinates we normally only use MRTZ. MRTX and MRTY would rotate the
x-y plane around the x- and y-axis. In this case we get a non-zero z-value
(which can be ignored however since it's the amount the new point was
shifted out of the x-y plane by the operation). Similarly we normally use
MTRA with tz set to zero and MSCA with sz set to one.

3.11.1  CONVERT 2D TO HOMOGENOUS COORDINATES

  ->P4:  3:x
         2:y
         1:0             ->   [[x y 0 1]]  (add dummy 0 as z-value)

3.11.2 CONVERT FROM HOMOGENOUS CFOORDINATES TO 2D

  P4->:                       1:x'
                              2:y'
         1:[[x y z w]]   ->   3:z'         (simply ignore z'-value)

  DROP:  3:x'
         2:y'                 2:x'
         1:z'            ->   1:y'


COPYRIGHT (C) laughlin   MARCH 11, 1994,  ALL RIGHTS RESERVED



 DDLG - THE HP48 HIDDEN SURFACE GRAPHICS PACKAGE -- Page 18


4. HIDDEN LINE REMOVAL

This package uses two horizoons to detect hidden lines. This is done as
follows:


4.1 HIN - HORIZOON INITIALIZATION

First the two horizoons, upper and lower, will be set to the opposite
values, that is the lower horizoon is set to the DYA value (MaxY-value of
device), the upper horizoon is set to the DYI value (MinY-value of device).
(see FIGURE - 4)

4.1.1 USING HIN TO INITIALIZE HORIZOONS

  HIN:     ->   (horizoons are initialized)


4.2 LDR, TDR AND PDR - DRAW PARTIALLY HIDDEN LINES

If the first line is drawn the upper and lower horizoon will be aktualized
to the line drawn as we can see in FIGURE - 4.

  |llllllllllllllllllll               |llllllll       lllll
  |                                   |
  |                                   |
  |                                   |           uuuu
  |                                   |       uuuu----
  |                                   |       ----llll
  |                                   |       llll
  |uuuuuuuuuuuuuuuuuuuu               |uuuuuuu        uuuuu
  o--------------------               o--------------------
    before line drawn                    after line drawn
     (initial state)

  lll = lower horizoon   uuu = upper horizoon

                                  FIGURE - 4

After three other lines, which together will form a rectangle we will get
somthing like FIGURE - 5 shows.

  |llll           lllll               |lllllll        lllll
  |                                   |        uuuuuuu
  |                                   |        -------
  |           uuuu                    |       u|     |
  |       uuuu----                    |       |   ----
  |       ----llll                    |       ----llll
  |       llll                        |       llll
  |uuuuuuu        uuuuu               |uuuuuuu        uuuuu
  o--------------------               o--------------------
    after line drawn                    after four line drawn

  lll = lower horizoon   uuu = upper horizoon

                                  FIGURE - 5



COPYRIGHT (C) laughlin   MARCH 11, 1994,  ALL RIGHTS RESERVED



 DDLG - THE HP48 HIDDEN SURFACE GRAPHICS PACKAGE -- Page 19


Now lets assume we are drawing FROM FRONT TO BACK! We have to draw a line
which will cross the region inside this rectangle. Thus this line is
(partially) hidden. We can find the part of that line hidden behind the
rectangle by testing with the horizoons:

If the point of the line is BETWEEN both horizoons, that is the
y-coordinate of that point is HIGHER THAN THE LOWER HORIZOON AND is LOWER
THAN THE UPPER HORIZOON at the x-coordinate that point is INVISIBLE.

Otherwise if the point of the line is on the OUTERSIDE of both horizoons,
that is the y-value is HIGHER THAN THE UPPER HORIZOON OR is LOWER THAN THE
LOWER HORIZOON at the x-coordinate that point is VISIBLE.

That's what LDR, TDR and PDR do. LDR generates the line point for point.
With each point TDR is called which tests if that point is visible. TDR
itself calls PDR. If the visibility changes from hidden to visible PDR
remains that point as first point. If the visibility changes back to
invisible that point is the last point. So a line between those two point
can be drawn by DOU (which is faster than drawing each point by itself).
If the line is partialy hidden the next part which is visible is drawn in
the same manner.

4.2.1 USING LDR TO DRAW (HIDDEN) LINES

 LDR:    4:X1
         3:Y1
         2:X2
         1:Y2        ->   (line is (partially) drawn)

4.3 LAK AND TAK - AKTUALIZE LOWER AND UPPER HORIZOON

LAK and TAK together update the horizoons LAK generates the line point by
point as LDR does but calls TAK instead, which updates both horizoons if
necessary.

4.3.1 USING LAK TO AKTUALIZE HORIZOONS ACCORDING LINE

 LDR:    4:X1
         3:Y1
         2:X2
         1:Y2        ->   (horizoons are aktualized according to line)


4.4 TDR AND TAK AND THE DEVICE'S BOUNDARIES

TDR and TAK both test if the actual point lays inside the x- and y-range of
the output device. Otherwise this point is ignored as it a wouldn't be
drawn (it would lay outside of the screen anyway!).











COPYRIGHT (C) laughlin   MARCH 11, 1994,  ALL RIGHTS RESERVED



 DDLG - THE HP48 HIDDEN SURFACE GRAPHICS PACKAGE -- Page 20


5. FINAL NOTES

This software is considered bug free. BUT ANYWAY:

  This software is provided AS IS without ANY warrenty! I won't be reliable
  for ANY problems caused by it or other damage resulting out of it's use.

  ALL FUNCTIONS ARE COPYRIGHT BY LAUGHLIN (C)           MARCH/1994 laughlin

Despite that I wish you much fun using this software though...
                                                                         lg
















































COPYRIGHT (C) laughlin   MARCH 11, 1994,  ALL RIGHTS RESERVED
