' FGRAPH.BAS -- by Gary Neal, Jr.
'
' Draws Circles and Lines on the graphics screen using floating point math.
'
'$INCLUDE: 'fgraph.bi'
' Contains the value to increment the Circle's angle with.
DIM SHARED angleIncr AS SINGLE
' DrawCircle
' Draws a circle on the graphics screen using floating point math.
'
SUB DrawCircle (xOffset%, yOffset%, radius%, C%)
DIM xPlot!, yPlot! ' Current pixel being plotted.
DIM angle!, angleRad! ' Current angle in degrees & radiens.
IF angleIncr < 1! THEN angleIncr = .5
FOR angle! = 0! TO 360! STEP angleIncr
' Convert degrees to radiens.
angleRad! = angle! * (3.141592654# / 180!)
' Convert polar to rectangular coordinates.
xPlot! = (radius% * COS(angleRad!)) + xOffset%
yPlot! = (radius% * SIN(angleRad!)) + yOffset%
' Check boundaries.
IF xPlot! < 0 THEN
xPlot! = 0
ELSEIF xPlot! > 319 THEN
xPlot! = 319
END IF
IF yPlot! < 0 THEN
yPlot! = 0
ELSEIF yPlot! > 199 THEN
yPlot! = 199
END IF
' Plot the pixel on the graphics screen.
PSET (xPlot!, yPlot!), C%
NEXT angle!
END SUB
' Line function
'
' Draws a line on the graphics screen using floating point math.
'
SUB DrawLine (X1%, Y1%, X2%, Y2%, C%)
DIM currX!, currY! ' Current pixel to be plotted
DIM deltaX!, deltaY!, slope! ' 2 deltas and slope
deltaX! = X2% - X1% ' Get value for delta X
deltaY! = Y2% - Y1% ' Get value for delta Y
IF ABS(deltaY!) > ABS(deltaX!) THEN
' |slope| > 1
IF Y2% < Y1% THEN
SWAP Y2%, Y1% ' Swap coordinates
SWAP X2%, X1% ' Y1 must be < Y2
END IF
' Compute the slope off the vertical axis
slope! = deltaX! / deltaY!
' Plot each pixel starting at (X1, X2)
currX! = X1%
FOR currY! = Y1% TO Y2%
PSET (currX!, currY!), C%
' Increment X by slope
currX! = currX! + slope!
NEXT currY!
EXIT SUB
ELSEIF deltaX! <> 0! OR deltaY! <> 0! THEN
' |slope| < 1
IF X2% < X1% THEN
SWAP X2%, X1% ' Swap coordinates
SWAP Y2%, Y1% ' X1 must be < X2
END IF
' Compute the slope off the horizontal axis
slope! = deltaY! / deltaX!
' Plot each pixel starting at (X1, X2)
currY! = Y1%
FOR currX! = X1% TO X2%
PSET (currX!, currY!), C%
' Increment X by slope
currY! = currY! + slope!
NEXT currX!
EXIT SUB
ELSE
' Set a single pixel because X1 = X2 and Y1 = Y2
PSET (X1%, X2%), C%
END IF
END SUB
' Returns the angleIncr value for other modules.
'
FUNCTION GetAngleIncr!
GetAngleIncr! = angleIncr
END FUNCTION
' Sets the angleIncr value for other modules.
'
SUB SetAngleIncr (newIncr AS SINGLE)
IF newIncr > 0 THEN
angleIncr = newIncr MOD 360
END IF
END SUB
               (
geocities.com/siliconvalley/park/7113/GameLib)                   (
geocities.com/siliconvalley/park/7113)                   (
geocities.com/siliconvalley/park)                   (
geocities.com/siliconvalley)