' RANMERGE.BAS -- by Gary Neal, Jr. (garyneal@geeocities.com) ' ' Merged RANGRAPH.BAS, FGRAPH.BI, and FGRAPH.BAS files for use in QBasic. DECLARE SUB DrawCircle (xOffset%, yOffset%, radius%, C%) DECLARE SUB DrawLine (X1%, Y1%, X2%, Y2%, C%) DIM SHARED angleIncr AS SINGLE SCREEN 13 ' Random pixels WHILE INKEY$ = "" PSET (RND * 320, RND * 200), RND * 256 WEND ' Clear keyboard WHILE INKEY$ <> "": WEND CLS ' Random lines WHILE INKEY$ = "" DrawLine RND * 320, RND * 200, RND * 320, RND * 200, RND * 256 WEND ' Clear keyboard WHILE INKEY$ <> "": WEND CLS ' Set angle increments directly in this program. angleIncr = .5 DrawCircle 160, 100, 90, 15 ' Wait for keypress WHILE INKEY$ = "": WEND ' Clear the keyboard WHILE INKEY$ <> "": WEND ' Clean up and exit SCREEN 0, 0, 0 END ' You can use SYSTEM also. ' 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