# Turtle graphics

Some simple "turtle graphics" functions are included. Functions to control the turtle begin with a capital letter: Forward, Turn, Circle, Orientation, Rectangle, Pendown, Penup, Pencolor, Penwidth, and Reposition, and so on, and angles are specified in degrees.

using Luxor, Colors
Drawing(600, 400, "assets/figures/turtles.png")
origin()
background("midnightblue")

🐢 = Turtle() # you can type the turtle emoji with \:turtle:
Pencolor(🐢, "cyan")
Penwidth(🐢, 1.5)
n = 5
for i in 1:400
global n
Forward(🐢, n)
Turn(🐢, 89.5)
HueShift(🐢)
n += 0.75
end
fontsize(20)
Message(🐢, "finished")
finish() The turtle commands expect a reference to a turtle as the first argument (it doesn't have to be a turtle emoji!), and you can have any number of turtles active at a time.

quantity = 9
turtles = [Turtle(O, true, 2π * rand(), (rand(), rand(), 0.5)...) for i in 1:quantity]
Reposition.(turtles, first.(collect(Tiler(800, 800, 3, 3))))
n = 10
Penwidth.(turtles, 0.5)
for i in 1:300
global n
Forward.(turtles, n)
HueShift.(turtles)
Turn.(turtles, [60.1, 89.5, 110, 119.9, 120.1, 135.1, 145.1, 176, 190])
n += 0.5
end
finish() # hide A turtle graphics approach lends itself well to recursive programming. This short recursive function draws a Hilbert curve.

function hilbert(t::Turtle, level, angle, lengthstep)
level == 0 && return

HueShift(t, 0.1)

Turn(t, angle)
hilbert(t, level-1, -angle, lengthstep)

Forward(t, lengthstep)
Turn(t, -angle)
hilbert(t, level-1, angle, lengthstep)

Forward(t, lengthstep)
hilbert(t, level-1, angle, lengthstep)

Turn(t, -angle)
Forward(t, lengthstep)
hilbert(t, level-1, -angle, lengthstep)

Turn(t, angle)
end

@draw begin
background("black")
setline(2)
setlinecap("round")

hilbert(Turtle(first(BoundingBox()) + (12, 12), true, 0, (1, 0, 0)),
6,  # level
90, # turn angle, in degrees
6   # steplength
)
end Luxor.TurtleType
Turtle()
Turtle(O)
Turtle(0, 0)
Turtle(O, pendown=true, orientation=0, pencolor=(1.0, 0.25, 0.25))

Create a Turtle. You can command a turtle to move and draw "turtle graphics".

The commands (unusually for Julia) start with a capital letter, and angles are specified in degrees.

Basic commands are Forward(), Turn(), Pendown(), Penup(), Pencolor(), Penwidth(), Circle(), Orientation(), Rectangle(), and Reposition().

Others include Push(), Pop(), Message(), HueShift(), Randomize_saturation(), Reposition(), and Pen_opacity_random().

Luxor.ForwardFunction
Forward(t::Turtle, d=1)

Move the turtle forward by d units. The stored position is updated.

Luxor.TurnFunction
Turn(t::Turtle, r=5.0)

Increase the turtle's rotation by r degrees. See also Orientation.

Luxor.CircleFunction
Circle(t::Turtle, radius=1.0)

Draw a filled circle centered at the current position with the given radius.

Luxor.HueShiftFunction
HueShift(t::Turtle, inc=1.0)

Shift the Hue of the turtle's pen forward by inc. Hue values range between 0 and 360. (Don't start with black, otherwise the saturation and brightness values will be black.)

Luxor.OrientationFunction
Orientation(t::Turtle, r=0.0)

Set the turtle's orientation to r degrees. See also Turn.

Luxor.RectangleFunction
Rectangle(t::Turtle, width=10.0, height=10.0)

Draw a filled rectangle centered at the current position with the given radius.

Luxor.PencolorFunction
Pencolor(t::Turtle, r, g, b)

Set the Red, Green, and Blue colors of the turtle.

Luxor.PointType

The Point type holds two coordinates. It's immutable, you can't change the values of the x and y values directly.

Luxor.PopFunction
Pop(t::Turtle)

Lift the turtle's position and orientation off a stack.

Luxor.PushFunction
Push(t::Turtle)

Save the turtle's position and orientation on a stack.

Luxor.RepositionFunction
Reposition(t::Turtle, pos::Point)
Reposition(t::Turtle, x, y)

Reposition: pick the turtle up and place it at another position.