Author Topic: Turtle Essence  (Read 163 times)

B+

  • Sr. Member
  • ****
  • Posts: 419
    • View Profile
Turtle Essence
« on: 13. June 2017, 19:35:39 »
Code: [Select]
' turtle essence.bas SmallBASIC 0.12.9 (B+=MGA) 2017-06-13

' From my experience with Draw Strings and Nano series,
' I think I have discovered the essence of Turtle Drawing
' I contend that all can be done with 5 global variables
' x, y the start position of ray = ra line
' a, r the radian angle of direction to go and r the distance
' p is pen color, draw if 0 <= p <= 15 (QB colors 0 - 15)
' and one graphics command ra (as sub or gosub)

sub ra
  local x1 = x + r * cos(a)
  local y1 = y + r * sin(a)
  ' if pen p has a color draw, else just update x, y position
  if 0 <= p and p <= 15 then line x, y, x1, y1, p
  x = x1 : y = y1 'update x, y with current turtle position
end
'===== that's all the setup you need to do turle drawing


'but let's setup a Home position in middle of screen
homeX = xmax/2 : homeY = ymax/2


' lines commented out are from Galileo's tree code found at
' http://retrogamecoding.org/board/index.php?topic=581.0

'this code modifies Galileo's allowing tree to grow and sway with beautiful background!

'import turtle3
'clear screen
'startTurtle()
'home() ' oh this sets start angle at 270 degrees or 3*pi/2
x = homex: y =homey
a = 3*pi/2

'goy(200)
y = y + 200

'pen(1)
'color 0, 255, 0
p = 6
horizon = ymax/2 + 100
for i = 1 to 300
  cls
  for h= 0 to horizon
    midInk 0,0,128, 40, 200, 255, h/horizon
    line 0, h, xmax, h
  next
  for h = horizon to ymax
    midInk 40,200,255, 50, 200, 50, (h-horizon)/(ymax-horizon)
    line 0, h, xmax, h
  next
  tree i
  showpage
next
while 1
  sway = sway + (rnd*2) -1
  if sway > 25 then sway = 25
  if sway < -25 then sway = -25
  cls
    for h= 0 to horizon
    midInk 0,0,128, 40, 200, 255, h/horizon
    line 0, h, xmax, h
  next
  for h = horizon to ymax
    midInk 40,200,255, 50, 200, 50, (h-horizon)/(ymax-horizon)
    line 0, h, xmax, h
  next
  a = a + rad(sway)
  tree i
  a = a - rad(sway)
  showpage
wend
pause

sub tree(size)
   if size < 5 then
     'move(size)
     r = size
     ra
     
     'move(-size)
     r = -size
     ra
     
     'return
     exit sub
   end if
   'move(size/3)
   r = size/3
   ra
   
   'turn(-30)
   a = a - rad(30 + sway)
   
   tree(size*2/3)
   
   'turn(30)
   a = a + rad(30 + sway)
   
   'move(size/6)
   r = size/6
   ra
   
   'turn(25)
   a = a + rad(25 + sway)
   
   tree(size/2)
   
   'turn(-25)
   a = a - rad(25 + sway)
   
   'move(size/3)
   r = size/3
   ra
   
   'turn(25)
   a = a + rad(25 + sway)
   
   tree(size/2)
   
   'turn(-25)
   a = a - rad(25 + sway)
   
   'move(size/6)
   r = size/6
   ra
   
   'move(-size)
   r = -size
   ra
end sub

sub midInk(r1, g1, b1, r2, g2, b2, fr)
  color rgb(r1+(r2-r1)*fr, g1+(g2-g1)*fr, b1+(b2-b1)*fr)
end


B+

  • Sr. Member
  • ****
  • Posts: 419
    • View Profile
Re: Turtle Essence
« Reply #1 on: 18. June 2017, 06:36:54 »
This doesn't add to the lively discussion we are having here but it just occurred to me today I could do this with that code:
Code: [Select]
' turtle essence 2.bas SmallBASIC 0.12.9 (B+=MGA) 2017-06-17

' http://retrogamecoding.org/board/index.php?topic=581.0
'this code modifies Galileo's allowing tree to grow and sway with beautiful background!
' and this code mod the mod adds more trees to the picture and shrinks screen
xxmax = 800
yymax = 600
horizon = yymax/2 + 50
dim t(10)
for i = 0 to 10
  t(i).x = 50 + rnd * (xxmax - 100)
  t(i).y = horizon + 30 + rnd * 50
  t(i).s = .2 * t(i).y
next

a = 3*pi/2
p = 6

for i = 1 to yymax * .5 step 5
  background
  x = .36 * xxmax : y = .85 * yymax
  tree i
  showpage
next
while 1
  sway = sway + (rnd*2) - 1
  if sway > 20 then sway = 20
  if sway < -20 then sway = -20
  background
  a = a + rad(sway)
  tree i
  a = a - rad(sway)
  showpage
wend
pause

sub ra
  local x1 = x + r * cos(a)
  local y1 = y + r * sin(a)
  ' if pen p has a color draw, else just update x, y position
  if 0 <= p and p <= 15 then line x, y, x1, y1, p
  x = x1 : y = y1 'update x, y with current turtle position
end

sub tree(size)
   if size < 5 then
     r = size : ra : r = -size : ra
     exit sub
   end if
   r = size/3 : ra
   a = a - rad(30 + sway)
   tree(size*2/3)
   a = a + rad(30 + sway)
   r = size/6 : ra
   a = a + rad(25 + sway)
   tree(size/2)
   a = a - rad(25 + sway)
   r = size/3 : ra
   a = a + rad(25 + sway)
   tree(size/2)
   a = a - rad(25 + sway)
   r = size/6 : ra
   r = -size : ra
end sub

sub midInk(r1, g1, b1, r2, g2, b2, fr)
  color rgb(r1+(r2-r1)*fr, g1+(g2-g1)*fr, b1+(b2-b1)*fr)
end

sub background()
  local i, h, x, y, s
  cls
  for h= 0 to horizon
    midInk 0,0,128, 40, 200, 255, h/horizon
    line 0, h, xxmax, h
  next
  for h = horizon to yymax
    midInk 40,200,255, 50, 130, 50, (h-horizon)/(yymax-horizon)
    line 0, h, xxmax, h
  next
  for i = 0 to 10
    x = t(i).x
    y = t(i).y
    s = t(i).s
    tree s
  next
end

Curious why the main trunk of the background trees are staying straight while all their branches sway like on the main tree?
Append: Oh, I see why! Wait... then why are the background tree branches swaying at all? hmm... very curious!

(Shrunk the screen just so screenshots would fit here.)
« Last Edit: 18. June 2017, 06:47:45 by B+ »