Turtle graphics

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)
global n = 5
for i in 1:400
    Forward(๐Ÿข, n)
    Turn(๐Ÿข, 89.5)
    HueShift(๐Ÿข)
    n += 0.75
end
fontsize(20)
Message(๐Ÿข, "finished")
finish()
โ”Œ Warning: Deprecated syntax `implicit assignment to global variable `n``.
โ”‚ Use `global n` instead.
โ”” @ nothing none:0
WARNING: importing deprecated binding Base.uninitialized into Cairo.
WARNING: Base.uninitialized is deprecated, use undef instead.
  likely near /Users/travis/build/JuliaGraphics/Luxor.jl/docs/make.jl:3
WARNING: Base.uninitialized is deprecated, use undef instead.
  likely near /Users/travis/build/JuliaGraphics/Luxor.jl/docs/make.jl:3
WARNING: Base.uninitialized is deprecated, use undef instead.
  likely near /Users/travis/build/JuliaGraphics/Luxor.jl/docs/make.jl:3
โ”Œ Warning: The function `cfunction` is now written as a macro `@cfunction`.
โ”‚   caller = get_stream_callback at Cairo.jl:145 [inlined]
โ”” @ Core Cairo.jl:145

text placement

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, 2pi * rand(), (rand(), rand(), 0.5)...) for i in 1:quantity]
Reposition.(turtles, first.(collect(Tiler(800, 800, 3, 3))))
global n = 10
Penwidth.(turtles, 0.5)
for i in 1:300
    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
โ”Œ Warning: Deprecated syntax `implicit assignment to global variable `n``.
โ”‚ Use `global n` instead.
โ”” @ nothing none:0
โ”Œ Warning: The function `cfunction` is now written as a macro `@cfunction`.
โ”‚   caller = get_stream_callback at Cairo.jl:145 [inlined]
โ”” @ Core Cairo.jl:145

text placement

Luxor.Turtle โ€” Type.
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().

source
Luxor.Forward โ€” Function.
Forward(t::Turtle, d=1)

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

source
Luxor.Turn โ€” Function.
Turn(t::Turtle, r=5.0)

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

source
Luxor.Circle โ€” Function.
Circle(t::Turtle, radius=1.0)

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

source
Luxor.HueShift โ€” Function.
HueShift(t::Turtle, inc=1.0)

Shift the Hue of the turtle's pen forward by inc. Hue values range between 0 and 360.

source
Luxor.Message โ€” Function.
Message(t::Turtle, txt)

Write some text at the current position.

source
Luxor.Orientation โ€” Function.
Orientation(t::Turtle, r=0.0)

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

source
Luxor.Randomize_saturation โ€” Function.
Randomize_saturation(t::Turtle)

Randomize the saturation of the turtle's pen color.

source
Luxor.Rectangle โ€” Function.
Rectangle(t::Turtle, width=10.0, height=10.0)

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

source
Luxor.Pen_opacity_random โ€” Function.
Pen_opacity_random(t::Turtle)

Change the opacity of the pen to some value at random.

source
Luxor.Pendown โ€” Function.
Pendown(t::Turtle)

Put that pen down and start drawing.

source
Luxor.Penup โ€” Function.
Penup(t::Turtle)

Pick that pen up and stop drawing.

source
Luxor.Pencolor โ€” Function.
Pencolor(t::Turtle, r, g, b)

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

source
Luxor.Penwidth โ€” Function.
Penwidth(t::Turtle, w)

Set the width of the line drawn.

source
Luxor.Point โ€” Type.

The Point type holds two coordinates. Currently it's immutable, so remember not try to change the values of the x and y values directly.

source
Luxor.Pop โ€” Function.
Pop(t::Turtle)

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

source
Luxor.Push โ€” Function.
Push(t::Turtle)

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

source
Luxor.Reposition โ€” Function.
Reposition(t::Turtle, pos::Point)
Reposition(t::Turtle, x, y)

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

source