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


Messages In This Thread
Turtle graphics - by Tomaaz - 02-13-2024, 05:13 PM
RE: Turtle graphics - by Marcus - 02-13-2024, 05:58 PM
RE: Turtle graphics - by johnno56 - 02-13-2024, 07:28 PM
RE: Turtle graphics - by Tomaaz - 02-13-2024, 08:09 PM
RE: Turtle graphics - by johnno56 - 02-13-2024, 08:57 PM
RE: Turtle graphics - by Tomaaz - 02-13-2024, 11:37 PM
RE: Turtle graphics - by Marcus - 02-14-2024, 07:28 PM
RE: Turtle graphics - by Tomaaz - 02-14-2024, 08:54 PM
RE: Turtle graphics - by 1micha.elok - 02-15-2024, 04:11 AM
RE: Turtle graphics - by Tomaaz - 02-15-2024, 08:18 AM
RE: Turtle graphics - by 1micha.elok - 02-15-2024, 09:22 AM
RE: Turtle graphics - by Tomaaz - 02-15-2024, 12:17 PM
RE: Turtle graphics - by Marcus - 02-15-2024, 06:59 PM
RE: Turtle graphics - by Tomaaz - 02-15-2024, 08:35 PM
RE: Turtle graphics - by 1micha.elok - 02-16-2024, 02:22 AM

Forum Jump:


Users browsing this thread: 3 Guest(s)