Thread Rating:
  • 1 Vote(s) - 4 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Turtle graphics
#1
So, here is my simple (and very basic) Turtle graphics library with two examples.

Library (sorry for Polish variable names Smile ):

Code:
visible katzolwia = 0
visible pozycjax = 300
visible pozycjay = 300
visible rysowanie = 1
visible pi = 3.14159

function forward (dlugosc)
    a = int (sin (katzolwia * pi / 180) * dlugosc)
    b = int (cos (katzolwia * pi / 180) * dlugosc)
    x = pozycjax + a
    y = pozycjay - b
    if rysowanie = 1 then
        draw line pozycjax, pozycjay, x, y
    endif
    pozycjax = x
    pozycjay = y
endfunc

function backward (dlugosc)
    a = int (sin (katzolwia * pi / 180) * dlugosc)
    b = int (cos (katzolwia * pi / 180) * dlugosc)
    x = pozycjax - a
    y = pozycjay + b
    if rysowanie = 1 then
        draw line pozycjax, pozycjay, x, y
    endif
    pozycjax = x
    pozycjay = y
endfunc

function turnright (zmiana)
    katzolwia = katzolwia + zmiana
    sprawdz ()
endfunc

function turnleft (zmiana)
    katzolwia = katzolwia - zmiana
    sprawdz ()
endfunc    

function penup ()
    rysowanie = 0
endfunc

function pendown ()
    rysowanie = 1
endfunc

function goxy (zmianax, zmianay)
    pozycjax = zmianax
    pozycjay = zmianay
endfunc

function gox (zmianax)
    pozycjax = pozycjax + zmianax
endfunc

function goy (zmianay)
    pozycjay = pozycjay + zmianay
endfunc

function reset ()
    katzolwia = 0
endfunc

function sprawdz ()
    if katzolwia > 360 then
        katzolwia = katzolwia - 360
    endif
    if katzolwia < 0 then
        katzolwia = katzolwia + 360
    endif
endfunc

A simple example:

Code:
include "turtle.n7"

set window "Turtle Example", 900, 600
set color 0, 0, 0
cls

set color 255, 0, 0

reset ()
goxy (110, 300)
for x = 1 to 100
    forward (250)
    turnleft (198)
next

set color 0, 0, 255

reset ()
goxy (230, 440)
for k = 1 to 10
gox (2)
goy (2)
for x = 1 to 36
    if x % 2 = 0
        penup ()
    endif
    backward (20)
    pendown ()
    turnright (10)
next
next

reset ()

set color 0, 255, 0
goxy (350, 550)
k = 2
while (k < 496)
    forward (500 - k)
    turnright (90.5)
    k = k + 2
    c = c + 1
wend

wait 5000

Koch Curve:

Code:
include "turtle.n7"
set window "Koch", 520, 480
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)
    endif
    
endfunc

koch(200, 5)
wait 5000

All files are attached.


Attached Files
.zip   turtle.zip (Size: 1.26 KB / Downloads: 4)
Reply
#2
Can't wait to try it out!
Reply
#3
Turtle graphics... gotta love it.... Back in "The Dark Ages" (mid to late 1980's), I can remember running turtle programs on my old 8bit Amstrad... Watching the shapes being drawn and anticipating the final result... It took "ages" to draw but it was worth the wait...

Nicely done! Thank you for the memories, Tomaaz!
Logic is the beginning of wisdom.
Reply
#4
Believe it or not, the only "computer science" I learned in high school was turtle graphics. Big Grin
Reply
#5
When I was in High School, computers were in the realm of big business or SciFi movies... It would be another 15 years later before I would actually own an 8 bit computer... yikes!
Logic is the beginning of wisdom.
Reply
#6
I should have probably mention that the only place and time when I studied "computer science" was that high school and it was after I managed to teach myself the machine code used by 6502 processors. Big Grin
Reply
#7
Before the old forum exploded I posted a basic compiler written in n7. It produced n7 assembler code that was compiled by the regular n7 compiler. Among all, it had built in turtle graphics. I haven't looked at or tried your code yet (still no computer). But after doing so, I'll probably get inspired and want to revisit my code.

One of the first computers I wrote programs for was named Compis (swedish pun actually, "kompis" means friend): https://en.m.wikipedia.org/wiki/Compis The programming language was Comal and it had commands for turtle graphics. It was fun Smile I wonder if it's possible to find a Compis somewhere today ..
Reply
#8
(02-14-2024, 07:28 PM)Marcus Wrote: I haven't looked at or tried your code yet (still no computer). But after doing so, I'll probably get inspired and want to revisit my code.

And I'm pretty sure you will come up with something much better. Wink My library is extremely basic. It should be named no turtle graphics as, prepare yourself, there is no... turtle on the screen. Big Grin 

(02-14-2024, 07:28 PM)Marcus Wrote: One of the first computers I wrote programs for was named Compis (swedish pun actually, "kompis" means friend): https://en.m.wikipedia.org/wiki/Compis  The programming language was Comal and it had commands for turtle graphics. It was fun Smile  I wonder if it's possible to find a Compis somewhere today ..

The machine I've had a pleasure to study turtle graphics on was Elwro 800 Junior - a Polish clone of ZXSpectrum. Smile At that time I was a happy owner of C64 and was able to write a simple demos in machine code, so "moving on" to turtle graphics was kind of funny. Big Grin

[Image: e0ffe91f65e1fd6ba1624132474ad619.jpg]
Reply
#9
(02-13-2024, 05:13 PM)Tomaaz Wrote: So, here is my simple (and very basic) Turtle graphics library ..

Code:
...

function sprawdz ()
    if katzolwia > 360 then
        katzolwia = katzolwia - 360
    endif
    if katzolwia < 0 then
        katzolwia = katzolwia + 360
    endif
endfunc
...

...

I found that "these angle 0 and and 360" would be missed in the calculation, I proposed to add >= and <=, so that your function would be

Code:
...

function sprawdz ()
   if katzolwia >= 360 then
     katzolwia = katzolwia - 360
   endif
   if katzolwia <= 0 then
     katzolwia = katzolwia + 360
   endif
endfunc
...
Reply
#10
While your code will work, it doesn't make any difference. Katzolwia represents the angle of the turtle in degrees. If the angle is bigger than 360, that function will change it to the same angle from 0..360 range. The same goes for angles smaller than 0. Of course, you can swap 360 for 0 and 0 for 360, but, like I said, it doesn't make any difference.

For smaller programs you could skip that part completely. Computers can calculate trigonometric functions from numbers bigger than 360 or smaller tha 0 with no problems. I decided to include this function to prevent katzolwia to become to big or to small what could make the entire program to crash (if the number becomes to big or to small for variable to keep it).
Reply


Forum Jump:


Users browsing this thread: 3 Guest(s)