Sub TurnTurtle(Angle) If Angle > 0 Then Turtle Turn Right Angle Else If Angle < 0 Then Turtle Turn Left Abs(Angle) End If End Sub
Sasquatch Guru Joined: 08/05/2020 Location: United StatesPosts: 377
Posted: 03:30am 03 Aug 2020
Recursive Fractal Trees!
' Recursive Fractal Trees
Mode 1,8
Turtle Reset Turtle Pen Up Turtle Move 400,600 Turtle Pen Down
Tree(120)
End
Sub Tree(Length) If Length < 1 Then Exit Sub
If Length > 20 Then Turtle Pen Colour RGB(139,69,19) Else Turtle Pen Colour RGB(34,139,34) EndIf
Turtle Pen Down Turtle Forward Length Turtle Turn Right 20 Tree(Length - 15) Turtle Turn Left 40 Tree(Length - 15) Turtle Pen UP Turtle Turn Right 20 Turtle Backward Length
End Sub
Sasquatch Guru Joined: 08/05/2020 Location: United StatesPosts: 377
Posted: 03:32am 03 Aug 2020
These Trees include a small amount of randomization making the trees look less uniform and more natural.
' Random Recursive Fractal Trees
Mode 1,8
Turtle Reset Turtle Pen Up Turtle Move 400,600 Turtle Pen Down
For X = 1 to 5 Tree(100) Pause(1000) Next X
End
Sub Tree(Length) If Length < 1 Then Exit Sub
Local RndLength = Length + Rnd * 5 Local RndAngle = 15 + Int(Rnd * 20.0)
If RndLength > 20 Then Turtle Pen Colour RGB(139,69,19) Else Turtle Pen Colour RGB(34,139,34) EndIf
Turtle Pen Down Turtle Forward RndLength Turtle Turn Right RndAngle Tree(RndLength - 15) Turtle Turn Left RndAngle * 2 Tree(RndLength - 15) Turtle Pen UP Turtle Turn Right RndAngle Turtle Backward RndLength
End Sub
Sasquatch Guru Joined: 08/05/2020 Location: United StatesPosts: 377
Posted: 03:47pm 03 Aug 2020
Sierpinski's Triangle Recursive Fractal. If you un-comment the pause statement, you will see that the entire thing is drawn as a series of tiny triangles.
' Sierpinski's Triangle Recursive Fractal
Mode 1,8
Colour 0,RGB(255,255,255)
Turtle Reset Turtle Pen Up Turtle Move 70,575 Turtle Turn Right 90 Turtle Pen Down
Turtle Pen Colour 0
Sierpinski(650,7)
End
Sub Sierpinski(Length,Level)
If Level = 0 Then For i = 1 to 3 Turtle Forward Length Turtle Turn Left 120 ' Pause(50) Next i Exit Sub EndIf
Sierpinski(Length / 2.0, Level - 1) Turtle Pen Up Turtle Forward Length / 2.0 Turtle Pen Down
Sierpinski(Length / 2.0, Level - 1) Turtle Pen Up Turtle Turn Left 120 Turtle Forward Length / 2.0 Turtle Turn Right 120 Turtle Pen Down
Sierpinski(Length / 2.0, Level - 1) Turtle Pen Up Turtle Turn Left 60 Turtle Backward Length / 2.0 Turtle Turn Right 60 Turtle Pen Down
End Sub
matherp Guru Joined: 11/12/2012 Location: United KingdomPosts: 10170
Posted: 05:35pm 03 Aug 2020
Sasquatch Guru Joined: 08/05/2020 Location: United StatesPosts: 377
Posted: 05:50pm 03 Aug 2020
The Nautilus
' Square Nautilus Turtle Graphics Demo
Mode 1,8
Size = 275 N = 100 Angle = 10 Ratio = 0.97
Turtle Reset
For i = 1 to N
'Draw a square For j = 1 to 4 Turtle Forward Size Turtle Turn Left 90 Next j
'Rinse and Repeat Turtle Turn Left Angle Size = Size * Ratio
Next i
End
Sasquatch Guru Joined: 08/05/2020 Location: United StatesPosts: 377
Posted: 07:03pm 03 Aug 2020
Here is a recursive fractal pine tree:
' Recursive Fractal Pine Trees
Mode 1,8
Turtle Reset Turtle Pen Up Turtle Move 400,500 Turtle Pen Down
PineTree(100,20)
End
Sub PineTree(Length,Depth) ' Print Length If Depth <= 0 Then Exit Sub
If Length > 2 Then Turtle Pen Colour RGB(139,69,19) 'Make the sticks brown Else Turtle Pen Colour RGB(0,100,0) 'Make the needles Green EndIf
Turtle Forward Length PineTree(Length * 0.8, Depth - 1) Turtle Turn Right 120 PineTree(Length * 0.5, Depth - 3) Turtle Turn Right 120 PineTree(Length * 0.5, Depth - 3) Turtle Turn Right 120 Turtle Pen Up Turtle Backward Length Turtle Pen Down
End Sub
The second version includes a little randomization to make it less uniform
' Random Recursive Fractal Pine Trees
Mode 1,8
Turtle Reset Turtle Pen Up Turtle Move 400,500 Turtle Pen Down
PineTree(100,19)
End
Sub PineTree(Length,Depth) If Depth <= 0 Then Exit Sub
Local Angle = 110 + 20 * Rnd()
If Length > 5 Then Turtle Pen Colour RGB(139,69,19) 'Make the sticks brown Else Turtle Pen Colour RGB(0,100,0) 'Make the needles Green EndIf
Turtle Forward Length PineTree(Length * 0.8, Depth - 1) Turtle Turn Right Angle PineTree(Length * 0.5, Depth - 3) Turtle Turn Right 120 PineTree(Length * 0.5, Depth - 3) Turtle Turn Right 240 - Angle Turtle Pen Up Turtle Backward Length Turtle Pen Down
End Sub
TassyJim Guru Joined: 07/08/2011 Location: AustraliaPosts: 6257
Posted: 10:39pm 03 Aug 2020
Thank you for all the turtle demos. It brings back memories of my brother demonstrating turtle drawing 50 years ago. It was a new thing back then.
My brother went on to be a much better programmer than I will ever be.
Jim
Sasquatch Guru Joined: 08/05/2020 Location: United StatesPosts: 377
Posted: 11:29pm 03 Aug 2020
Ok, this one is basically blowing my poor little mind.
While it doesn't use Turtle Graphics it is a very interesting fractal known as the Barnsley Fern. I may "need" to do this one as a CSUB to see how fast it will fill. It would be interesting to zoom in if it would fill in faster.
'Barnsley's Fern
Mode 1,8
CLS
G = RGB(0,100,0)
For i = 1 to 1000000
Select Case Rnd() Case IS < .01 NextX = 0 NextY = 0.16 * Y Case .01 TO .08 NextX = .2 * X - .26 * Y NextY = .23 * X + .22 * Y + 1.6 Case .08 TO .15 NextX = -.15 * X + .28 * Y NextY = .26 * X + .24 * Y + .44 Case Else NextX = .85 * X + .04 * Y NextY = -.04 * X + .85 * Y + 1.6 End Select
X = NextX Y = NextY
Pixel X * 100 + 400,600 - Y * 55 ,G
Next i
vegipete Guru Joined: 29/01/2013 Location: CanadaPosts: 1126
Posted: 11:51pm 03 Aug 2020
Turtle meets Dragon
This is perhaps my favorite fractal line drawing. Especially neat is the way they fit together. In this code, the red and green dragons are identical, just rotated 180 degrees. And they fit together perfectly.
It does look though that rounding errors are creeping into play here. The start point is fixed but the end point should also always end up the same. But if you watch closely between different orders, particularly the higher ones, the end point moves quite a lot. As a result, the green and red dragons must be the same order or they won't line up at all.
Orders higher than 19 don't work because the draw distance becomes less than 1 so nothing beyond a single dot gets drawn.
cls turtle reset text 100,500,"Dragon Curve - a Fractal","CT" do print @(0,520) "Order: (1-19)" input "0 to quit: "; dord loop until dord >= 0 and dord < 20
do if dord = 0 then end
dord = dord - 1 dist = MM.HRES/2/(sqr(2)^dord)
cls turtle reset text 80, 0,"Dragon Curve","CT",2 text 80,20,"Order:" + str$(dord+1), "CT",2 turtle pen up ' no line yet turtle move MM.HRES * .25, MM.VRES * .55 turtle pen down
do print @(0,520) "Order: (1-19)" input "0 to quit: "; dord loop until dord >= 0 and dord < 20
loop
sub DrawDragon(ord, sig) if ord = 0 then turtle forward dist else DrawDragon(ord-1, 1) turtle turn right 90 * sig DrawDragon(ord-1, -1) endif end sub
Sasquatch Guru Joined: 08/05/2020 Location: United StatesPosts: 377
Posted: 12:12am 04 Aug 2020
That is a nice one! At lower orders it doesn't give any clue as to it's potential. It amazes me how such a simple looking algorithm can generate something so complex.
Thanks for sharing, if you have any other gems I'd like to see them.
capsikin Guru Joined: 30/06/2020 Location: AustraliaPosts: 341
Posted: 12:46am 04 Aug 2020
Cool thread, there's some other recursive line fractals I'd like to try if I get time (I think I was inspired by one called the koch curve and I changed things around and got a new one with a kind of hexagonal pattern)
capsikin Guru Joined: 30/06/2020 Location: AustraliaPosts: 341
Posted: 01:32am 04 Aug 2020
Cool thread, there's some other recursive line fractals I'd like to try if I get time (I think I was inspired by one called the koch curve and I changed things around and got a new one with a kind of hexagonal pattern).
Update: I gave it a quick try by modifying the Sierpinski program above. The rounding errors were already spoiling it before I got as much detail as I wanted. Here's the code I used:
' Hex Gasket Recursive Fractal
Mode 1,8
Colour 0,RGB(255,255,255)
Turtle Reset Turtle Pen Up Turtle Move 70,575 Turtle Turn Right 90 Turtle Pen Down
Turtle Pen Colour 0
'Gasket(650,7)
Gasket(650,5)
End
Sub Gasket(Length,Level)
If Level = 0 Then Turtle Forward Length ' Pause(50) Exit Sub EndIf
Turtle Pen Up Turtle Forward Length Turtle Pen Down
Turtle Turn Left 120
Gasket(Length / 2.0, Level - 1)
Turtle Turn Left 60
Gasket(Length / 2.0, Level - 1)
Turtle Turn Left 60
Gasket(Length / 2.0, Level - 1)
Turtle Turn Left 120
Turtle Pen Up Turtle Forward Length Turtle Pen Down
End Sub
I changed the program to exactly retrace the same lines when going backwards, rather than taking a shortcut. It's more complicated, but it cancels out the rounding errors:
' Hex Gasket Recursive Fractal
Mode 1,8
Colour 0,RGB(255,255,255)
Turtle Reset Turtle Pen Up Turtle Move 70,575 Turtle Turn Right 90 Turtle Pen Down
Turtle Pen Colour 0
'Gasket(650,7)
Gasket(650,10)
End
Sub Gasket(Length,Level)
If Level = 0 Then Turtle Forward Length
Turtle Pen Up Turtle Backward Length Turtle Pen Down
' Pause(50) Exit Sub EndIf
Turtle Turn Left 60
Turtle Pen Up Turtle Forward Length/2 Turtle Pen Down Turtle Turn Right 180 Gasket(Length / 2.0, Level - 1) Turtle Turn Left 180
Turtle Turn Right 60
Turtle Pen Up Turtle Forward Length/2 Turtle Pen Down Turtle Turn Right 180 Gasket(Length / 2.0, Level - 1) Turtle Turn Left 180
Turtle Turn Right 60
Turtle Pen Up Turtle Forward Length/2 Turtle Pen Down Turtle Turn Right 180 Gasket(Length / 2.0, Level - 1) Turtle Turn Left 180
Turtle Pen Up
Turtle Turn Right 180 Turtle Forward Length/2
Turtle Turn Left 60 Turtle Forward Length/2
Turtle Turn Left 60 Turtle Forward Length/2
Turtle Turn Right 60 Turtle Turn Right 180
Turtle Pen Down
End Sub
Sasquatch Guru Joined: 08/05/2020 Location: United StatesPosts: 377
Posted: 03:53am 04 Aug 2020
Good Job! MMBasic does make it easy to try changes quickly. I've added this one to my collection as well.
JohnS Guru Joined: 18/11/2011 Location: United KingdomPosts: 4026
Posted: 08:21am 04 Aug 2020
Just in case anyone doesn't know, each dragon is the same as folding a piece of paper in half repeatedly (you can't do it much as it gets too thick) then opening it out so every crease is a right angle and viewing from an edge side.
Origami...
John
Atomizer_Zero Senior Member Joined: 04/07/2020 Location: United KingdomPosts: 134
Posted: 04:38pm 04 Aug 2020
Wow, these are awesome. I love seeing these patterns drawing and stuff. Before I got my CMM2, I played around in QBASIC 1 in DOS, and made this
https://www.youtube.com/watch?v=zJe2Ajp2eVg
Keep em coming!
bigmik Guru Joined: 20/06/2011 Location: AustraliaPosts: 2947
Posted: 06:57am 05 Aug 2020
Turtle meets Dragon
Hi Pete,
That code fails for anything after Option 3.. I think it is to do with negative angles.
Maybe something changed in a later version of MMBasic, I am using 5.05.04
There is also an error in the Barnsley Fern by sasqatch.
Kind Regards
Mick
TassyJim Guru Joined: 07/08/2011 Location: AustraliaPosts: 6257
Posted: 07:11am 05 Aug 2020
Maybe something changed in a later version of MMBasic, I am using 5.05.04
Mick
Yep. All fixed in the latest beta.
Jim
bigmik Guru Joined: 20/06/2011 Location: AustraliaPosts: 2947
Posted: 10:02am 05 Aug 2020
Thanks Jim,
I thought it was firmware differences, I will try the latest beta..