home *** CD-ROM | disk | FTP | other *** search
/ QBasic & Borland Pascal & C / Delphi5.iso / Basic / Q_BASIC.450 / TORUS.BAS (.txt) < prev    next >
Encoding:
QuickBASIC Tokenized Source  |  1989-08-05  |  27.0 KB  |  388 lines

  1. @    GetConfig
  2. SetPalette
  3. TorusDefine
  4. @    TorusCalc
  5. TorusColor
  6. @    TorusSort
  7. HighE
  8. @    TorusDraw
  9. Index9
  10. TileDraw
  11. TorusRotate
  12. First+
  13. Delay"
  14. Seconds
  15. CountTiles9
  16. Message`
  17. @    SetConfig
  18. Inside$
  19. DegToRad)
  20. Degrees
  21. Rotated
  22. Lower
  23. Upper
  24. Current
  25. FALSEK
  26. TROWi
  27. START3
  28. CONTINUE
  29. EGA256
  30. EGA64]
  31. HERCu
  32. TColor
  33. ConfigP
  34. Colors
  35. Atribs6
  36. XPix{
  37. TORUS
  38. Panel
  39. SectC
  40. Thick
  41. XDegree
  42. YDegree
  43. Bordj
  44. InitRowsN
  45. BestMode
  46.     Available
  47.     TOR.Thick
  48.     TOR.Panel
  49. TOR.XDegreek
  50. VC.Scrn
  51. MemErrY
  52. VideoErr
  53. EGAErr
  54. TOR.Sect
  55. RowErrq
  56. Begin
  57. Highest
  58. Lowest
  59. Border
  60. T.xcC
  61. IsUp_
  62. IsDown 
  63.     VC.Colors
  64.     VC.Atribs8
  65. VC.XPix
  66. VC.YPix
  67. VC.TCOL
  68. VC.TROW
  69. XSect
  70. YPanel
  71. CXRot
  72. SXRot
  73. CYRotI
  74. SYRot>
  75. XInc'
  76. FirstY
  77. LastAtr
  78. ENTER
  79. ESCAPE{
  80.     DOWNARROW
  81. UPARROWl
  82.     LEFTARROW
  83. RIGHTARROW
  84. Fields
  85. TOR.YDegree
  86. TOR.Bord
  87.     TOR.Delay
  88. FirstClr
  89. LastClr
  90. Toggle
  91.     Partition
  92.     RandIndex
  93.   TORUS
  94.    This program draws a Torus figure. The program accepts user input
  95.    to specify various TORUS parameters. It checks the current system
  96.    configuration and takes appropriate action to set the best possible
  97.    initial mode.
  98.  General purpose constants
  99.  Rotation flagse
  100.  Constants for best available screen mode=
  101.  User-defined type for tiles - an array of these make a torus=
  102.  User-defined type to hold information about the mode 
  103.  User-defined type to hold information about current Torus
  104.  A palette of colors to paint with
  105.  Error variables to check screen typen
  106.  The code of the module-level program begins heree
  107.  Initialize defaults
  108.  Get best configuration and set initial graphics mode to it
  109.  Loop forever (exit is from within a SUB)m
  110.  Get Torus definition from userf
  111.  Dynamically dimension arrayse
  112.  Array for indexes
  113.  Turn on error trap for insufficient memorya
  114.  Array for tiles
  115.  Initialize array of indexes
  116.  Calculate the points of each tile on the torusB
  117. Calculating"8
  118.  Color each tile in the torus.
  119.  Sort the tiles by their "distance" from the screen
  120. Sorting"8
  121.  Set the screen mode
  122.  Mix a palette of colors
  123.  Set logical window with variable thicknesse
  124.  Center is 0, up and right are positive, down and left are negative=
  125.  Draw and paint the tiles, the farthest first and nearest last
  126.  Rotate the torus by rotating the color paletten
  127.  Restore original rows
  128.  Error trap to make torus screen independent
  129.  Fall through until something workst
  130. 12789"
  131. Sorry. Graphics not available. Can't run Torus."
  132.  Trap to detect 64K EGAa
  133. 12789"
  134.  Trap to detect insufficient memory for large Toruse
  135. Out of memory"
  136. Reducing panels from
  137. Reducing sections from
  138.  Trap to determine initial number of rows so they can be restoredv
  139. GetConfig
  140. = GetConfig 
  141.    Get the starting number of lines and the video adapter.
  142.  Assume 50 line display and fall through error
  143.  until we get the actual numberl
  144.  Assume best possible screen mode
  145. 12789BCD
  146.  Fall through error trap until a mode worksr
  147.  If EGA, then check pages to see whether more than 64K
  148.  Reset text mode
  149. SetPalette
  150. = SetPalette 
  151.    Mixes palette colors in an array.
  152.  Mix only if the adapter supports color attributes
  153.  Red, green, blue, and intense in four bits of a byte=
  154.  Bits: 
  155. 0irgb
  156.  Change the order of FOR loops to change color mix
  157.  EGA red, green, and blue colors in 6 bits of a byte
  158.  Capital letters repesent intense, lowercase normale
  159.  Bits:  00rgbRGB
  160.  Change the order of FOR loops to change color mix
  161.  VGA colors in 6 bits of 3 bytes of a long integer
  162.  Bits:  
  163.     0 00
  164.  Change the order of FOR loops to change color mix
  165.  Decrease the STEP and increase VC.Colors to get more colors
  166.  Assign colors
  167. TorusDefine
  168. = TorusDefine 
  169.    Define the attributes of a Torus based on information from the=
  170.    user, the video configuration, and the current screen mode.
  171.  Constants for key codes and column positions=
  172.  Display key instructions
  173. UP .............. Move to next field
  174. DOWN ........ Move to previous field
  175. LEFT ......... Rotate field value up
  176. RIGHT ...... Rotate field value down
  177. ENTER .... Start with current values
  178. ESCAPE .................. Quit Torus
  179.  Block cursor.
  180.  Display fields
  181. Thickness"
  182. [ # ]"
  183. Panels per Section
  184. [ ## ]
  185. Sections per Torus
  186. [ ## ]
  187. Tilt around Horizontal Axis"
  188. [ ### ]"
  189. Tilt around Vertical Axis"
  190. [ ### ]"
  191. Tile Border"
  192. [ & ] 
  193. Screen Mode"
  194. [ ## ]
  195.  Skip field 10 if there's only one value
  196.  Update field values and position based on keystrokes
  197.  Put cursor on field
  198.  Get a key and strip null off if it's an extended code
  199.  End program
  200.  Adjust field location
  201.  Adjust field
  202.  Thickness
  203.  Panels
  204.  Sections
  205.  Horizontal tilt
  206.  Vertical tilt
  207.  Border
  208.  Available screen modes
  209.  Set configuration data for graphics mode
  210.  Draw Torus if ENTER
  211.  Remove cursor
  212.  Set different delays depending on moded
  213.  Get new random seed for this torusm
  214. TorusCalc
  215. = TorusCalc 
  216.    Calculates the x and y coordinates for each tile.
  217.  Calculate sine and cosine of the angles of rotation
  218.  Calculate the angle to increment between one tile and the next.
  219.  First calculate the first point, which will be used as a reference=
  220.  for future points. This point must be calculated separately because
  221.  it is both the beginning and the end of the center seam.e
  222.  Starting point is x1 of 0 section, 0 panel
  223.  last
  224.  Also x2 of tile on last section, 0 panel   ' |
  225.  | last
  226.  x3|x4
  227.  Also x3 of last section, last panel
  228.  x2|x1
  229.  |  0'
  230.  Also x4 of 0 section, last panel
  231.  A similar pattern is used for assigning all points of Torus
  232.  Starting Y point is 0 (center)a
  233.  Only one z coordinate is used in sort, so other three can be ignored
  234.  Starting at first point, work around the center seam of the Torus.e
  235.  Assign points for each section. The seam must be calculated separately
  236.  because it is both beginning and of each section.
  237.  X, Y, and Z elements of equation 
  238.  Now start at the first seam between panel and assign points for
  239.  each section of each panel. The outer loop assigns the initialr
  240.  point for the panel. This point must be calculated separately
  241.  since it is both the beginning and the end of the seam of panels.
  242.  X, Y, and Z elements of equationn
  243.  Assign X points for each panel
  244.  Current ring, current sidea
  245.  Current ring minus 1, next side
  246.  Current ring minus 1, previous side
  247.  Current ring, previous side
  248.  Assign Y points for each panel
  249.  Z point for each panel
  250.  The inner loop assigns points for each ring (except the first)
  251.  on the current side.n
  252.  Display section and panel
  253.  Erase message
  254. TorusColor
  255. = TorusColor 
  256.    Assigns color atributes to each tile.
  257.  Skip first and last atributes
  258.  Cycle through each attribute until all tiles are done
  259. TorusSort
  260. = TorusSort 
  261.    Sorts the tiles of the Torus according to their Z axis (distancet
  262.    from the "front" of the screen). When the tiles are drawn, thec
  263.    farthest will be drawn first, and nearer tiles will overwrite them
  264.    to give a three-dimensional effect.
  265.    To make sorting as fast as possible, the Quick Sort algorithm ise
  266.    used. Also, the array of tiles is not actually sorted. Instead ae
  267.    parallel array of tile indexes is sorted. This complicates things,
  268.    but makes the sort much faster, since two-byte integers are swapped
  269.    instead of 46-byte Tile variables.n
  270.  If only one, compare and swap if necessary=
  271.  The SUB procedure only stops recursing when it reaches this point
  272.  If more than one, separate into two random groups
  273.  Sort one group
  274.  Find the largest
  275.  Find the smallest
  276.  Swap them if necessary
  277.  Now get the other group and recursively sort it
  278. TorusDraw
  279. = TorusDraw 
  280.    Draws each tile of the torus starting with the farthest and working
  281.    to the closest. Thus nearer tiles overwrite farther tiles to give
  282.    a three-dimensional effect. Notice that the index of the tile being
  283.    drawn is actually the index of an array of indexes. This is because
  284.    the array of tiles is not sorted, but the parallel array of indexes
  285.    is. See TorusSort for an explanation of how indexes are sorted.
  286. TileDraw
  287. = TileDraw 
  288.    Draw and optionally paint a tile. Tiles are painted if there are
  289.    more than two atributes and if the inside of the tile can be found.
  290. Set border
  291.  Draw and quit for two-color modes
  292.  For other modes, draw in the border color
  293.  (which must be different than any tile color)
  294.  See if tile is large enough to be painted
  295. Black out the center to make sure it isn't paint color
  296.  Paint tile black so colors of underlying tiles can't interfere=
  297.  Fill with the final tile color.
  298.  A border drawn with the background color looks like a border.
  299.  One drawn with the tile color doesn't look like a border.
  300.  Redraw with the final borderr
  301. TorusRotate
  302. = TorusRotate 
  303.    Rotates the Torus. This can be done more successfully in some modes
  304.    than in others. There are three methods: 
  305.  1. Rotate the palette colors assigned to each attribute
  306.  2. Draw, erase, and redraw the torus (two-color modes)e
  307.  3. Rotate between two palettes (CGA and MCGA screen 1)e
  308.    Note that for EGA and VGA screen 2, methods 1 and 2 are both used.s
  309.  For EGA and higher rotate colors through palette=
  310.  Argument determines whether to start at next color, first color,=
  311.  or random color
  312.  Set last color to smaller of last possible color or last tile
  313.  If color is too low, rotate to endp
  314.  Set last attribute
  315.  Last for two-color modes
  316.  Smaller of last color or next-to-last attribute
  317.  Cycle through attributes, assigning colorsi
  318.  For two-color screens, the best we can do is erase and redraw the torus
  319.  Set all tiles to color 
  320.  Draw Torus
  321.  Toggle between color and background
  322.  For CGA or MCGA screen 1, toggle palettes using the COLOR statement
  323.  (these modes do not allow the PALETTE statement)t
  324. Delay
  325. = Delay 
  326.    Delay based on time so that wait will be the same on any processor.
  327.    Notice the check for negative numbers so that the delay won't
  328.    freeze at midnight when the delay could become negative.w
  329. CountTiles
  330. = CountTiles 
  331.    Displays number of the tiles currently being calculated or sorted.
  332.  Erase previous
  333.  If positive, display - give negative values to erase=
  334. Tile "
  335. Message
  336. = Message 
  337.    Displays a status message followed by blinking dots.
  338.  White
  339.  Blink
  340.  . . .
  341.  White
  342. SetConfig
  343. = SetConfig 
  344.    Sets the correct values for each field of the VC variable. They
  345.    vary depending on Mode and on the current configuration.e
  346.  Four-color graphics for CGA, EGA, VGA, and MCGA
  347.  Two-color medium-res graphics for CGA, EGA, VGA, and MCGA
  348.  Two-color high-res graphics for HerculesG
  349.  16-color medium-res graphics for EGA and VGAV
  350.  16-color high-res graphics for EGA and VGAG
  351.  16- or 4-color very high-res graphics for EGA and VGA
  352.  Two-color high-res graphics for EGA or VGA monochrome
  353.  Two-color very high-res graphics for VGA and MCGA
  354.  Note that for VGA screens 11, 12, and 13, more colors are
  355.  available, depending on how the colors are mixed.
  356.  16-color very high-res graphics for VGA
  357.  256-color medium-res graphics for VGA and MCGAs
  358. Inside
  359. = Inside 
  360.    Finds a point, T.xc and T.yc, that is mathematically within a tile.
  361.    Then check to see if the point is actually inside. Because of the
  362.    jagged edges of tiles, the center point is often actually insidee
  363.    very thin tiles. Such tiles will not be painted, This causess
  364.    imperfections that are often visible at the edge of the Torus.
  365.    Return FALSE if a center point is not found inside a tile.r
  366.  Find an inside point. Since some tiles are triangles, the
  367.  diagonal center isn't good enough. Instead find the center
  368.  by drawing a diagonal from the center of the outside to
  369.  a bottom corner.g
  370.  If we're on a border, no need to fill
  371.  Find highest and lowest Y on the tile
  372.  Convert coordinates to pixels
  373.  Search for top and bottom tile borders until we either find them
  374.  both, or check beyond the highest and lowest points.e
  375.  Search up
  376.  Search down
  377.  If top and bottom are found, we're inside
  378. DegToRad
  379. = DegToRad 
  380.    Convert degrees to radians, since BASIC trigonometric functions
  381.    require radians.t
  382. Rotated
  383. = Rotated 
  384.    Returns the Current value adjusted by Inc and rotated if necessary=
  385.    so that it falls within the range of Lower and Upper.
  386.  Calculate the next value
  387.  Handle special cases of rotating off top or bottom=
  388.