worldturtle-0.1.0.0: Turtle graphics.
Copyright(c) Archibald Neil MacDonald 2020
LicenseBSD3
MaintainerFortOyer@hotmail.co.uk
Stabilityexperimental
PortabilityPOSIX
Safe HaskellNone
LanguageHaskell2010

Graphics.WorldTurtle

Description

Graphics.WorldTurtle is a module for writing and rendering turtle graphics in Haskell.

Synopsis

Running the turtle

It is easy to create and animate your turtle. You just pass your commands to runTurtle like so:

     import Control.Monad (replicateM_)
     import Graphics.WorldTurtle

     myCommand :: TurtleCommand ()
     myCommand = do 
       t <- makeTurtle
       replicateM_ 4 $ forward 90 t >> right 90 t

     main :: IO ()
     main = runTurtle myCommand

Which will produce this animation

data TurtleCommand a Source #

A TurtleCommand represents an instruction to execute. It could be as simple as "draw a line" or more complicated like "draw 300 circles."

TurtleCommands can be executed in order by combining them using the monadic operator (>>).

Here is an example of how to write a function that when given a size and a turtle, will return a new TurtleCommand which will draw a square with a length and breadth of size using turtle.

     drawSquare :: Float -> Turtle -> TurtleCommand ()
     drawSquare size t = replicateM_ 4 $ forward size t >> right 90 t
  

This draws a square by doing the following in order:

(1/4)
  • Move forward by size amount.
  • Turn right by 90 degrees
(2/4)
  • Move forward by size amount.
  • Turn right by 90 degrees
(3/4)
  • Move forward by size amount.
  • Turn right by 90 degrees
(4/4)
  • Move forward by size amount.
  • Turn right by 90 degrees

Instances

Instances details
Monad TurtleCommand Source # 
Instance details

Defined in Graphics.WorldTurtle.Internal.Commands

Functor TurtleCommand Source # 
Instance details

Defined in Graphics.WorldTurtle.Internal.Commands

Methods

fmap :: (a -> b) -> TurtleCommand a -> TurtleCommand b #

(<$) :: a -> TurtleCommand b -> TurtleCommand a #

MonadFail TurtleCommand Source # 
Instance details

Defined in Graphics.WorldTurtle.Internal.Commands

Methods

fail :: String -> TurtleCommand a #

Applicative TurtleCommand Source # 
Instance details

Defined in Graphics.WorldTurtle.Internal.Commands

Alternative TurtleCommand Source # 
Instance details

Defined in Graphics.WorldTurtle.Internal.Commands

MonadPlus TurtleCommand Source # 
Instance details

Defined in Graphics.WorldTurtle.Internal.Commands

Semigroup a => Semigroup (TurtleCommand a) Source # 
Instance details

Defined in Graphics.WorldTurtle.Internal.Commands

runTurtle Source #

Arguments

:: TurtleCommand ()

Command sequence to execute

-> IO () 

runTurtle takes a TurtleCommand and produces the animation in a new window!

The simplest way to run runTurtle is to execute it directly from your main function like so:

        main :: IO ()
        main = runTurtle yourOwnCoolCommand
    

While running, you can interact with the window in the following way:

ActionInteraction
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
QuitEscape key

Parallel animation

We already know that TurtleCommands can be combined with (>>), but the alternative operation (<|>) can alo be used to combine two TurtleCommands. This has a special meaning: do both animations at the same time!

Note that the result of a <|> b is:

>>> a <|> b
a

when a is not mzero.

(<|>) :: Alternative f => f a -> f a -> f a infixl 3 #

An associative binary operation

Further documentation