### Author Topic: Simple turtle library for EGSL  (Read 4447 times)

#### Tomaaz

• Guest
##### Simple turtle library for EGSL
« on: 11. November 2012, 21:45:05 »
I've ported my simple turtle library for Yabasic to EGSL. Not really meant for games, but who knows.

forward(x) - moves the turtle x pixels forward and draws the line when pen is down
backward(x) - moves the turtle x pixels backward and draws the line when pen is down
turnright(x) - turns the turtle x degrees to the right
turnleft(x) - turns the turtle x degrees to the left
pendown() - puts the pen down
penup() - lifts the pen up
goxy(x, y) - moves the turtle to (x, y) position
gox(x) - moves the turtle vertically by x pixels
goy(x) - moves the turtle horizontally by x pixels
resetangle() - set the angle to 0

No matter if the pen is down or up, goxy, gox and goy don't draw anything on the canvas. Value of angle (in degrees) is stored in katzolwia global variable. Position of the turtle is stored in pozycjax and pozycjay global variables.

The source, two examples and updated configuration file for Geany (syntax highlighting) are attached.

Library:
Code: [Select]
`pi = 3.14159katzolwia = 0pozycjax = 300pozycjay = 300rysowanie = 1function forward(dlugosc)    local a, b, x, y    a = (math.sin(katzolwia * pi / 180) * dlugosc)    b = (math.cos(katzolwia * pi / 180) * dlugosc)    x = pozycjax + a    y = pozycjay - b    if rysowanie == 1 then        line (pozycjax, pozycjay, x, y)    end    pozycjax = x    pozycjay = y    redraw()endfunction backward(dlugosc)    local a, b, x, y    a = (math.sin(katzolwia * pi / 180) * dlugosc)    b = (math.cos(katzolwia * pi / 180) * dlugosc)    x = pozycjax - a    y = pozycjay + b    if rysowanie == 1 then line (pozycjax, pozycjay, x, y)    end    pozycjax = x    pozycjay = y    redraw ()end function turnright(zmiana)    katzolwia = katzolwia + zmiana    if katzolwia > 360 then        katzolwia = katzolwia - 360    end    if katzolwia < 0 then        katzolwia = katzolwia + 360    endendfunction turnleft (zmiana)    katzolwia = katzolwia - zmiana    if katzolwia > 360 then        katzolwia = katzolwia - 360    end    if katzolwia < 0 then        katzolwia = katzolwia + 360    endendfunction penup()    rysowanie = 0endfunction pendown()    rysowanie = 1endfunction goxy(zmianax, zmianay)    pozycjax = zmianax    pozycjay = zmianayendfunction gox(zmianax)    pozycjax = pozycjax + zmianaxendfunction goy(zmianay)    pozycjay = pozycjay + zmianayendfunction resetangle()    katzolwia = 0end`
Koch Curve:
Code: [Select]
`require "turtle"openwindow (640, 480, 32, "Test")goxy (10, 300)turnright(90)function koch(x, t) if t > 0 then t = t - 1 x = x / 3 koch(x, t) turnleft(60) koch(x, t) turnright(120) koch(x, t) turnleft(60) koch(x, t) else forward(3 * x) endendkoch(200, 6)inkey()`

Example 2:
Code: [Select]
`require "turtle"openwindow(900, 650, 32, "Test")resetangle()goxy(110, 300)    for x = 1, 20 do colour(255, 255 - x * 10, x * 5) forward(250)    turnleft(198)    end    resetangle()    goxy(230, 440)    for k = 1, 20   do  colour(0, 0, 256 - k * 10) gox(2)    goy(2)    for x = 1, 36 do    if x % 2 == 0 then    penup()    end    backward(20)    pendown()    turnright(10)    end    end   resetangle()    goxy(350, 550)    k = 2    while (k < 496) do colour (k / 2, k / 2, k / 2) forward(500 - k)    turnright(91)    k = k + 2   endinkey()`
« Last Edit: 03. February 2013, 11:40:27 by Cybermonkey »

#### cvirus

• Regular Member
• Jr. Member
• Posts: 76
##### Re: Simple turtle library for EGSL
« Reply #1 on: 12. November 2012, 14:02:15 »
Very nice, thanks.

#### Tomaaz

• Guest
##### Re: Simple turtle library for EGSL
« Reply #2 on: 12. November 2012, 19:20:28 »
There is no need to use a special pi variable as Lua has build in math.sin() function. For some reason I was trying to call sin() instead of math.sin(). Of course it didn't work, so I added the pi variable.

I've also made another improvement. By default the library call EGSL redraw() function after any drawing takes place. Now it's possible to disable it by using a new setredraw(0) function and call redraw() manually when needed. It makes execution significantly faster. Just compare old Koch Curve example with this one:
Code: [Select]
`require "turtle"openwindow (640, 480, 32, "Test")goxy (10, 300)turnright(90)setredraw(0)function koch(x, t) if t > 0 then t=t-1 x=x/3 koch(x, t) turnleft(60) koch(x, t) turnright(120) koch(x, t) turnleft(60) koch(x, t) else forward(3*x) endendkoch(200, 6)redraw()inkey()`
To enable automatic redrawing just use setredraw(1).

New version of the library and an updated file for Geany are attached.
« Last Edit: 12. November 2012, 21:07:55 by Tomaaz »

#### Bereb

• Newcomer
• Posts: 47
##### Re: Simple turtle library for EGSL
« Reply #3 on: 13. November 2012, 00:12:12 »
Here's a very very minimal "turtle", which could be written as a library too :

Code: [Select]
`#!/usr/bin/egsl--================================================---- Turtle Graphics  (EGSL) - RECURSIVE TREE (3)---- EGSL 1.5.3 -  Bertrand Carette - nov. 2012--================================================--openwindow(400, 400, 0, "EGSL Turtle Graphics")backcolor(255,255,255)color(0,0,0)clearscreen()--------------------------------------------------- constants & variables-------------------------------------------------center = {x = screenwidth()/2, y =  screenheight()/2}coor= {x = 0, y = 0} -- coordinates of the turtleheading = 0 -- orientation of the turtlependown = true -- pen is downpensize = 0 -- drawing width--------------------------------------------------- basic procedures for polar/turtle graphics-------------------------------------------------local sin, cos, rad = math.sin, math.cos, math.rad -- (faster if declared as local)function setpos(px, py) -- draw if pen is down if pendown then for i = 0, pensize/2, 0.25 do aaline(center.x+(coor.x+i), center.y-(coor.y+i),center.x+(px+i), center.y-(py+i)) end end -- set new position coor.x, coor.y = px, pyendfunction home() -- move to screen center without drawing or clearing pen 'up'   -- pendown = false setpos(0,0) heading = 0 pen 'down' -- pendown = trueendfunction turn(degree) -- set orientation in degree : right = +degree / left = -degree heading = heading + degree % 360endfunction go(distance) -- move : forward = +distance / backward = -distance local direction = rad(heading) setpos(coor.x+(distance*sin(direction)), coor.y+(distance*cos(direction)))endfunction pen(p) local p = p or 'down' if p == 'down' then pendown = true elseif p == 'up' then pendown = false else pendown = false endend---------------------------------------------------- MAIN PROGRAM : recursive tree (3)--------------------------------------------------setcaption("Recursive tree (3)")red   = {255,0,0}green = {0,255,0}blue  = {0,0,255}col   = {red, green, blue}function tree(size) pensize = size/8 pencolor = col[math.random(1,3)] colour(pencolor[1], pencolor[2], pencolor[3]) go(size) if size > 5 then turn(30) tree(size/2) turn(30) tree(size/2) turn(-90) tree(size/2) turn(-30) tree(size/2) turn(60) end pen 'up' go(-size) pen 'down' pensize = 0end pen 'up'go(-150)pen 'down'tree(150)redraw()inkey()closewindow()-- fin du programme`
« Last Edit: 13. November 2012, 09:51:13 by Bereb »
Regards, Bertrand
** Lubuntu 14.04 (Intel Celeron CPU 2.66GHz - 1280MiB) **

#### Tomaaz

• Guest
##### Re: Simple turtle library for EGSL
« Reply #4 on: 13. November 2012, 03:09:36 »
Nice. But I needed to fix line 25 to make it work: local sin, cos, rad = math.sin, math.cos, math.rad --(faster if declared as local)

Regards.

#### Bereb

• Newcomer
• Posts: 47
##### Re: Simple turtle library for EGSL
« Reply #5 on: 13. November 2012, 09:50:40 »
But I needed to fix line 25 to make it work: local sin, cos, rad = math.sin, math.cos, math.rad --(faster if declared as local)
Oh, sorry
This comment was not in the original script. I added it afterwards to point the trick : when variables are declared as 'local', programs run faster. It's obvious especially with fractals (I've got some samples and have tested them with other prog. languages, see there). Thus these sorts of program run up to 5 or 6 times faster and then EGSL (and consequently Lua) has no cause to be envious of some other compiled programing languages
« Last Edit: 13. November 2012, 10:28:02 by Bereb »
Regards, Bertrand
** Lubuntu 14.04 (Intel Celeron CPU 2.66GHz - 1280MiB) **

#### Cybermonkey

• Hero Member
• Posts: 577
##### Re: Simple turtle library for EGSL
« Reply #6 on: 13. November 2012, 10:27:19 »
Good to know (with that local declaration). Although the EGSL implemented sin, cos etc. are implemented in Pascal and are compiled ...
EDIT: Hm, forget that I was talking rubbish ... The math.sin etc. are implemented in C, right? So Pascal can't be faster.
« Last Edit: 13. November 2012, 10:35:14 by Cybermonkey »
Best regards,
Cybermonkey

#### Bereb

• Newcomer
• Posts: 47
##### Re: Simple turtle library for EGSL
« Reply #7 on: 13. November 2012, 10:33:58 »
Although the EGSL implemented sin, cos etc. are implemented in Pascal and are compiled ...
Yes. I go on  declaring them out of a bad habit, that I'v got before  I notice the EGSL helper functions, sorry !
Regards, Bertrand
** Lubuntu 14.04 (Intel Celeron CPU 2.66GHz - 1280MiB) **

#### lelldorin

• Jr. Member
• Posts: 67
##### Re: Simple turtle library for EGSL
« Reply #8 on: 02. February 2013, 21:24:10 »
Hello All,

can i add this example as part of my EGSL IDE?

http://haikuware.com/directory/view-details/development/ides/egsl-ide

#### Tomaaz

• Guest
##### Re: Simple turtle library for EGSL
« Reply #9 on: 03. February 2013, 00:27:52 »
I'm not sure if you're asking about my simple library or Bereb's work, but if it's my library, feel free to use it and all my examples.

Regards!
« Last Edit: 03. February 2013, 00:37:45 by Tomaaz »

#### Cybermonkey

• Hero Member
• Posts: 577
##### Re: Simple turtle library for EGSL
« Reply #10 on: 03. February 2013, 11:41:36 »
Sorry, but due to security reasons I removed the links to the images. As Mopz said, maybe you'll find another hoster for images (flickr?)
Best regards,
Cybermonkey

#### Tomaaz

• Guest
##### Re: Simple turtle library for EGSL
« Reply #11 on: 03. February 2013, 12:40:05 »
Yes, I need to find something or... maybe you could change the limit of attachments from 2 to 3? One archive with all files needed to run an example and two screenshots would be enough.

#### Tomaaz

• Guest
##### Re: Simple turtle library for EGSL
« Reply #12 on: 16. May 2013, 23:35:26 »
I've added some features to make the library more kids-friendly (after all, I've written it for my son). There are two new functions for colour. They accept 17 colour names (white, black, green, red, blue, yellow, grey, purple, aqua, teal, olive, maroon, navy, magenta, lightblue, lightgrey, darkgrey).

setcolour("[colour name]") - set the colour for drawing to "colour name"
setbgcolour("[colour name]") - clears the screen and set the background colour to"colour name"

For more colours you can still use EGSL functions.

There is also a new fill() function that fills the area with a currently set colour.

Here is a small example (see the screenshot):
Code: [Select]
`require "turtle"openwindow (640, 550, 0, "Example of turtle graphics")function koch(x, t) if t > 0 then t=t-1 x=x/3 koch(x, t) turnleft(60) koch(x, t) turnright(120) koch(x, t) turnleft(60) koch(x, t) else forward(3*x) endendsetredraw(0)setbgcolor("red")setcolor("yellow")goxy (100, 150)turnright(90)for x = 1, 3 do koch(150, 5) turnright(120)endgoxy (250, 237)for x = 1, 3 do koch(50, 4) turnright(120)endsetcolor("white")circle (325, 275, 200)redraw()setredraw(1)goxy (200, 200)setcolor("purple")fill()goxy (320, 275)setcolor("olive")fill()goxy (10, 10)setcolor("teal")fill()inkey()`
Source codes and an updated configuration file for Geany are attached.
« Last Edit: 17. May 2013, 00:10:14 by Tomaaz »

#### Aurel

• Regular Member
• Sr. Member
• Posts: 286
##### Re: Simple turtle library for EGSL
« Reply #13 on: 25. September 2014, 19:55:27 »
Hi Tomek
I am playing with this Turtle functions in my interpreter and i looking into
how big is variable t
because inside if / end if you have
t=t-1  but i don't see where is t set to value
any help maybe ?
new basic pro forum on;
http://basicpro.spacefor.site/smf/
GUEST posting enabled

#### Cybermonkey

`koch(150, 5)`then t=5.