L-system fractals!

Miscellaneous Forums/Blitz Showcase/L-system fractals!

The forum is locked. These are the original blitzbasic.com forums, kept so they can still be read. No new topics, replies or edits can be made here. To join in, head over to the Blitz3D+ forums.

Hi, I just thought I'd share with you some Lindenmayer system fractals a made a month back. The code is sloppy and probably uncommented, but you can make some pretty neat stuff with it.

I only made 2: a (fake) Serpinski triangle, and a Dragon's curve.

Find them here and here

And for the source...

Fractal1.exe:
Graphics3D 1024, 768, 32, 2


Const Rule$ = "F+F-F-F+F"
Const Angle# = 120
Global Length# = 10
Global a# = 0
Global iterations# = 0

Global LString$ = "F"
DrawFractal()
Repeat

	If KeyHit(57) Then KochIt()
	If MouseHit(2) Then Stop 
	
Until KeyHit(1)

End


Function DrawFractal()
	Cls
	RenderWorld()
	Local x1#, y1#, x2#, y2#
	x1 = 0 : y1 = 768
	tmpstr$ = LString$
	
	;Scale the triangle by iteration count
	Length = -7*iterations + 54
	
	For i = 1 To Len(tmpstr)
		If Mid$(tmpstr, i, 1) = "F" Then 
			x2# = x1# + Length# * Cos(a)
			y2# = y1# + Length# * Sin(a)
			Line x1, y1, x2, y2
			x1 = x2 : y1 = y2
		EndIf
		If Mid$(tmpstr, i, 1) = "+" Then 
			a = a - Angle
		EndIf
		If Mid$(tmpstr, i, 1) = "-" Then 
			a = a + Angle
		EndIf
	Next
	
	Flip()
End Function 


Function KochIt()
	tmpstr$ = LString$
	Local x1, y1, x2, y2, a#
	x2 = 0 : y2 = 384
	LString$ = ""
	For i = 1 To Len(tmpstr)
		tmpchr$ = Mid$(tmpstr, i, 1)
		If tmpchr$ = "F" Then 
			LString = LString + Rule$
		ElseIf tmpchr$ = "+"
			LString = LString + "+"
		ElseIf tmpchr$ = "-"
			LString = LString + "-"
		EndIf
	Next
	iterations = iterations+1
	DrawFractal()
End Function


And Fractal2.exe:

Graphics3D 1024, 768, 32, 2


Const Rule$ = "X+YF+"
Const Rule2$ = "-FX-Y"
Const Angle# = 90
Global Length# = 10
Global a# = 0
Global iterations# = 0
Global playerscale# = 1
Global LString$ = "FX"
DrawFractal()
Repeat

	If KeyHit(57) Then KochIt()
	If MouseHit(2) Then Stop 
	If KeyHit(200) Then ScUp()
	If KeyHit(208) Then ScDown()
	
Until KeyHit(1)

End


Function DrawFractal()
	Cls
	RenderWorld()
	Local x1#, y1#, x2#, y2#
	x1 = 640 : y1 = 525
	tmpstr$ = LString$
	

	;Scale it by iteration count
	Length = 100-(iterations*8)*playerscale
	If length =< 0 Then length = 1
	
	For i = 1 To Len(tmpstr)
		If Mid$(tmpstr, i, 1) = "F" Then 
			;COLORS!!!!!!
			c# = (255*i)/Len(tmpstr)
			Color c, -c, c
			


			x2# = x1# + Length# * Cos(a)
			y2# = y1# + Length# * Sin(a)
			Line x1, y1, x2, y2
			x1 = x2 : y1 = y2
		EndIf
		If Mid$(tmpstr, i, 1) = "+" Then 
			a = a - Angle
		EndIf
		If Mid$(tmpstr, i, 1) = "-" Then 
			a = a + Angle
		EndIf
	Next
	
	Text 0, 0, "Iteration " + iterations
	Text 0, 24, "Zoom = " + playerscale
	Flip()
End Function 


Function ScUp()
	playerscale = playerscale - 0.05
	DrawFractal()
End Function

Function ScDown()
	playerscale = playerscale + 0.05
	DrawFractal()
End Function


Function KochIt()
	tmpstr$ = LString$
	LString$ = ""
	For i = 1 To Len(tmpstr)
		tmpchr$ = Mid$(tmpstr, i, 1)
		If tmpchr$ = "X" Then 
			LString = LString + Rule$
		ElseIf tmpchr$ = "Y"
			LString = LString + Rule2$
		ElseIf tmpchr$ = "+"
			LString = LString + "+"
		ElseIf tmpchr$ = "-"
			LString = LString + "-"
		EndIf
	Next
	iterations = iterations+1
	DrawFractal()
End Function


Anyone who wants to modify this so it's more flexible is more than welcome! Try changing the angle values, you get some weird results. Check out the dragon (fractal2) at 75 degrees