Page 1 of 1

Turtle Graphics

Posted: Wed Sep 27, 2017 6:35 pm
by rbytes
The very familiar drawing language designed for children. Yet with the right code, one can draw amazing geometric shapes!

Code: Select all

' Color Turtle Graphics v1.0 by rbytes
' September 2017
' inspired by Turtle Graphics Library by Gordon

turtlegraphics()=
    #.angle(#.degrees)
    #.scrclear(0,0,0)
    home()
    pendown()
.

home()=
    x = #.scrwidth()/2
    y = #.scrheight()/2
    heading = 0
.

penup()=
    status = 0
.

pendown()=
    penup.status = 1
.

left(angle)=
    home.heading = (home.heading+angle)%360
    ? home.heading < 0, home.heading += 360
.

right(angle)=
    left(0-angle)
.

forward(distance)=
    newx = home.x-distance*#.sin(home.heading)
    newy = home.y-distance*#.cos(home.heading)
    ? penup.status = 1
       #.drawline(home.x,home.y,newx,newy)
    .
    home.x = newx
    home.y = newy
.

back(distance)=
    forward(0-distance)
.

' ----- turtle shape definitions start here -----

poly(sides,length,percent,turnangle,speed,curve)=
  > x, 1..sides*percent/100     
    forward(length)
    ? curve = 1
         left((percent/100)*360*(turntangle/100)/sides)
    !
         right((percent/100)*360*(turnangle/100)/sides)
    .
    #.delay(speed)
  <
.

star(points,radius)=
    > x, 1..points
        forward(radius)
        back(radius)
        right(360/points)
    <
.

starpoint(baseside, height, spacer, fillflag, speed, orient)=
  a = baseside/2
  b = height
  c = #.sqrt(a^2 + b^2)
  'turnangle = asin(a/c).   asin not working!!!!
  turnangle = 4
  ' but needs to be doubled to account for the 2 R-A triangles
  angle1 = 180 - turnangle * 2
  angle2 = (360 - angle1)/2
  penup()
  forward(a)   'To account for the two back-to back r-a triangles
  #.delay(speed) 
  left(angle2) 'The final angle we drew in the r-a triangle script
  pendown()
  forward(c)
  #.delay(speed)
  left(angle1) 'Visualize this triangle having two equal sides the length of c
  forward(C)   'Like the hypotenuse, with a mirror image
  #.delay(speed)
  left(angle2)
  penup()
  forward(a/2)
  forward(orient*a) '-1 to rotate left, 1 to rotate right
.

' ******** turtle actions start here. **********

'draw star shapes using isosceles starpoints (base of triangle not drawn)
turtlegraphics()
> z, 1..10
#.scrclear(0,0,0)
a = #.rnd(10)
b = #.rnd(200)
c = #.rnd(20)+5
d = 10-#.rnd(10)
e = 60-#.rnd(50)
f = #.rnd(360)
g = #.rnd(3)+ 1
rotate = #.rnd(20) + 2
> t, 0..360, rotate
    #.drawcolor(#.hsv2rgb(t,1,1):3)
starpoint(a,b,c,0,.003,-5)
left(rotate)
<
#.delay(.2)
home()
<

'horn of plenty - draw polys with increasing number of sides
turtlegraphics()
#.scrclear(0,0,0)
#.drawsize(1)
> x, 3..180
    #.drawcolor(#.hsv2rgb((x-2)*6,1,1):3)
    poly(x,40,100,100,.0001,0)
    right(2)
<
#.delay(1)

'draw starburst with curved lines
turtlegraphics()
penup()
#.drawsize(1)
pendown()
turn = 2
> y, -1..1, 2
  > x, 1..180
    #.drawcolor(#.hsv2rgb(x*2,1,1):3)
    right(turn)
    poly(8,70,50,y*50,.002,0)
    home()
    turn += 2
  <
<
#.delay(1)

'draw starburst with less-curved lines
turtlegraphics()
penup()
#.drawsize(1)
pendown()
turn = 2
> y, -1..1, 2
  > x, 1..180
    #.drawcolor(#.hsv2rgb(x*2,1,1):3)
    right(turn)
    poly(8,70,50,y*2,.002,0)
    home()
    turn += 2
  <
<
#.delay(1)

'fireworks
turtlegraphics()
penup()
#.drawsize(2)
forward(250)
left(90)
pendown()
> x, 3..60
    #.drawcolor(#.hsv2rgb((x-2)*6,1,1):3)
    star(x,40+x)
    forward(20)
    left(x/6)
<
#.delay(1)
penup()
home()
back(400)
left(90)
forward(200)
right(90)
pendown()
> x, 3..60
    #.drawcolor(#.hsv2rgb((x-2)*6,1,1):3)
    Star(x,40+x)
    forward(20)
    right(x/10)
<
#.delay(2)

'draw a spiral
turtlegraphics()
#.drawsize(2)
> x, 3..120
    #.drawcolor(#.hsv2rgb((x-2)*6,1,1):3)
    poly(x/3,40,50,95,.005,0)
    right(1)
<