Tutorial

Tutorial: Turtle Geometry

Have you ever heard of turtle geometry?

(1983) Talking Turtle (for PBS) (opens in a new tab)

Watch on tube.arthack.nz (opens in a new tab) or archive.org (opens in a new tab)

We could implement Logo-inspired turtle geometry using Rimu, where kids could move turtles with code.

If we wanted to make a star (opens in a new tab):

- setwidth: 10
- setcolor: "red"
 
- right: 18
- forward: 50
 
- map
    list: range({ end: 5 })
    each: () =>
      - right: 144
      - forward: 50
      - left: 72
      - forward: 50
 

Then the output is a list of commands for the turtle to execute.

Now we might start to think about a better data model, how do we want users to describe their turtle actions?

Let's say we create 3 types of actions:

  • set: set the width or color of the pen
  • rotate: rotate the turtle by some degrees
  • move: move the turtle by some distance

We code our actions in Rust:

#[derive(Debug, serde::Deserialize)]
#[serde(tag = "action")]
enum Action {
    #[serde(rename = "set")]
    Set {
        width: Option<u32>
        color: Option<Color>
    },
    #[serde(rename = "rotate")]
    Rotate(Rotation)
    #[serde(rename = "move")]
    Move(Movement)
}
 
#[derive(Debug, serde::Deserialize)]
#[serde(untagged)]
enum Rotation {
    Left {
        left: u32
    },
    Right {
        right: u32
    },
}
 
#[derive(Debug, serde::Deserialize)]
#[serde(untagged)]
enum Movement {
    Forward {
        forward: u32
    },
    Backward {
        backward: u32
    },
}

Now to describe our star:

- action: "set"
  width: 10
  color: 4
 
- action: "rotate"
  right: 18
- action: "move"
  forward: 50
 
- map
    list: range({ end: 5 })
    each: () =>
      - action: "rotate"
        right: 144
      - action: "move"
        forward: 50
      - action: "rotate"
        left: 72
      - action: "move"
        forward: 50
 

TODO: Make a demo to show this in action.

For now, see Start: Library for how to parse, evaluate, and convert Rimu code into Rust data types.