Lesson goal: Turtle Graphics and Logo Drawing

Previous: Draw something! | Home | Next: Turtle Fractals and Recursive Curves

Turtle Graphics was created in 1967 by Seymour Papert, Wally Feurzeig, and Cynthia Solomon at MIT as a core part of the Logo programming language. Instead of plotting points at fixed $(x, y)$ coordinates, you command an imaginary "turtle" holding a pen using simple, relative movements.

The Turtle Coordinate System

The turtle lives on our graphics canvas with its origin $(0, 0)$ at the center of the screen:
  • Its starting position is at the center: $(x = 0, y = 0)$.
  • Its heading points straight North ($0^\circ$, pointing toward the top of the canvas).
  • Its pen is down, ready to draw.


Complete Turtle Command Reference

Here are the commands available in CodeByMath's Turtle API:
  • Movement:
    • forward(distance) or fd(d): Move forward by distance pixels in the current heading.
    • backward(distance) or bk(d): Move backward by distance pixels without changing heading.
    • setpos(x, y) or setxy(x, y): Move the turtle directly to coordinates $(x, y)$.
  • Turning & Orientation:
    • right(degrees) or rt(angle): Turn clockwise by degrees.
    • left(degrees) or lt(angle): Turn counter-clockwise by degrees.
    • heading(degrees): Set an absolute heading in degrees ($0^\circ = \text{North/Up}$, $90^\circ = \text{East/Right}$, $180^\circ = \text{South/Down}$, $270^\circ = \text{West/Left}$).
  • Pen & Styling:
    • penup() or up(): Lift the pen so moving the turtle leaves no trail.
    • pendown() or down(): Put the pen down so moving the turtle draws a line.
    • pencolor("color"): Set drawing color (accepts names like "cyan", "lime", "yellow", "red", "orange", "purple", or hex codes like "#00ff88").
    • pensize(width): Set stroke thickness (e.g. 1, 2, 3).
  • Turtle Avatar & Screen Controls:
    • draw_turtle() or showturtle(): Draw the turtle avatar at its current location and heading on the canvas.
    • turtle_reset() or reset(): Move the turtle back to $(0,0)$ facing North with pen down.
    • pcls(): Clear the canvas screen.
    • fast(speed): Control animation speed (e.g. fast(1) for step-by-step animation, fast(20) for rapid drawing).
    • turtle_circle(radius, extent): Draw a smooth circle or arc of given radius.


The Mathematics of Polygons and Exterior Angles

When drawing any regular polygon of $n$ sides, the turtle must complete a full $360^\circ$ circuit. Therefore, the turn angle at each vertex is the exterior angle: $$\text{Turn Angle} = \frac{360^\circ}{n}$$
  • Equilateral Triangle ($n = 3$): $360 / 3 = 120^\circ$
  • Square ($n = 4$): $360 / 4 = 90^\circ$
  • Regular Pentagon ($n = 5$): $360 / 5 = 72^\circ$
  • Regular Hexagon ($n = 6$): $360 / 6 = 60^\circ$
  • Regular Octagon ($n = 8$): $360 / 8 = 45^\circ$

Now you try. Modify the code to draw an equilateral triangle ($3$ sides, $120^\circ$ turn) or a regular hexagon ($6$ sides, $60^\circ$ turn).

Type your code here:


See your results here: