Author Topic: Turtle graphic library  (Read 4655 times)

Aurel

  • Guest
Re: Turtle graphic library
« Reply #15 on: June 02, 2017, 09:25:37 AM »
Main question here is :
What is a license of this examples...he ...he... 8)


ps..it is joke question  :D

Galileo...very nice examples ..i like it  :D

Peter

  • Guest
Re: Turtle graphic library
« Reply #16 on: June 11, 2017, 02:34:32 PM »
The original tree program uses colors which looks awesome... below the BaCon version.

Code: [Select]
INCLUDE canvas.bac

OPTION VARTYPE double

WINDOW("Turtle Graphics Tree", 600, 600)

INK(0,0,0,255)
CLS

RESETANGLE

PENXY(300, 600)

PEN(1, TRUE)

CALL tree(400)

WAITKEY

SUB tree(size)

    IF size < 5 THEN
        DRAW(size)
        DRAW(-size)
        EXIT SUB
    ENDIF

    INK(0,255,0,255)
    DRAW(size/3)

    TURN(-30)
    tree(size*2/3)
    TURN(30)

    INK(0,255,0,255)
    DRAW(size/6)

    TURN(25)
    tree(size/2)
    TURN(-25)

    INK(0,255,0,255)
    DRAW(size/3)

    TURN(25)
    tree(size/2)
    TURN(-25)

    INK(139,69,19,255)
    DRAW(size/6)

    DRAW(-size)

END SUB

Peter

  • Guest
Re: Turtle graphic library
« Reply #17 on: June 11, 2017, 06:26:38 PM »
Can't resist animations  ;D

Code: [Select]
INCLUDE canvas-gd.bac

OPTION VARTYPE double

WINDOW("Dudeney", 500, 500)

RESETANGLE

PENXY(250, 250)

PEN(2, TRUE)

DELAY(80)

CALL Dudeney()

WAITKEY

SUB sqtri()

    DRAW(80) : TURN(120) : DRAW(160)
    TURN(120) : DRAW(119.28) : TURN(-42.12)
    DRAW(60) : TURN(90) : DRAW(104) : TURN(90)
    DRAW(106) : TURN(90) : DRAW(53)

END SUB

SUB shape0()

    DRAW(52) : TURN(90) : DRAW(45.5) : TURN(42.12)
    DRAW(41) : TURN(120) : DRAW(80)

END SUB

SUB shape1(offset)

    TURN(-offset) : DRAW(80) : TURN(120) : DRAW(80)
    CALL shape2(offset) : TURN(180 + offset)
    DRAW(60.5) : TURN(90) : DRAW(52)

END SUB

SUB shape2(offset)

    TURN(-offset) : DRAW(80) : TURN(120) : DRAW(39)
    CALL shape3(offset) : TURN(180 + offset)
    DRAW(52) : TURN(90) : DRAW(46)

END SUB

SUB shape3(offset)

    TURN(-offset) : DRAW(80) : TURN(137.88)
    DRAW(60) : TURN(90) : DRAW(52)

END SUB

SUB dudneyDraw(ang)

    LOCAL offset

    offset = 90 * (1 + COS(ang * 0.0174533))
    PENXY(250, 250)
    TURN(210)
    INK(128, 128, 128, 255)
    CLS
    sqtri()
    INK(0, 255, 0, 255)
    shape0()
    shape1(offset)
    SYNC

END SUB

SUB Dudeney()

    LOCAL ang

    FOR ang = 0 TO 180 STEP 5
        dudneyDraw(ang)
    NEXT

    FOR ang = 180 DOWNTO 0 STEP 5
        dudneyDraw(ang)
    NEXT

END SUB


Galileo

  • Guest
Re: Turtle graphic library
« Reply #18 on: December 21, 2017, 12:48:23 PM »
A small update to the library. Added the filling function and the possibility to show the orientation of the turtle.

Code: [Select]
REM Simplified turtle commands for Yabasic 2.78.0, by Galileo, 5/2017. Rev. 12/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

export sub infill(test$) // Fill irregular shapes

wid = peek("winwidth") : hei = peek("winheight")
floodFill(posx, posy, test$)
end sub

sub floodFill(x, y, test$)

if test$ <> getbit$(x, y, x, y) then
dot x, y
floodFill(x + 1, y, test$)
floodFill(x - 1, y, test$)
floodFill(x, y + 1, test$)
floodFill(x, y - 1, test$)
end if
end sub

export sub turtle() // show turtle orientation

color 255,0,0

pen(true)
move(10)
fill circle posx, posy, 2
end sub

A little example.

Code: [Select]
import turtle3

clear screen

startTurtle()

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

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

sub multiBatman()
local i

for i = 0.1 to 0.8 step 0.2
reset()
gox(i * 200) : goy(i * -50)
turn(i*100)
color 128 * i, 128 * i, 255
batman(i)
next i
end sub

sub batman(scale)
local ax, ay, angle, color$

ax = posx() : ay = posy()

hemibatman(1, scale)
goxy(ax, ay)
color$ = getbit$(ax,ay,ax,ay)
turn(-263)
hemibatman(-1, scale)
pen(false) : turn(90) : move(5) : pen(true)
infill(color$)
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