-- Hoogle documentation, generated by Haddock -- See Hoogle, http://www.haskell.org/hoogle/ -- | LOGO-like Turtle graphics with a monadic interface. -- -- Have you ever heard of Turtle Graphics? -- -- If not, then think of a turtle as a cursor you can program to -- draw! -- -- Turtle graphics are a fantastic introduction to the world of -- programming and to the syntax of a new programming language. -- -- -- This module is a framework built on top of gloss to render -- turtles programmed in Haskell as animations. This is primarily aimed -- as a teaching tool to beginners - but also, it's cool to draw things! -- -- See The API ref, Graphics.WorldTurtle, for features! -- -- It's easy to create a new project with stack: -- --
-- stack new my-worldturtle-project aneilmac/worldturtle -- cd my-worldturtle-project -- stack build -- stack exec my-worldturtle-project --@package worldturtle @version 0.3.1.0 -- | This module is a collection of color manipulation commands! module Graphics.WorldTurtle.Color -- | Rotates a given color's hue between [0, 360) degrees. shiftHue :: Float -> Color -> Color -- | This module exposes shapes not found in gloss but may be found to be -- worthwhile. module Graphics.WorldTurtle.Shapes -- | Creates the default turtle polygon arrow with a given outline color -- and fill color. turtleArrow :: Color -> Color -> Picture -- | Draws a line from a start-point to an end-point with a given -- thickness. thickLine :: Point -> Point -> Float -> Picture -- | This module contains all commands used to create, move and manipulate -- a turtle. module Graphics.WorldTurtle.Commands -- | The Turtle that is drawn on the canvas! Create a new turtle using -- makeTurtle. data Turtle -- | A point on the x-y plane. type Point = (Float, Float) -- | Creates a new Turtle and displays it on the canvas. This turtle -- can then be manipulated! For example, to create a turtle and then move -- the turtle forward: -- --
-- main:: IO () -- main = runWorld $ do -- t <- makeTurtle -- t >/> forward 90 ---- -- The default turtle starts at position (0, 0) and is -- orientated north. makeTurtle :: WorldCommand Turtle -- | This variant of makeTurtle takes a starting position, a -- starting orientation, and a color to apply to the turtle and the -- turtle's pen. -- --
-- myCommand :: WorldCommand () -- myCommand = do -- t1 <- makeTurtle' (0, 0) 0 green -- t2 <- makeTurtle' (0, 0) 90 red -- (t1 >/> forward 90) >!> (t2 >/> forward 90) ---- -- See makeTurtle. makeTurtle' :: Point -> Float -> Color -> WorldCommand Turtle -- | Clears all drawings form the canvas. Does not alter any turtle's -- state. clear :: WorldCommand () -- | World sleeps for a given amount of time in seconds before running the -- next command. -- -- This is the WorldComamnd variant of wait. -- -- A negative value will be clamped to 0. sleep :: Float -> WorldCommand () -- | Move the turtle forward by the specified distance, in the -- direction the turtle is headed. forward :: Float -> TurtleCommand () -- | Shorthand for forward. fd :: Float -> TurtleCommand () -- | Move the turtle backward by the specified distance, in the -- direction the turtle is headed. backward :: Float -> TurtleCommand () -- | Shorthand for backward. bk :: Float -> TurtleCommand () -- | Turn a turtle left by the given degrees amount. left :: Float -> TurtleCommand () -- | Shorthand for left. lt :: Float -> TurtleCommand () -- | Turn a turtle right by the given degrees amount. right :: Float -> TurtleCommand () -- | Shorthand for right. rt :: Float -> TurtleCommand () -- | Draw a circle with a given radius. The center is -- radius units left of the turtle if positive. -- Otherwise radius units right of the turtle if -- negative. -- -- The circle is drawn in an anticlockwise direction if the radius is -- positive, otherwise, it is drawn in a clockwise direction. -- -- Circle is an alias for circle r = arc r 360. circle :: Float -> TurtleCommand () -- | Draw an arc with a given radius. The center is -- radius units left of the turtle if positive. -- Otherwise radius units right of the turtle if -- negative. -- -- The arc is drawn in an anticlockwise direction if the radius is -- positive, otherwise, it is drawn in a clockwise direction. arc :: Float -> Float -> TurtleCommand () -- | Sets the turtle's position to the new given value. -- -- This command does not animate, nor is a line drawn between the old -- position and the new position. -- -- Use goto if you want a drawn line. -- -- This command does not affect the turtle's heading. jump :: Point -> TurtleCommand () -- | Sets the turtle's position to the new given value. -- -- This command will animate. A line will be drawn between the turtle's -- old position and the new set position if the turtle's pen is down. -- -- Use jump if you do not want a drawn line. -- -- This command does not affect the turtle's heading. goto :: Point -> TurtleCommand () -- | Warps the turtle to its starting position (0, 0) and resets -- the orientation to north (90 degrees). No line is -- drawn moving the turtle. home :: TurtleCommand () -- | Sets the turtle's heading. See heading. setHeading :: Float -> TurtleCommand () -- | Sets the turtle's speed. See speed. setSpeed :: Float -> TurtleCommand () -- | Sets the turtle's rotation speed. See rotationSpeed. setRotationSpeed :: Float -> TurtleCommand () -- | Turtle waits for a given amount of time in seconds before continuing -- with the next command. -- -- This is the TurtleCommand variant of sleep. -- -- A negative value will be clamped to 0. wait :: Float -> TurtleCommand () -- | Repeats the same command several times. -- -- Example: -- --
-- repeatFor 4 $ do -- forward 50 -- right 90 ---- -- This is an alias of replicateM_. -- -- That is: -- --
-- repeatFor = replicateM_ --repeatFor :: Int -> TurtleCommand a -> TurtleCommand () -- | Stamp a copy of the turtle shape onto the canvas at the current turtle -- position. stamp :: TurtleCommand () -- | Gets the turtle's representation as a Picture. representation :: TurtleCommand Picture -- | Writes a string to screen at the turtle's position. -- -- The written text color will match turtle pen color. -- -- This is eqivelent to: -- --
-- label = label' 0.2 --label :: String -> TurtleCommand () -- | Variant of label which takes a scale argument to scale the size -- of the drawn text. label' :: Float -> String -> TurtleCommand () -- | Returns the turtle's current position. Default (starting) position is -- (0, 0). position :: TurtleCommand Point -- | Returns the turtle's heading. -- -- 0 is along the positive x-axis, going anticlockwise. So: -- --
-- import Graphics.WorldTurtle -- import qualified Graphics.Gloss.Data.Picture as G -- -- myCommand :: TurtleCommand () -- myCommand = do -- setPenColor red -- setRepresentation (G.color red $ G.circleSolid 10) -- forward 90 --setRepresentation :: Picture -> TurtleCommand () -- | Sets the turtle's visibility to visible. -- -- The turtle's representation will be drawn to canvas. -- -- See visible and setInvisible. setVisible :: TurtleCommand () -- | Sets the turtle's visibility to invisible. -- -- The turtle's representation will not be drawn to canvas. -- -- See visible and setVisible. setInvisible :: TurtleCommand () -- | 0 degrees. east :: Float -- | 90 degrees. north :: Float -- | 180 degrees. west :: Float -- | 270 degrees. south :: Float -- | Graphics.WorldTurtle is a module for writing and rendering -- turtle graphics in Haskell. -- -- Take a look at the examples on Github! module Graphics.WorldTurtle -- | Takes a TurtleCommand and executes the command on an implicitly -- created turtle that starts at position (0, 0) with heading -- north. -- -- This is a convenience function written in terms of runWorld as: -- --
-- runTurtle c = runWorld $ makeTurtle >>= run c ---- -- See also: makeTurtle. runTurtle :: TurtleCommand () -> IO () -- | Variant of runTurtle which takes an additional background color -- parameter. runTurtle' :: Color -> TurtleCommand () -> IO () -- | A TurtleCommand represents an instruction to execute on a -- turtle. 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 (>>). -- -- For example, to draw an equilateral triangle using do notation: -- --
-- drawTriangle :: TurtleCommand () -- drawTriangle = do -- setHeading east -- forward 100 -- left 120 -- forward 100 -- left 120 -- forward 100 ---- -- Which would produce: -- data TurtleCommand a -- | runWorld takes a WorldCommand and produces the animation -- in a new window! -- --
-- myCommand :: Turtle -> WorldCommand Float -- myCommand t = do -- (x, _) <- run position t -- return x ---- -- Or to create a command that accepts a turtle and draws a right angle: -- --
-- myCommand :: Turtle -> WorldCommand () -- myCommand = run $ forward 10 >> right 90 >> forward 10 --run :: TurtleCommand a -> Turtle -> WorldCommand a -- | This is an infix version of run where the arguments are -- swapped. -- -- We take a turtle and a command to execute on the turtle. The result of -- the computation is returned wrapped in a WorldCommand. -- -- To create a turtle and draw a right-angle: -- --
-- myCommand :: WorldCommand () -- myCommand = do -- t <- makeTurtle -- t >/> do -- forward 10 -- right 90 -- forward 10 --(>/>) :: Turtle -> TurtleCommand a -> WorldCommand a infixl 4 >/> -- | While WorldCommands can be combined with (>>) to -- produce sequential instructions, we can also use the parallel -- animation operator (>!>) to achieve parallel -- instructions. That is: animate two turtles at time! -- -- Here is an example: -- --
-- import Graphics.WorldTurtle -- -- main :: IO () -- main = runWorld $ do -- t1 <- makeTurtle' (0, 0) north green -- t2 <- makeTurtle' (0, 0) north red -- -- -- Draw the anticlockwise and clockwise circles in sequence. -- t1 >/> circle 90 >> t2 >/> circle (-90) -- -- clear -- -- -- Draw the anticlockwise and clockwise circles in parallel. -- t1 >/> circle 90 >!> t2 >/> circle (-90) ---- -- Which would produce an animation like this -- -- -- Note that (>!>) is an alias for bindM2, and is -- defined as: -- --
-- (>!>) = bindM2 (const . return) --(>!>) :: WorldCommand () -> WorldCommand () -> WorldCommand () infixl 3 >!>