home *** CD-ROM | disk | FTP | other *** search
- '------------------------------------------------------------------------------
- ' TITLE: antialias.bas
- ' DATE: February 19, 1991
- ' AUTHOR: R. Gonzalez
- '
- ' DESCRIPTION: Draw circles using various techniques, including antialiasing.
- ' REQUIRES COLOR OR GRAY-SCALE MONITOR.
- '
- ' COMPILING: Remove STATIC declarations, uncomment indicated lines
- ' Check: Include MBPCs & MBLCs, Include runtime code, Make all arrays static,
- ' Use default window & menu, (if available: Generate 68020 & 68881 code).
- '
- ' (MODIFICATION HISTORY)
- ' DATE:
- ' AUTHOR:
- ' DESCRIPTION:
- '------------------------------------------------------------------------------
-
- ' these variables may be shared among several subprograms:
- DIM SHARED TRUE%,FALSE%,PI,white%,light.gray%,gray%,black%
-
- '------------------------------------------------------------------------------
- ' main procedure
- '------------------------------------------------------------------------------
- 'MAIN
-
- DIM window.width%,window.height%,x%,y%,radius%
-
- TRUE% = -1
- FALSE% = 0
- PI = 3.14159
- white% = 30
- light.gray% = 273
- gray% = 137
- black% = 33
- forecolor white%
- backcolor black%
-
- window.width% = WINDOW(2)
- window.height% = WINDOW(3)
- radius% = 50
- y% = INT(window.height%/2)
-
- CLS
- x% = INT(window.width%/4)
- CIRCLE (x%,y%),radius%
- x% = INT(window.width%/2)
- draw.circle x%,y%,radius%,FALSE%
- x% = INT(3*window.width%/4)
- draw.circle x%,y%,radius%,TRUE%
-
- pause
-
- END
-
- '------------------------------------------------------------------------------
- ' pause until mouse or any key is pressed
- '------------------------------------------------------------------------------
- SUB pause STATIC
-
- WHILE INKEY$ = "" AND MOUSE(0) <> 1
- WEND
-
- END SUB
-
- '------------------------------------------------------------------------------
- ' draw circle using line-drawing routine, either antialiased or not
- '------------------------------------------------------------------------------
- SUB draw.circle (x%,y%,radius%,anti%) STATIC
-
- 'for compiler only:
- ' dim increment,angle,x1,y1,x2,y2
-
- increment = .1
- x1 = radius%+x%
- y1 = y%
-
- FOR angle = 0 TO 2*PI+increment STEP increment
- x2 = radius%*COS(angle)+x%
- y2 = radius%*SIN(angle)+y%
- draw.line x1,y1,x2,y2,anti%
- x1 = x2
- y1 = y2
- NEXT
-
- END SUB
-
- '------------------------------------------------------------------------------
- ' draw plain line using white or antialiased line using white and grays. Use
- ' floating-point arguments so that antialiasing procedure will work properly
- ' with very short lines, e.g. when drawing curves.
- '------------------------------------------------------------------------------
- SUB draw.line (x1.param,y1.param,x2.param,y2.param,anti%) STATIC
-
- 'for compiler only:
- ' dim m,yp,x1,y1,x2,y2
- ' dim x%,ya%,yb%,switch.xy%,color.a%,color.b%
-
- ' local variables because of call-by-reference:
- x1 = x1.param
- y1 = y1.param
- x2 = x2.param
- y2 = y2.param
-
- convert.line x1,y1,x2,y2,m,switch.xy%
-
- FOR x% = INT(x1+.5) TO INT(x2+.5)
- yp = m*(x%-x1)+y1
- ya% = INT(yp+.5)
- IF NOT anti% THEN
- color.a% = white%
- ELSE
- IF ya%<yp THEN
- yb% = ya% + 1
- ELSE
- yb% = ya% - 1
- END IF
- ' For efficiency it may be better to later use inline code rather than a
- ' subprogram call here, since we are in a critical loop:
- select.anti.colors ABS(ya%-yp),color.a%,color.b%
- forecolor color.b%
- IF switch.xy% THEN
- PSET (yb%,x%)
- ELSE
- PSET (x%,yb%)
- END IF
- END IF
- forecolor color.a%
- IF switch.xy% THEN
- PSET (ya%,x%)
- ELSE
- PSET (x%,ya%)
- END IF
- NEXT
-
- END SUB
-
- '------------------------------------------------------------------------------
- ' convert line if necessary so that slope<=1 and x1<x2. If switch.xy% is set to TRUE
- ' then the calling routine should switch x and y coordinates when drawing the line.
- '------------------------------------------------------------------------------
- SUB convert.line (x1,y1,x2,y2,m,switch.xy%) STATIC
-
- 'for compiler only:
- ' dim temp
-
- IF ABS(x2-x1)<.000001 THEN
- m = 1000000!
- ELSE
- m = (y2-y1)/(x2-x1)
- END IF
-
- IF ABS(m) > 1 THEN
- temp = x1
- x1 = y1
- y1 = temp
- temp = x2
- x2 = y2
- y2 = temp
- IF ABS(x2-x1)<.000001 THEN
- m = 1000000!
- ELSE
- m = (y2-y1)/(x2-x1)
- END IF
- switch.xy% = TRUE%
- ELSE
- switch.xy% = FALSE%
- END IF
-
- IF x1 > x2 THEN
- temp = x1
- x1 = x2
- x2 = temp
- temp = y1
- y1 = y2
- y2 = temp
- END IF
-
- END SUB
-
- '------------------------------------------------------------------------------
- ' select pixel colors for antialiasing line.
- '------------------------------------------------------------------------------
- SUB select.anti.colors (distance,color.a%,color.b%) STATIC
-
- SELECT CASE distance
- CASE 0 TO .1
- color.a% = white%
- color.b% = black%
- CASE .1 TO .25
- color.a% = white%
- color.b% = gray%
- CASE .25 TO .4
- color.a% = white%
- color.b% = light.gray%
- CASE ELSE
- color.a% = light.gray%
- color.b% = light.gray%
- END SELECT
-
- END SUB
-