Skip to content

LOGO-like Turtle Graphics in the Haskell language using Monads.

License

Notifications You must be signed in to change notification settings

aneilmac/worldturtle-haskell

Repository files navigation

WorldTurtle Hackage

Turtle Animations in Haskell

WorldTurtle is a Haskell take on Turtle Graphics.

The intent of this module is a teaching tool by using turtle commands to produce animations.

parallel circles animation

Features

Monadic commands

Turtle commands are monads!

The following snippet produces a square:

import Graphics.WorldTurtle

main :: IO ()
main = runTurtle $
  repeatFor 4 $ do
    forward 90
    right 90

Like so!

turtle drawing a square

Parallel animations

Use of the parallel animation operator (>!>) allows animations to run in parallel. The parallel/serial comparison example shows that when given this code:

import Graphics.WorldTurtle

main :: IO ()
main = runWorld $ do
  turtle1 <- makeTurtle' (0, 0) north green
  turtle2 <- makeTurtle' (0, 0) north red

  -- Draw the anticlockwise and clockwise circles in sequence. 
  (turtle1 >/> circle 90) >> (turtle2 >/> circle (-90))
  
  clear

  -- Draw the anticlockwise and clockwise circles in parallel.
  (turtle1 >/> circle 90) >!> (turtle2 >/> circle (-90))

We get this animation:

serial vs parallel comparison

Interactive Controls

You can interact with the animation window in the following ways:

Action Interaction
Pan the viewport. Click and drag
Zoom in/out. Mousewheel up/down
Reset the viewport to initial position. Spacebar
Reset the animation. R key
Pause the animation. P key
Quit Escape key

Examples

For all examples, look here!

spiralstar example

spiralstar animation

spiralsquare example

spiralsquare animation

lsystem example

lsystem animation

Building a project

Prerequisites

To build this project you need stack and ghc. If you don't already have these, then you can install them easily from the Haskell Platform!

Windows

If you get this error on startup:

user error (unknown GLUT entry glutInit)

Then this means you need the freeglut MSVC binaries which you can get here.

Extract freeglut\bin\x64\freeglut.dll to the same location as the executable you wish to run, or place it in a folder that can be discovered by your %PATH% variable. (Here are some steps on how to add a new folder to your %PATH%.)

Making a new turtle project from a template

Using stack, you can create your own worldturtle project by using the provided template.

To create and run your own project, use the following commands to get setup:

stack new my-new-project aneilmac/worldturtle
cd my-new-project
stack build
stack exec my-new-project

Building and running examples

Examples can be built via stack.

stack setup
stack build

After building, examples in the worldturtle-examples folder can then be executed from stack. To run parallelcircles try:

stack exec parallelcircles-exe

Transcoding from LOGO to WorldTurtle

See here for a detailed description on porting LOGO code to WorldTurtle.