Turtle Graphics

Post Reply
User avatar
rbytes
Posts: 1338
Joined: Sun May 31, 2015 12:11 am
My devices: iPhone 11 Pro Max
iPad Pro 11
MacBook
Dell Inspiron laptop
CHUWI Plus 10 convertible Windows/Android tablet
Location: Calgary, Canada
Flag: Canada
Contact:

Turtle Graphics

Post 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)
<



Attachments
Screenshot (75).png
Screenshot (75).png (595.1 KiB) Viewed 5042 times
Screenshot (74).png
Screenshot (74).png (1.05 MiB) Viewed 5042 times
Screenshot (73).png
Screenshot (73).png (854.44 KiB) Viewed 5042 times
Screenshot (72).png
Screenshot (72).png (1.87 MiB) Viewed 5042 times
The only thing that gets me down is gravity...

Post Reply