Simple turtle graphics library
Apr 26, 2012 0:22:41 GMT 1
Post by Deleted on Apr 26, 2012 0:22:41 GMT 1
This library was initially created to work with HUG, however, as it make more sense to use it with a new canvas.bac library, it's been slightly modified. It will not work with HUG any more and SETCOLOR function has been removed.
This is a BaCon version of a simple turtle graphics library I've written for Yabasic some time ago. Commands:
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
SETCOLOR ("#xxxxxx") - set color for drawing (use INK from canvas.bac instead)
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.
Library:
An example program:
EDIT: Here is a slower version:
Sources are attached.
EDIT 2: I've changed commands to capitals in description (thanx alexfish!)
This is a BaCon version of a simple turtle graphics library I've written for Yabasic some time ago. Commands:
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.
Library:
GLOBAL KATZOLWIA, POZYCJAX, POZYCJAY, RYSOWANIE
GLOBAL KOLOR$
KOLOR$ = "#000000"
KATZOLWIA = 0
POZYCJAX = 300
POZYCJAY = 300
RYSOWANIE = 1
SUB FORWARD (NUMBER DLUGOSC)
LOCAL A, B, X, Y
A = INT (SIN (KATZOLWIA * PI / 180) * DLUGOSC)
B = INT (COS (KATZOLWIA * PI / 180) * DLUGOSC)
X = POZYCJAX + A
Y = POZYCJAY - B
IF RYSOWANIE = 1 THEN
LINE (KOLOR$, POZYCJAX, POZYCJAY, X, Y)
END IF
POZYCJAX = X
POZYCJAY = Y
SYNC ()
END SUB
SUB BACKWARD (NUMBER DLUGOSC)
LOCAL A, B, X, Y
A = INT (SIN (KATZOLWIA * PI / 180) * DLUGOSC)
B = INT (COS (KATZOLWIA * PI / 180) * DLUGOSC)
X = POZYCJAX - A
Y = POZYCJAY + B
IF RYSOWANIE = 1 THEN
LINE (KOLOR$, POZYCJAX, POZYCJAY, X, Y)
END IF
POZYCJAX = X
POZYCJAY = Y
SYNC ()
END SUB
SUB TURNRIGHT (NUMBER ZMIANA)
KATZOLWIA = KATZOLWIA + ZMIANA
IF KATZOLWIA > 360 THEN
KATZOLWIA = KATZOLWIA - 360
END IF
IF KATZOLWIA < 0 THEN
KATZOLWIA = KATZOLWIA + 360
END IF
END SUB
SUB TURNLEFT (NUMBER ZMIANA)
KATZOLWIA = KATZOLWIA - ZMIANA
IF KATZOLWIA > 360 THEN
KATZOLWIA = KATZOLWIA - 360
END IF
IF KATZOLWIA < 0 THEN
KATZOLWIA = KATZOLWIA + 360
END IF
END SUB
SUB PENUP
RYSOWANIE = 0
END SUB
SUB PENDOWN
RYSOWANIE = 1
END SUB
SUB GOXY (NUMBER ZMIANAX, NUMBER ZMIANAY)
POZYCJAX = ZMIANAX
POZYCJAY = ZMIANAY
END SUB
SUB GOX (NUMBER ZMIANAX)
POZYCJAX = POZYCJAX + ZMIANAX
END SUB
SUB GOY (NUMBER ZMIANAY)
POZYCJAY = POZYCJAY + ZMIANAY
END SUB
SUB RESETANGLE
KATZOLWIA = 0
END SUB
SUB SETCOLOR (STRING NOWYKOLOR$)
KOLOR$ = NOWYKOLOR$
END SUB
An example program:
INCLUDE "/usr/lib/bacon/hug.bac"
HUGOPTIONS ("CANVAS GL")
HUGOPTIONS ("GL_FREEZE 1")
INCLUDE "turtle.bac"
FUNCTION RYSUJ
SQUARE ("#0000FF", 0, 0, 900, 600, TRUE)
RESETANGLE ()
GOXY (110, 300)
FOR X = 1 TO 100
FORWARD (250)
TURNLEFT (198)
NEXT X
RESETANGLE ()
SETCOLOR ("#FF0000")
GOXY (230, 440)
FOR K = 1 TO 10
GOX (2)
GOY (2)
FOR X = 1 TO 36
IF MOD (X, 2) = 0 THEN
PENUP ()
END IF
BACKWARD (20)
PENDOWN ()
TURNRIGHT (10)
NEXT X
NEXT K
RESETANGLE ()
SETCOLOR ("@FFFF00")
GOXY (350, 550)
K = 2
WHILE (K < 496)
FORWARD (500 - K)
TURNRIGHT (91)
K = K + 2
C = C + 1
WEND
SYNC ()
RETURN (FALSE)
END FUNCTION
WIN = WINDOW ("Example of turtle graphics", 920, 620)
RYSUNEK = CANVAS (900, 600)
ATTACH (WIN, RYSUNEK, 10, 10)
TIMEOUT (50, RYSUJ)
DISPLAY
EDIT: Here is a slower version:
INCLUDE "/usr/lib/bacon/hug.bac"
INCLUDE "/usr/lib/bacon/turtle.bac"
HUGOPTIONS ("CANVAS GL")
HUGOPTIONS ("GL_FREEZE 1")
FUNCTION RYSUJ
SQUARE ("#0000FF", 0, 0, 900, 600, TRUE)
RESETANGLE ()
GOXY (110, 300)
FOR X = 1 TO 20
FORWARD (250)
TURNLEFT (198)
SLEEP (500)
NEXT X
RESETANGLE ()
SETCOLOR ("#FF0000")
GOXY (230, 440)
FOR K = 1 TO 10
GOX (2)
GOY (2)
FOR X = 1 TO 36
IF MOD (X, 2) = 0 THEN
PENUP ()
END IF
BACKWARD (20)
PENDOWN ()
TURNRIGHT (10)
SLEEP (100)
NEXT X
NEXT K
RESETANGLE ()
SETCOLOR ("#FFFF00")
GOXY (350, 550)
K = 2
WHILE (K < 496)
FORWARD (500 - K)
TURNRIGHT (91)
K = K + 2
C = C + 1
SLEEP (250)
WEND
RETURN (FALSE)
END FUNCTION
WIN = WINDOW ("Example of turtle graphics", 920, 620)
RYSUNEK = CANVAS (900, 600)
ATTACH (WIN, RYSUNEK, 10, 10)
TIMEOUT (50, RYSUJ)
DISPLAY
Sources are attached.
EDIT 2: I've changed commands to capitals in description (thanx alexfish!)