Home
JAQForum Ver 20.06
Log In or Join  
Active Topics
Local Time 07:57 24 Sep 2020 Privacy Policy
Jump to

Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.

Forum Index : Microcontroller and PC projects : CMM2 Turtle Graphics Demos

     Page 1 of 2    
Author Message
Sasquatch

Senior Member

Joined: 08/05/2020
Location: United States
Posts: 156
Posted: 09:25pm 02 Aug 2020
Copy link to clipboard 
Print this post

Here is a demo using turtle graphics, Each "slide" displays for 10 seconds.

'CMM2 Turtle Graphics Demo
'Adapted from various sources on the Internet
'by "Sasquatch"

Mode 1,8

Draw(400,45.5)
Pause(10000)

Draw (500,55.5)
Pause(10000)

Draw (600,60.2)
Pause(10000)

Draw (800,89.5)
Pause(10000)

Draw (900,110)
Pause(10000)

Draw (1000,119.9)
Pause(10000)

Draw (1000,120.1)
Pause(10000)

Draw (1000,135.1)
Pause(10000)

Draw (1000,145)
Pause(10000)

Draw (1000,176)
Pause(10000)

Draw (1000,190)
Pause(10000)

End


Sub Draw(Count,Angle)

 Turtle Reset
 N = 5
 A = 90

 For I = 1 To Count
   Turtle Forward N
   A = A + Angle
   Turtle Heading A mod 360
   Turtle Pen Colour Map(I mod 255)
   N = N + 0.5
 Next I

End Sub

-Carl
 
Sasquatch

Senior Member

Joined: 08/05/2020
Location: United States
Posts: 156
Posted: 09:26pm 02 Aug 2020
Copy link to clipboard 
Print this post

This one uses recursion instead of looping.

' Hilbert Curve using recursion

Mode 1,8

C = 1

Turtle Reset
Turtle Pen Up
Turtle Move 20,600
Turtle Pen Down

'Hilbert(Level,Angle,Length)
Hilbert(7,90,6)
Pause 10000

Turtle Reset
Turtle Pen Up
Turtle Move 150,550
Turtle Pen Down

Hilbert(8,90,2)
'Pause 10000

End


Sub Hilbert(Level,Angle,Length)
 If Level = 0 Then Exit Sub

 C = C + 0.1
 Turtle Pen Colour Map(C Mod 255)

 TurnTurtle(Angle)
 Hilbert(Level - 1,0 - Angle, Length)

 Turtle Forward Length
 TurnTurtle(0 - Angle)
 Hilbert(Level - 1,Angle,Length)

 Turtle Forward Length
 Hilbert(Level - 1,Angle,Length)

 TurnTurtle(0 - Angle)
 Turtle Forward Length
 Hilbert(Level - 1,0 - Angle,Length)

 TurnTurtle(Angle)

End Sub


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

-Carl
 
Sasquatch

Senior Member

Joined: 08/05/2020
Location: United States
Posts: 156
Posted: 03:30am 03 Aug 2020
Copy link to clipboard 
Print this post

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




-Carl
 
Sasquatch

Senior Member

Joined: 08/05/2020
Location: United States
Posts: 156
Posted: 03:32am 03 Aug 2020
Copy link to clipboard 
Print this post

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


-Carl
 
Sasquatch

Senior Member

Joined: 08/05/2020
Location: United States
Posts: 156
Posted: 03:47pm 03 Aug 2020
Copy link to clipboard 
Print this post

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




-Carl
 
matherp
Guru

Joined: 11/12/2012
Location: United Kingdom
Posts: 3869
Posted: 05:35pm 03 Aug 2020
Copy link to clipboard 
Print this post

   
 
Sasquatch

Senior Member

Joined: 08/05/2020
Location: United States
Posts: 156
Posted: 05:50pm 03 Aug 2020
Copy link to clipboard 
Print this post

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


-Carl
 
Sasquatch

Senior Member

Joined: 08/05/2020
Location: United States
Posts: 156
Posted: 07:03pm 03 Aug 2020
Copy link to clipboard 
Print this post

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


-Carl
 
TassyJim

Guru

Joined: 07/08/2011
Location: Australia
Posts: 3709
Posted: 10:39pm 03 Aug 2020
Copy link to clipboard 
Print this post

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
It all started with the ZX81....
VK7JH
http://www.c-com.com.au/MMedit.htm
 
Sasquatch

Senior Member

Joined: 08/05/2020
Location: United States
Posts: 156
Posted: 11:29pm 03 Aug 2020
Copy link to clipboard 
Print this post

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

-Carl
 
vegipete

Guru

Joined: 29/01/2013
Location: Canada
Posts: 368
Posted: 11:51pm 03 Aug 2020
Copy link to clipboard 
Print this post

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

 turtle heading 90 - (dord MOD 8) * 45
 turtle pen colour rgb(red)
 DrawDragon(dord,1)

 turtle heading 270 - (dord MOD 8) * 45
 turtle pen colour rgb(green)
 DrawDragon(dord,1)

 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

Senior Member

Joined: 08/05/2020
Location: United States
Posts: 156
Posted: 12:12am 04 Aug 2020
Copy link to clipboard 
Print this post

  vegipete said  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.



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.
-Carl
 
capsikin
Guru

Joined: 30/06/2020
Location: Australia
Posts: 332
Posted: 12:46am 04 Aug 2020
Copy link to clipboard 
Print this post

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: Australia
Posts: 332
Posted: 01:32am 04 Aug 2020
Copy link to clipboard 
Print this post

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

Senior Member

Joined: 08/05/2020
Location: United States
Posts: 156
Posted: 03:53am 04 Aug 2020
Copy link to clipboard 
Print this post

  capsikin said  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)


Good Job!  MMBasic does make it easy to try changes quickly.  I've added this one to my collection as well.
-Carl
 
JohnS
Guru

Joined: 18/11/2011
Location: United Kingdom
Posts: 2151
Posted: 08:21am 04 Aug 2020
Copy link to clipboard 
Print this post

  Sasquatch said  
  vegipete said  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.



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


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 Kingdom
Posts: 134
Posted: 04:38pm 04 Aug 2020
Copy link to clipboard 
Print this post

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: Australia
Posts: 2368
Posted: 06:57am 05 Aug 2020
Copy link to clipboard 
Print this post

  vegipete said  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
Mick's uMite Stuff can be found >>> HERE (Kindly hosted by Dontronics) <<<
 
TassyJim

Guru

Joined: 07/08/2011
Location: Australia
Posts: 3709
Posted: 07:11am 05 Aug 2020
Copy link to clipboard 
Print this post

  bigmik said  
Maybe something changed in a later version of MMBasic, I am using 5.05.04

Mick

Yep. All fixed in the latest beta.

Jim
It all started with the ZX81....
VK7JH
http://www.c-com.com.au/MMedit.htm
 
bigmik

Guru

Joined: 20/06/2011
Location: Australia
Posts: 2368
Posted: 10:02am 05 Aug 2020
Copy link to clipboard 
Print this post

Thanks Jim,


I thought it was firmware differences, I will try the latest beta..

I usually stick to the latest release version

Regards,

Mik
Mick's uMite Stuff can be found >>> HERE (Kindly hosted by Dontronics) <<<
 
     Page 1 of 2    
Print this page
© JAQ Software 2020