Author Topic: Turtle graphic library  (Read 4720 times)

Galileo

  • Guest
Turtle graphic library
« on: May 31, 2017, 05:37:57 PM »
I have developed for Yabasic 2.78.0 a simplified library for the generation of turtle graphics. It is based on an earlier Tomaaz library.

Code: [Select]
REM Simplified turtle commands for Yabasic 2.78.0, by Galileo, 5/2017.
REM Adapted from Tomaaz's turtle library.

export sub startTurtle() // Initialize turtle environment.
open window 850, 700
backcolor 0, 0, 0
clear window
home()
end sub

export sub move(long) // If positive, forward. Negative, backward.
local a, b, x, y

a = sin (angle * pi / 180) * long
b = cos (angle * pi / 180) * long
x = posx + a
y = posy - b
if draw then
line posx, posy, x, y
end if
posx = x
posy = y
end sub

export sub turn(degrees) // If positive, turn right. Negative, turn left.
angle = angle + degrees
if angle > 360 angle = angle - 360
if angle < 0 angle = angle + 360
end sub

export sub pen(state) // If 0, no draw. Another, draw.
draw = state // Global variable for this module.
end sub

export sub goxy (x, y) // Jump to location. No draw.
posx = x
posy = y
end sub

export sub gox (steps) // Jump steps pixels in x coord. No draw. Positive or negative.
posx = posx + steps
end sub

export sub goy (steps) // Jump steps pixels in y coord. No draw. Positive or negative.
posy = posy + steps
end sub

export sub reset () // Set angle to 0 degrees.
angle = 0 // Global variable for this module.
end sub

export sub posx() // Return turtle x location.
return posx
end sub

export sub posy() // Return turtle y location.
return posy
end sub

export sub angle() // Return turtle orientation.
return angle
end sub

export sub home() // Establish start mode.
reset()
posy = peek("winheight") / 2 // Global variable for this module.
posx = peek("winwidth") / 2 // Global variable for this module.
pen(1)
end sub

export sub arc(ang, radius) // Moves the turtle along an arc.
local i, stp, sgn

if ang < 0 then sgn = -1 else sgn = 1 end if

stp = (2 * pi * radius) / 360

for i = 1 to abs(ang)
move(stp)
turn(sgn)
next i
end sub

Code example

Code: [Select]
import turtle3

clear screen

startTurtle()

home()
goy(200)
pen(1)
color 0, 255, 0
tree(300)

sub tree(size)
   if size < 5 then
move(size)
move(-size)
return
   end if
   move(size/3)
   turn(-30)
   tree(size*2/3)
   turn(30)
   move(size/6)
   turn(25)
   tree(size/2)
   turn(-25)
   move(size/3)
   turn(25)
   tree(size/2)
   turn(-25)
   move(size/6)
   move(-size)
end sub


Galileo

  • Guest
Re: Turtle graphic library
« Reply #1 on: May 31, 2017, 05:51:40 PM »
Adapted code from logo examples.

Code: [Select]
import turtle3

clear screen

startTurtle()

reset()
goxy(50,100)
rancolor()
square(40)

reset()
goxy(125,75)
rancolor()
rhombus()

reset()
goxy(225,100)
rancolor()
triang()

reset()
goxy(350,75)
rancolor()
windmill()

reset()
goxy(450,75)
rancolor()
tire()

reset()
goxy(550,75)
rancolor()
saw()

reset()
goxy(625,90)
rancolor()
polygon(6, 35)

reset()
goxy(750,75)
rancolor()
AWindow(50)

sub rancolor()
color ran(256), ran(256), ran(256)
end sub

sub rectang(k, m)
local i

for i = 1 to 2
move(k) : turn(90)
move(m) : turn(90)
next i
end sub


sub square(k)
rectang(k, k)
end sub


sub rhombus()
turn(45)
square(40)
turn(45)
end sub


sub triang()
local i

for i = 1 to 3
move(40)
turn(120)
next i
end sub


sub windmill()
local i

for i = 1 to 4
triang()
turn(90)
next i
end sub

// =================

sub tire()
llanta()
goma()
end sub

sub llanta()
local i

for i = 1 to 24
move(25) : move(-25) : turn(15)
next i
end sub

sub goma()
local i

pen(0)
for i = 1 to 360
move(25) : pen(1) : move(10)
pen(0) : move(-35) : turn(1)
next i
pen(1)
end sub

// ===================

sub diente()
move(40) : turn(-120) : move(20)
move(-20) : turn(120) : move(-40)
end sub

sub saw()
local i

for i=1 to 12
diente() : turn(-30)
next i
end sub

// ==================

sub polygon(n, k)
local i

for i = 1 to n
move(k) : turn(360/n)
next i
end sub


sub AWindow(k)
local i

for i=1 to 4
square(k/2) : turn(90)
next i
end sub

B+

  • Guest
Re: Turtle graphic library
« Reply #2 on: May 31, 2017, 05:58:39 PM »
Nice tree!

Galileo

  • Guest
Re: Turtle graphic library
« Reply #3 on: May 31, 2017, 06:09:37 PM »
More logo examples.

Code: [Select]
import turtle3

clear screen

startTurtle()

home()
gox(-350)
rancolor()
turbine(15)

home()
gox(-225)
rancolor()
fan(60, 7, 20)

home()
gox(-100)
turn(18)
rancolor()
star(5, 25)

home()
rancolor()
catch(20, 20)

home()
gox(150)
pen(1)
rancolor()
spiral(1, 3)

home()
gox(300)
pen(1)
asterix(32)


sub rancolor()
color ran(256), ran(256), ran(256)
end sub


sub rectang(k, m)
local i

for i = 1 to 2
move(k) : turn(90)
move(m) : turn(90)
next i
end sub


sub square(k)
rectang(k, k)
end sub

// =====================

sub turbine(k)
local i

for i = 1 to 18
move(2 * k) : square(k)
move(-2 * k) : turn(20)
next i
end sub


sub fan(k, m, p)
local i

turn(-p * 5)
for i=1 to p
rectang(k, m) : turn(10)
next i
turn(p * 5)
end sub


sub star(k, m)
local i

for i = 1 to k
move(m)
turn(720/k)
move(m)
turn(-(360/k))
next i
end sub


sub catch(k, m)
local i

for i = 1 to k
pen(1) : circle posx(), posy(), m
turn(720/k) : pen(0) : move(m/1.6)
next i
end sub


sub spiral(k, m)
if k < 1250 then
move(k/500)
turn(m)
spiral(k + m, m)
end if
end sub


sub asterix(size)
local i

if size > 1 then
for i = 1 to 5
move(size)
asterix(size * .4)
move(-size)
turn(72)
next i
end if
end sub

Galileo

  • Guest
Re: Turtle graphic library
« Reply #4 on: May 31, 2017, 06:22:57 PM »
And more ...

Code: [Select]
import turtle3

clear screen

startTurtle()

home()
gox(-250)
rancolor()
turn(90)
koch(500, 8)

home()
gox(-100)
goy(100)
rancolor()
turn(90)
snowflake(200, 6)


sub rancolor()
color ran(256), ran(256), ran(256)
end sub

// =====================

sub koch(k, m)
if m = 0 then
move(k)
else
koch(k/3, m-1) : turn(-60)
koch(k/3, m-1) : turn(120)
koch(k/3, m-1) : turn(-60)
koch(k/3, m-1)
end if
end sub


sub snowflake(k, m)
local i

for i = 1 to 3
koch(k, m) : turn(120)
next i
end sub

Galileo

  • Guest
Re: Turtle graphic library
« Reply #5 on: May 31, 2017, 06:44:51 PM »
Batman!

Code: [Select]
import turtle3

clear screen

startTurtle()

home()
goxy(100,600)
multiBatman()


sub rancolor()
color ran(256), ran(256), ran(256)
end sub

// =====================

sub multiBatman()
local i

for i = 0.1 to 0.8 step 0.2
reset()
gox(i * 300) : goy(i * 20)
rancolor()
batman(i)
next i
end sub

sub batman(scale)
local ax, ay

ax = posx() : ay = posy()

hemibatman(1, scale)
goxy(ax, ay)
turn(-263)
hemibatman(-1, scale)
end sub

sub hemibatman(sign, scale)

turn(sign * 5)
draw(30, scale * 3, sign * 1)
draw(250, scale * 0.3, sign * 0.5)
turn(sign * 45)
draw(15, scale * -3, sign * 1)
draw(240, scale * -0.4, sign * 0.7)
draw(10, scale * -3, sign * 1)
turn(sign * 50)
draw(160, scale * 1, sign * -0.6)
draw(100, scale * 1, sign * -0.5)
draw(80, scale * 1, sign * -0.2)
turn(sign * 70)
draw(20, scale * -3, sign * 2)
draw(200, scale * -0.4, sign * 0.7)
move(scale * -40)
turn(sign * 30)
draw(71, scale * 0.8, sign * 0.8)
end sub

sub draw(reps, st, turn)
local i

for i = 1 to reps
move(st) : turn(turn)
next i
end sub

Galileo

  • Guest
Re: Turtle graphic library
« Reply #6 on: June 01, 2017, 03:32:30 PM »
A couple of spirals ...

Code: [Select]
import turtle3

clear screen

startTurtle()

home()
goxy(200, 200)
pen(1)
spircol(.5)

home()
goxy(450, 600)
pen(1)
spirdots(0.3)


sub rancolor()
color ran(256), ran(256), ran(256)
end sub

sub spircol(scale)
local i, c$(6)

c$(0) = "128,0,0" // red
c$(1) = "64,0,64" // purple
c$(2) = "0,0,128" // blue
c$(3) = "0,128,0" // green
c$(4) = "128,128,0" // yellow
c$(5) = "128,83,0" // orange

for i = 1 to 360
color(c$(mod(i,6)))
move(scale * i)
turn(-59)
next i
end sub

//===================

sub prong(i, scale)
pen(0)
move(scale * i)
pen(1)
fill circle posx(), posy(), scale * i / 20
pen(0)
move(scale * i)
end sub

sub spirdots(scale)
local i

for i = 360 to 1 step -1
rancolor()
prong(i, scale)
turn(61)
next i
end sub

Galileo

  • Guest
Re: Turtle graphic library
« Reply #7 on: June 01, 2017, 03:40:16 PM »
Moons, stars, ...

Code: [Select]
import turtle3

clear screen

startTurtle()

home()
MoonAndStars()


sub Mjump()
home()
turn(ran(360))
pen(0)
gox(ran(600)-300)
goy(ran(600)-300)
end sub

sub Mlune(rad)
move(-rad) : turn(-90) : pen(1)
arc(240, rad) : turn(120)
arc(-120, rad)
pen(0) : turn(-150) : move(rad)
end sub

sub Mstar(rad)
local i

move(rad) : turn(162) : pen(1)

for i = 1 to 5
move(rad / 1.38) : turn(-72)
move(rad / 1.38) : turn(144)
next i

pen(0) : turn(-162) : move(-rad)
end sub

sub MoonAndStars()
local i

for i = 1 to 60
pen(0)
Mjump()
if int(ran(2)) then
color 128,128,128
Mlune(19)
else
color 255,255,00
Mstar(25)
end if
next i
end sub


Galileo

  • Guest
Re: Turtle graphic library
« Reply #8 on: June 01, 2017, 04:05:10 PM »
Organic forms ...

Code: [Select]
import turtle3

clear screen

startTurtle()

home()
color 0, 0, 255
gox(-100)
goy(-100)
turn(270)
move(192)
arcWave(192)

home()
gox(-200)
goy(100)
pen(1)
rancolor()
curly(10)

home()
gox(100)
goy(175)
pen(1)
rancolor()
plant(100,0)

home()
gox(100)
goy(-150)
pen(1)
rancolor()
Orbs_1()


sub rancolor()
color ran(256), ran(256), ran(256)
end sub


sub arcWave(size)
if size < 20 return
turn(90)
pen(1)
arc(10, size)
turn(-90)
move(-size)
pen(0)
move(size)
arcWave(size / 1.11)
end sub


sub curly(size)
local i

if size < .5 return

for i = 1 to 360
switch i
case 5: turn(-90) : curly(size/2) : turn(90) : break
case 10: case 15: case 25: turn(-90) : curly(size/5) : turn(90) : break
case 20: turn(-90) : curly(size/4) : turn(90) : break
case 30: turn(-90) : curly(size/8) : turn(90) : break
end switch
move(size)
turn(i)
next i
turn(180)
end sub


sub plant(size, angle)
local i

if size < 1 return

turn(angle)
move(size)
for i = 1 to 4
plant(size/2, ran(160) - 80)
next i
move(-size)
turn(-angle)
end sub


sub Orbs_1()
local size, ang

size = 40

for ang = 0 to 360 step 360/48
pen(-1)
reset()
turn(ang - 90)
move(size) : pen(1)
circle posx(), posy(), size * sin(ang * 0.0174533)
pen(-1) : move(-size)
next ang
end sub

Galileo

  • Guest
Re: Turtle graphic library
« Reply #9 on: June 01, 2017, 04:11:37 PM »
A classic fractal with a few instructions ...

Code: [Select]
import turtle3

clear screen

startTurtle()

home()
color 0, 255, 0
goxy(300,600)
fern(600)


sub fern(size)
  If size < 7 return
  move(size / 20)
  turn(-80) : fern(size * 0.3)
  turn(82) : move(size / 20)
  turn(80) : fern(size * 0.3)
  turn(-78) : fern(size * 0.9)
  turn(-2) : move(-(size / 20))
  turn(-2) : move(-(size / 20))
end sub

Galileo

  • Guest
Re: Turtle graphic library
« Reply #10 on: June 01, 2017, 04:20:10 PM »
A lot of spirals ...

Code: [Select]
import turtle3

clear screen

startTurtle()

home()
PolySpiral()


sub PSpiral(length, angle)
color 0, length/2, 0
if length > 390 then
move(length/2)
return
end if
move(length) : turn(-angle)
PSpiral(length+4, angle)
end sub

sub PolySpiral()
local angle

for angle = 158 to 176 step 2
home()
clear window
color 255,255,255
text 275,100,"Poly spiral number " + str$(angle),"swiss30"
PSpiral(4, angle)
pause 2
next angle
end sub

B+

  • Guest
Re: Turtle graphic library
« Reply #11 on: June 01, 2017, 04:30:38 PM »
Wow! nice collection!

Galileo

  • Guest
Re: Turtle graphic library
« Reply #12 on: June 01, 2017, 04:38:05 PM »
Turning an equilateral triangle into a square: Dudeney's hinged dissection

This animation demonstrates how a triangle can be converted into a square (and vice versa). Compare this code with the javascript equivalent: http://bl.ocks.org/robinhouston/6016317

Code: [Select]
import turtle3

startTurtle()

home()
Dudeney()


sub sqtri()
move(80) : turn(120) : move(160)
turn(120) : move(119.28) : turn(-42.12)
move(60) : turn(90) : move(104) : turn(90)
move(106) : turn(90) : move(53)
end sub

sub shape0()
move(52) : turn(90) : move(45.5) : turn(42.12)
move(41) : turn(120) : move(80)
end sub

sub shape1(offset)
turn(-offset) : move(80) : turn(120) : move(80)
shape2(offset) : turn(180 + offset)
move(60.5) : turn(90) : move(52)
end sub

sub shape2(offset)
turn(-offset) : move(80) : turn(120) : move(39)
shape3(offset) : turn(180 + offset)
move(52) : turn(90) : move(46)
end sub

sub shape3(offset)
turn(-offset) : move(80) : turn(137.88)
move(60) : turn(90) : move(52)
end sub

sub dudneyDraw(ang)
local offset

offset = 90 * (1 + cos(ang * 0.0174533))
home()
turn(210)
color 128, 128, 128
clear window
sqtri()
  color 0, 255, 0
  shape0()
  shape1(offset)
  pause .5
end sub

sub Dudeney()
local ang

for ang = 0 to 180 step 9
dudneyDraw(ang)
next ang

for ang = 180 to 0 step -9
dudneyDraw(ang)
next ang
end sub

Galileo

  • Guest
Re: Turtle graphic library
« Reply #13 on: June 01, 2017, 04:46:26 PM »
Thank you, B +. Making graphics with turtle instructions is a lot of fun.

B+

  • Guest
Re: Turtle graphic library
« Reply #14 on: June 02, 2017, 07:17:17 AM »
Hi Galileo,

The triangle hinge square thingy is nice find! Thanks for sharing. :)