'Coded By Ashish in QB64
'Twiter : @KingOfCoders
_TITLE "Fractals [Part 2 : L-System]"
fractalsName(0) = "Board": fractalRules(0).token = "F": fractalRules(0).replace = "FF+F+F+F+FF"
fractalsName(1) = "Crystal": fractalRules(1).token = "F": fractalRules(1).replace = "FF+F++F+F"
fractalsName(2) = "Peano Curve": fractalRules(2).token = "X": fractalRules(2).replace = "XFYFX+F+YFXFY-F-XFYFX": fractalRules(3).token = "Y": fractalRules(3).replace = "YFXFY-F-XFYFX+F+YFXFY"
fractalsName(3) = "Quadratic Gosper": fractalRules(4).token = "X": fractalRules(4).replace = "XFX-YF-YF+FX+FX-YF-YFFX+YF+FXFXYF-FX+YF+FXFX+YF-FXYF-YF-FX+FX+YFYF-": fractalRules(5).token = "Y": fractalRules(5).replace = "+FXFX-YF-YF+FX+FXYF+FX-YFYF-FX-YF+FXYFYF-FX-YFFX+FX+YF-YF-FX+FX+YFY"
fractalsName(4) = "Quadratic Snowflake": fractalRules(6).token = "F": fractalRules(6).replace = "F-F+F+F-F"
fractalsName(5) = "Quadratic Koch Island": fractalRules(7).token = "F": fractalRules(7).replace = "F-FF+FF+F+F-F-FF+F+F-F-FF-FF+F"
fractalsName(6) = "Square Sierpinski": fractalRules(8).token = "X": fractalRules(8).replace = "XF-F+F-XF+F+XF-F+F-X"
fractalsName(7) = "Triangle": fractalRules(9).token = "F": fractalRules(9).replace = "F-F+F"
fractalsName(8) = "Von Koch Snowflake": fractalRules(10).token = "F": fractalRules(10).replace = "F-F++F-F"
fractalsName(9) = "Hilbert": fractalRules(11).token = "X": fractalRules(11).replace = "-YF+XFX+FY-": fractalRules(12).token = "Y": fractalRules(12).replace = "+XF-YFY-FX+"
fractalsName(10) = "Cross": fractalRules(13).token = "F": fractalRules(13).replace = "F+F-F+F+F"
fractalsName(11) = "Pentaplexity": fractalRules(14).token = "F": fractalRules(14).replace = "F++F++F|F-F++F"
choice = getChoice
g$ = prepareGrammer("F+F+F+F", 0, 0, 5)
angle! = 90
g$ = prepareGrammer("F+F+F+F", 1, 1, 5)
angle! = 90
g$ = prepareGrammer("X", 2, 3, 4)
angle! = 90
g$ = prepareGrammer("-YF", 4, 5, 3)
angle! = 90
g$ = prepareGrammer("F", 6, 6, 5)
angle! = 90
g$ = prepareGrammer("F+F+F+F", 7, 7, 3)
angle! = 90
g$ = prepareGrammer("F+XF+F+XF", 8, 8, 5)
angle! = 90
g$ = prepareGrammer("F", 9, 9, 7)
angle! = 120
g$ = prepareGrammer("F++F++F", 10, 10, 5)
angle! = 60
g$ = prepareGrammer("X", 11, 12, 7)
angle! = 90
g$ = prepareGrammer("F+F+F+F", 13, 13, 6)
angle! = 90
g$ = prepareGrammer("F++F++F++F++F", 14, 14, 5)
angle! = 36
showControl
x = 100
y = 100
r = 3
drawFractal g$, angle!, r, x, y
FUNCTION prepareGrammer$
(axm$
, s
, e
, N
) DIM fractalRule
(e
- s
) AS rule_type
fractalRule(i - s).token = fractalRules(i).token
fractalRule
(i
- s
).replace
= RTRIM$(fractalRules
(i
).replace
) prepareGrammer$ = generateGrammer$(axm$, fractalRule(), N)
_TITLE "Fractals [Part 2 : L-System]" PRINT i
+ 1;
". "; fractalsName
(i
) INPUT "Enter the fractal No. : ", getChoice
getChoice = getChoice - 1
'######################################################
'Fractal related subroutine
PRINT "Use 'w' and 's' to move up and down" PRINT "Use 'a' and 'd' to move left and rigth" PRINT "Use 'q' and 'e' to zoom-in and zoom-out" PRINT "Use 'Esc' to goto fractal menu"
SUB drawFractal
(grammer$
, ang!
, r
, x
, y
) ang2! = 0
px = x
py = y
ca$
= MID$(grammer$
, i
, 1) px = xx
py = yy
ang2! = ang2! + ang!
ang2! = ang2! - ang!
ang2! = ang2! + 180
px = xx
py = yy
'L-System Function
FUNCTION generateGrammer$
(axiom$
, rule
() AS rule_type
, n~&
) axiom2$ = axiom$
axiom3$ = ""
ca$
= MID$(axiom2$
, i
, 1) nca$ = ca$
nca$
= RTRIM$(rule
(j
).replace
) axiom3$ = axiom3$ + nca$
axiom2$ = axiom3$
axiom3$ = ""
count = count + 1
generateGrammer$ = axiom2$