home *** CD-ROM | disk | FTP | other *** search
/ Garbo / Garbo.cdr / mac / progrmng / qbgrphcs.sit / antialias.bas / antialias.bas
Encoding:
BASIC Source File  |  1991-06-05  |  5.4 KB  |  202 lines  |  [TEXT/MSBB]

  1. '------------------------------------------------------------------------------
  2. ' TITLE:    antialias.bas
  3. ' DATE:     February 19, 1991
  4. ' AUTHOR: R. Gonzalez
  5. '
  6. ' DESCRIPTION:    Draw circles using various techniques, including antialiasing.
  7. '     REQUIRES COLOR OR GRAY-SCALE MONITOR.
  8. '
  9. ' COMPILING:    Remove STATIC declarations, uncomment indicated lines
  10. '     Check: Include MBPCs & MBLCs, Include runtime code, Make all arrays static,
  11. '     Use default window & menu, (if available: Generate 68020 & 68881 code).
  12. '
  13. ' (MODIFICATION HISTORY)
  14. ' DATE:
  15. ' AUTHOR:
  16. ' DESCRIPTION:
  17. '------------------------------------------------------------------------------
  18.  
  19. ' these variables may be shared among several subprograms:
  20. DIM SHARED TRUE%,FALSE%,PI,white%,light.gray%,gray%,black%
  21.  
  22. '------------------------------------------------------------------------------
  23. ' main procedure
  24. '------------------------------------------------------------------------------
  25. 'MAIN
  26.  
  27.     DIM window.width%,window.height%,x%,y%,radius%
  28.  
  29.     TRUE% = -1
  30.     FALSE% = 0
  31.     PI = 3.14159
  32.     white% = 30
  33.     light.gray% = 273
  34.     gray% = 137
  35.     black% = 33
  36.     forecolor white%
  37.     backcolor black%
  38.    
  39.     window.width% = WINDOW(2)
  40.     window.height% = WINDOW(3)
  41.     radius% = 50
  42.     y% = INT(window.height%/2)
  43.  
  44.     CLS
  45.     x% = INT(window.width%/4)
  46.     CIRCLE (x%,y%),radius%
  47.     x% = INT(window.width%/2)
  48.     draw.circle x%,y%,radius%,FALSE%
  49.     x% = INT(3*window.width%/4)
  50.     draw.circle x%,y%,radius%,TRUE%
  51.  
  52.     pause
  53.  
  54. END
  55.  
  56. '------------------------------------------------------------------------------
  57. ' pause until mouse or any key is pressed
  58. '------------------------------------------------------------------------------
  59. SUB pause STATIC
  60.  
  61.     WHILE INKEY$ = "" AND MOUSE(0) <> 1 
  62.     WEND
  63.         
  64. END SUB
  65.  
  66. '------------------------------------------------------------------------------
  67. ' draw circle using line-drawing routine, either antialiased or not
  68. '------------------------------------------------------------------------------
  69. SUB draw.circle (x%,y%,radius%,anti%) STATIC
  70.  
  71.     'for compiler only:
  72. '    dim increment,angle,x1,y1,x2,y2
  73.  
  74.     increment = .1
  75.     x1 = radius%+x%
  76.     y1 = y%
  77.     
  78.     FOR angle = 0 TO 2*PI+increment STEP increment
  79.         x2 = radius%*COS(angle)+x%
  80.         y2 = radius%*SIN(angle)+y%
  81.         draw.line x1,y1,x2,y2,anti%
  82.         x1 = x2
  83.         y1 = y2
  84.     NEXT
  85.     
  86. END SUB
  87.  
  88. '------------------------------------------------------------------------------
  89. ' draw plain line using white or antialiased line using white and grays.  Use
  90. ' floating-point arguments so that antialiasing procedure will work properly
  91. ' with very short lines, e.g. when drawing curves.
  92. '------------------------------------------------------------------------------
  93. SUB draw.line (x1.param,y1.param,x2.param,y2.param,anti%) STATIC
  94.  
  95.     'for compiler only:
  96. '    dim m,yp,x1,y1,x2,y2
  97. '    dim x%,ya%,yb%,switch.xy%,color.a%,color.b%
  98.  
  99.     ' local variables because of call-by-reference:
  100.     x1 = x1.param
  101.     y1 = y1.param
  102.     x2 = x2.param
  103.     y2 = y2.param
  104.  
  105.     convert.line x1,y1,x2,y2,m,switch.xy%
  106.  
  107.     FOR x% = INT(x1+.5) TO INT(x2+.5)
  108.         yp = m*(x%-x1)+y1
  109.         ya% = INT(yp+.5)
  110.         IF NOT anti% THEN
  111.             color.a% = white%
  112.         ELSE
  113.             IF ya%<yp THEN
  114.                 yb% = ya% + 1
  115.             ELSE
  116.                 yb% = ya% - 1
  117.             END IF
  118.             ' For efficiency it may be better to later use inline code rather than a
  119.             ' subprogram call here, since we are in a critical loop:
  120.             select.anti.colors ABS(ya%-yp),color.a%,color.b%
  121.             forecolor color.b%
  122.             IF switch.xy% THEN
  123.                 PSET (yb%,x%)
  124.             ELSE
  125.                 PSET (x%,yb%)
  126.             END IF
  127.         END IF
  128.         forecolor color.a%
  129.         IF switch.xy% THEN
  130.             PSET (ya%,x%)
  131.         ELSE
  132.             PSET (x%,ya%)
  133.         END IF
  134.     NEXT
  135.     
  136. END SUB
  137.  
  138. '------------------------------------------------------------------------------
  139. ' convert line if necessary so that slope<=1 and x1<x2.  If switch.xy% is set to TRUE
  140. ' then the calling routine should switch x and y coordinates when drawing the line.
  141. '------------------------------------------------------------------------------
  142. SUB convert.line (x1,y1,x2,y2,m,switch.xy%) STATIC
  143.  
  144.     'for compiler only:
  145. '    dim temp
  146.  
  147.     IF ABS(x2-x1)<.000001 THEN
  148.         m = 1000000!
  149.     ELSE
  150.         m = (y2-y1)/(x2-x1)
  151.     END IF
  152.  
  153.     IF ABS(m) > 1 THEN
  154.         temp = x1
  155.         x1 = y1
  156.         y1 = temp
  157.         temp = x2
  158.         x2 = y2
  159.         y2 = temp
  160.         IF ABS(x2-x1)<.000001 THEN
  161.             m = 1000000!
  162.         ELSE
  163.             m = (y2-y1)/(x2-x1)
  164.         END IF
  165.         switch.xy% = TRUE%
  166.     ELSE
  167.         switch.xy% = FALSE%
  168.     END IF
  169.  
  170.     IF x1 > x2 THEN
  171.         temp = x1
  172.         x1 = x2
  173.         x2 = temp
  174.         temp = y1
  175.         y1 = y2
  176.         y2 = temp
  177.     END IF
  178.  
  179. END SUB
  180.  
  181. '------------------------------------------------------------------------------
  182. ' select pixel colors for antialiasing line.
  183. '------------------------------------------------------------------------------
  184. SUB select.anti.colors (distance,color.a%,color.b%) STATIC
  185.  
  186.     SELECT CASE distance
  187.         CASE 0 TO .1
  188.             color.a% = white%
  189.             color.b% = black%
  190.         CASE .1 TO .25
  191.             color.a% = white%
  192.             color.b% = gray%
  193.         CASE .25 TO .4
  194.             color.a% = white%
  195.             color.b% = light.gray%
  196.         CASE ELSE
  197.             color.a% = light.gray%
  198.             color.b% = light.gray%
  199.     END SELECT
  200.  
  201. END SUB
  202.