Dragon curve: Difference between revisions

Content added Content deleted
(→‎{{header|QuickBASIC}}: Added a solution.)
(→‎{{header|QuickBASIC}}: Introduced some parameters in the recursive subroutine (especially for a level and instead of the array simulating a stack).)
Line 977: Line 977:


==={{header|QuickBASIC}}===
==={{header|QuickBASIC}}===
{{trans|GW-BASIC|Introduced some parameters in the recursive subroutine (especially for a level and instead of the array simulating a stack).}}
{{trans|GW-BASIC}}
<syntaxhighlight lang="basic">
<syntaxhighlight lang="basic">
REM Dragon curve
REM Dragon curve
REM SIN, COS in arrays for PI/4 multipl.
REM SIN, COS in arrays for PI/4 multipl.
DECLARE SUB Dragon ()
DECLARE SUB Dragon (BYVAL Insize!, BYVAL Level%, BYVAL RQ%)
DIM SHARED S(7), C(7), X, Y, RotQPi%, Level%, Insize, RQ%, SQ
DIM SHARED S(7), C(7), X, Y, RotQPi%
CONST QPI = .785398163397448# ' PI / 4
QPi = ATN(1)
SQ = SQR(2)
FOR I = 0 TO 7
FOR I = 0 TO 7
S(I) = SIN(I * QPi)
S(I) = SIN(I * QPI)
C(I) = COS(I * QPi)
C(I) = COS(I * QPI)
NEXT I
NEXT I
Level% = 15
Insize = 128: REM 2^WHOLE_NUM (looks better)
X = 112: Y = 70
X = 112: Y = 70
RotQPi% = 0: RQ% = 1
DIM SHARED R%(Level%)
SCREEN 2: CLS
SCREEN 2: CLS
CALL Dragon(128, 15, 1) ' Insize = 2^WHOLE_NUM (looks better)
CALL Dragon
END
END


SUB Dragon (BYVAL Insize, BYVAL Level%, BYVAL RQ%)
SUB Dragon
CONST SQ = 1.4142135623731# ' SQR(2)
RotQPi% = RotQPi% AND 7
IF Level% <= 1 THEN
IF Level% <= 1 THEN
YN = S(RotQPi%) * Insize + Y
XN = C(RotQPi%) * Insize + X
XN = C(RotQPi%) * Insize + X
YN = S(RotQPi%) * Insize + Y
LINE (2 * X, Y)-(2 * XN, YN): REM For SCREEN 2 doubled x-coords
LINE (2 * X, Y)-(2 * XN, YN) ' For SCREEN 2 doubled x-coords
X = XN: Y = YN
X = XN: Y = YN
ELSE
ELSE
Insize = Insize * SQ / 2
RotQPi% = (RotQPi% + RQ%) AND 7
RotQPi% = (RotQPi% + RQ%) AND 7
Level% = Level% - 1
CALL Dragon(Insize / SQ, Level% - 1, 1)
R%(Level%) = RQ%: RQ% = 1
RotQPi% = (RotQPi% - RQ% * 2) AND 7
CALL Dragon
CALL Dragon(Insize / SQ, Level% - 1, -1)
RotQPi% = (RotQPi% - R%(Level%) * 2) AND 7
RQ% = -1
CALL Dragon
RQ% = R%(Level%)
RotQPi% = (RotQPi% + RQ%) AND 7
RotQPi% = (RotQPi% + RQ%) AND 7
Level% = Level% + 1
Insize = Insize * SQ
END IF
END IF
END SUB
END SUB