Post by GuppyB on Aug 16, 2004 14:19:56 GMT -5
After the Mandlebrot program, started working on this but recently got sidetracked with "real" work. It does basic l-system string rewriting, the initiator and generator code that are provided in the code will produce a 2D "grown bush". You can change the initiator, generator and angles and the code will produce Koch islands and other fractal beauties.
I was going to take it to the next level and do node rewriting as well as adding interpretation of 3d turtle graphic commands, but got sidetracked on another project, so feel free to improve, complete and etc... the code below.
There is some great documentation for those interested in a pdf on l-systems and fractal biological modelling at:
algorithmicbotany.org/documents/
Enjoy...G
I was going to take it to the next level and do node rewriting as well as adding interpretation of 3d turtle graphic commands, but got sidetracked on another project, so feel free to improve, complete and etc... the code below.
There is some great documentation for those interested in a pdf on l-systems and fractal biological modelling at:
algorithmicbotany.org/documents/
Enjoy...G
dim n 'number of iterations
dim d 'distance
dim rules
dim deltaAngle 'angle of change
dim initiator$ 'starting sequence
dim i,j,k,l,m 'loop variable
dim x,y,angle 'position and angle onscreen for turtle graphics
dim w,h 'widht and height of screen
dim turtle$ 'command to turtle
dim BranchLevel,lastBranch 'level of stack
w = 1024 : h = 768 'screen size
d = 200 'distance
n = 4 'set iterations
rules = 1
dim gen$(rules),rule$(rules), generator$(rules),replaced
dim stackX(n+5), stackY(n+5), stackAngle(n+5)
dim oldstring$(n*50), newstring$(n*50),ns 'generated string
deltaAngle = 22 'set angle
initiator$ = "F"
generator$(0) = "F->FF-[-F+F+F]+[+F-F-F]"
w = 1024: h = 768 'set to width and height of your screen
x = 512: y = 10 : angle = 90
glMatrixMode (GL_PROJECTION)
glLoadIdentity ()
glOrtho (0, w, 0, h, -1, 1)
glMatrixMode (GL_MODELVIEW)
glDisable (GL_DEPTH_TEST)
glClear (GL_COLOR_BUFFER_BIT)
gosub GenerateString
j = 0
while newstring$(j) <> ""
For i = 1 to len(newstring$(j))
turtle$ = mid$(newstring$(j),i,1)
' below are the turtle commands
if turtle$ = "F" then gosub DrawForward : endif
if turtle$ = "f" then gosub MoveForward : endif
if turtle$ = "-" then gosub TurnRight : endif
if turtle$ = "+" then gosub TurnLeft : endif
if turtle$ = "[" then gosub PushtoStack : endif
if turtle$ = "]" then gosub PullfromStack : endif
SwapBuffers ()
next
j = j + 1
wend
while TRUE
wend
'*******************Draw Forward ************************************
DrawForward:
glDrawBuffer(gl_front_and_back)
GLBegin (GL_lines)
glColor3f(0.4,1.0,0.2)
glVertex2f(x,y)
x = x + (d * cosd(angle)) : y = y + (d * sind(angle))
glVertex2f(x,y)
glEnd()
return
'*******************Move Forward no Draw************************************
MoveForward:
x = x + (d * cosd(angle)) : y = y + (d * sind(angle))
return
' **************************** Turn Right ****************************
TurnRight:
angle = angle - deltaAngle
return
' ****************************** Turn Left ***************************
TurnLeft:
angle = angle + deltaAngle
return
' *************************** Push to Stack ***************************
PushtoStack:
if lastBranch = 1 then Branchlevel = Branchlevel + 1 : endif
stackX(Branchlevel) = x : stackY(Branchlevel) = y: stackAngle(Branchlevel) = Angle
lastBranch = 1
return
' *************************** Pull from Stack **************************
PullfromStack:
if lastBranch = 0 then Branchlevel = Branchlevel - 1 : endif
x = stackX(Branchlevel) : y = stackY(Branchlevel) : Angle = stackAngle(Branchlevel)
lastBranch = 0
return
'***************************** L-System Re-writing done below ************************
GenerateString:
newstring$(0) = initiator$
for k = 1 to rules
gen$(k-1) = left$(generator$(k-1),1): rule$(k-1) = right$(generator$(k-1),len(generator$(k-1)) - 3)
next
if n = 0 then return: endif
for i = 1 to n
for k = 0 to (n*50) : oldstring$(k) = newstring$(k): newstring$(k) = "": next
j=1 : k = 0 : l = 0
while oldstring$(k)<>""
while j < len(oldstring$(k)) + 1
replaced = 0
for m = 1 to rules
if mid$(oldstring$(k),j,1) = gen$(m-1) then
newstring$(l) = newstring$(l) + rule$(m-1)
replaced = 1
endif
next
if replaced = 0 then newstring$(l) = newstring$(l) + mid$(oldstring$(k),j,1) : endif
replaced = 0
if len(newstring$(l)) > 240 then l = l + 1: endif
j = j + 1
wend
k=k+1 : j = 1
wend
d = d / 2
next
return