-' Program to render spiral resembling fractal made out of circles.\r
-' By Svjatoslav Agejenko.\r
+DECLARE SUB drawFractal (x!, y!, angle AS SINGLE, size AS SINGLE, w AS INTEGER)\r
+' Program Title: Spiral Fractal Renderer\r
+' Description: Renders a spiral fractal made up of circles using recursion in QBasic.\r
+' Author: Svjatoslav Agejenko\r
' Email: svjatoslav@svjatoslav.eu\r
' Homepage: http://www.svjatoslav.eu\r
'\r
' 2003.12, Initial version\r
' 2024.08, Improved program readability using AI\r
\r
-DECLARE SUB drawFractal (x!, y!, angle!, size!, w!)\r
-DIM SHARED depth\r
-DIM SHARED pi\r
+' Declare shared variables to be used across the program\r
+DIM SHARED depth AS INTEGER\r
+DIM SHARED pi AS SINGLE\r
\r
-DIM SHARED scaleH1, scaleH2, scaleV1, scaleV2, hp, vp\r
+' Define scaling factors for horizontal and vertical directions\r
+DIM SHARED scaleH1 AS SINGLE, scaleH2 AS SINGLE, scaleV1 AS SINGLE, scaleV2 AS SINGLE\r
+DIM SHARED hp AS SINGLE, vp AS SINGLE\r
\r
-pi = 3.14128\r
+' Initialize constants\r
+pi = 3.14159\r
\r
-SCREEN 12\r
+SCREEN 12 ' Set the screen mode to 12 for better graphics quality\r
\r
-scaleV1 = 5\r
-scaleV2 = 2\r
-vp = .2\r
+' Define scaling factors and direction variables\r
+scaleV1 = 5 ' Vertical scale factor for one direction\r
+scaleV2 = 2 ' Vertical scale factor for another direction\r
+vp = .2 ' Vertical angle increment\r
\r
-scaleH1 = 2\r
-scaleH2 = 1.4\r
-hp = .2\r
+scaleH1 = 2 ' Horizontal scale factor for one direction\r
+scaleH2 = 1.4 ' Horizontal scale factor for another direction\r
+hp = .2 ' Horizontal angle increment\r
\r
+' Start the fractal drawing process from the center of the screen\r
drawFractal 320, 240, pi - .9, 50, 0\r
\r
+' Wait for user input to exit the program gracefully\r
a$ = INPUT$(1)\r
-SYSTEM\r
+SYSTEM ' Terminate the program\r
\r
-SUB drawFractal (x, y, angle, size, w)\r
- depth = depth + 1\r
- IF size < .2 THEN GOTO 1\r
+SUB drawFractal (x, y, angle AS SINGLE, size AS SINGLE, w AS INTEGER)\r
+ depth = depth + 1 ' Increment the recursion depth\r
+\r
+ IF size < .2 THEN GOTO 1 ' Base case for recursion termination\r
\r
' Determine color based on the current depth\r
- IF depth / 2 = depth \ 2 THEN\r
- c = 15\r
+ IF depth MOD 2 = 0 THEN\r
+ c = 15 ' White\r
ELSE\r
- c = 10\r
+ c = 10 ' Light green\r
END IF\r
\r
- CIRCLE (x, y), size, c\r
- PAINT (x, y), c\r
+ CIRCLE (x, y), size, c ' Draw a circle at the current position and size with specified color\r
+ PAINT (x, y), c ' Fill the circle to create a solid shape\r
\r
- ' Recursive calls for different directions\r
+ ' Recursive calls for different directions based on the parameter w\r
IF w <> 1 THEN\r
+ ' Calculate new coordinates after rotating angle + vp degrees\r
x1 = SIN(angle) * size * 2.5 + x\r
y1 = COS(angle) * size * 2.5 + y\r
+\r
+ ' Determine scaling factor based on the direction\r
IF w = 3 THEN\r
newSize = size / scaleV2\r
ELSE\r
newSize = size / scaleV1\r
END IF\r
- drawFractal x1, y1, angle + vp, newSize, 3\r
+\r
+ drawFractal x1, y1, angle + vp, newSize, 3 ' Recursive call to the same subroutine with updated parameters\r
END IF\r
\r
IF w <> 2 THEN\r
- x1 = SIN(angle - pi / 2) * size * 2.5 + x\r
- y1 = COS(angle - pi / 2) * size * 2.5 + y\r
+ ' Calculate new coordinates after rotating angle - pi/2 degrees\r
+ x1 = SIN(angle - .5 * pi) * size * 2.5 + x\r
+ y1 = COS(angle - .5 * pi) * size * 2.5 + y\r
+\r
+ ' Determine scaling factor based on the direction\r
IF w = 4 THEN\r
newSize = size / scaleH2\r
ELSE\r
newSize = size / scaleH1\r
END IF\r
- drawFractal x1, y1, angle + hp, newSize, 4\r
+\r
+ drawFractal x1, y1, angle + hp, newSize, 4 ' Recursive call to the same subroutine with updated parameters\r
END IF\r
\r
IF w <> 3 THEN\r
+ ' Calculate new coordinates after rotating angle - pi degrees\r
x1 = SIN(angle - pi) * size * 2.5 + x\r
y1 = COS(angle - pi) * size * 2.5 + y\r
+\r
+ ' Determine scaling factor based on the direction\r
IF w = 1 THEN\r
newSize = size / scaleV2\r
ELSE\r
newSize = size / scaleV1\r
END IF\r
- drawFractal x1, y1, angle + vp, newSize, 1\r
+\r
+ drawFractal x1, y1, angle + vp, newSize, 1 ' Recursive call to the same subroutine with updated parameters\r
END IF\r
\r
IF w <> 4 THEN\r
- x1 = SIN(angle - pi * 1.5) * size * 2.5 + x\r
- y1 = COS(angle - pi * 1.5) * size * 2.5 + y\r
+ ' Calculate new coordinates after rotating angle - pi*3/2 degrees\r
+ x1 = SIN(angle - 1.5 * pi) * size * 2.5 + x\r
+ y1 = COS(angle - 1.5 * pi) * size * 2.5 + y\r
+\r
+ ' Determine scaling factor based on the direction\r
IF w = 2 THEN\r
newSize = size / scaleH2\r
ELSE\r
newSize = size / scaleH1\r
END IF\r
- drawFractal x1, y1, angle + hp, newSize, 2\r
+\r
+ drawFractal x1, y1, angle + hp, newSize, 2 ' Recursive call to the same subroutine with updated parameters\r
END IF\r
\r
- 1\r
- depth = depth - 1\r
+1\r
+ depth = depth - 1 ' Decrement recursion depth after all recursive calls are made\r
END SUB\r
+\r