Copyright | (c) Archibald Neil MacDonald 2020 |
---|---|
License | BSD3 |
Maintainer | FortOyer@hotmail.co.uk |
Stability | experimental |
Portability | POSIX |
Safe Haskell | None |
Language | Haskell2010 |
Graphics.WorldTurtle is a module for writing and rendering turtle graphics in Haskell.
Take a look at the examples on Github!
Synopsis
- data TurtleCommand a
- runTurtle :: TurtleCommand () -> IO ()
- (<|>) :: Alternative f => f a -> f a -> f a
- module Graphics.WorldTurtle.Commands
- module Graphics.WorldTurtle.Shapes
- module Graphics.WorldTurtle.Color
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."
TurtleCommand
s 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
- Move forward by
(2/4)
- Move forward by
size
amount. - Turn right by
90
degrees
- Move forward by
(3/4)
- Move forward by
size
amount. - Turn right by
90
degrees
- Move forward by
(4/4)
- Move forward by
size
amount. - Turn right by
90
degrees
- Move forward by
Instances
:: 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:
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 |
Parallel animation
We already know that TurtleCommand
s can be combined with (>>)
, but the
alternative operation (<|>)
can alo be used to combine two
TurtleCommand
s. 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
module Graphics.WorldTurtle.Shapes
module Graphics.WorldTurtle.Color