## Turtle Graphics

rbytes
Posts: 1963
Joined: Sun May 31, 2015 12:11 am
My devices: iPhone X
MacBook
Dell Inspiron laptop
CHUWI Plus 10 convertible Windows/Android tablet
Flag:
Contact:

### Turtle Graphics

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
.

penup()=
status = 0
.

pendown()=
penup.status = 1
.

left(angle)=
.

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

forward(distance)=
? 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)
<
.

> x, 1..points
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 (595.1 KiB) Viewed 1032 times
Screenshot (74).png (1.05 MiB) Viewed 1032 times
Screenshot (73).png (854.44 KiB) Viewed 1032 times
Screenshot (72).png (1.87 MiB) Viewed 1032 times
Zzzzz