Turtle graphics is a vector graphics system using a relative cursor (the turtle) on a cartesian plane.
The turtle has three properties: a position, a heading (direction), and a pen.
The turtle can be moved using commands that are relative to its current state (position and heading).
The turtle carries a pen that can ben set down on the paper, lifted up, and change color.
Each time the turtle moves and the pen is down on the paper it will draw a line on its path.
Turtle graphics was popularized by the Logo programming language. This Xcode playground provides a Swift implementation of a similar system. We use an octopus to represent the turtle because it is cuter and also because it can actually spray ink.
Turtle movement commands:
forward(distance)
: move forward in the current direction by the indicated distanceback(distance)
: move backwards in the current direction by the indicated distanceleft(degrees)
: turn left by the indicated angleright(degrees)
: turn right by the indicated anglehome()
: move the turtle to the starting position and initial heading (at center of the paper, pointing up)
Pen commands:
penUp()
: lift the pen up from the paperpenDown()
: set the pen down on the papersetColor(color)
: change the color of the pen. Colors can be created using the functionTurtleColor.from(hex:)
, a few predefined colors are provided by the system:TurtleColor.black
,TurtleColor.white
,TurtleColor.blue
,TurtleColor.green
,TurtleColor.red
,TurtleColor.yellow
The turtle can be show or hidden without affecting its drawing (if the turtle is hidden and the pen is down it will still draw):
hide()
show()