Copyright | (c) Archibald Neil MacDonald 2020 |
---|---|
License | BSD3 |
Maintainer | FortOyer@hotmail.co.uk |
Stability | experimental |
Portability | POSIX |
Safe Haskell | None |
Language | Haskell2010 |
This module contains all commands used to create, move and manipulate a turtle.
Synopsis
- data Turtle
- makeTurtle :: TurtleCommand Turtle
- makeTurtle' :: Point -> Float -> Color -> TurtleCommand Turtle
- type Point = (Float, Float)
- forward :: Float -> Turtle -> TurtleCommand ()
- fd :: Float -> Turtle -> TurtleCommand ()
- backward :: Float -> Turtle -> TurtleCommand ()
- bk :: Float -> Turtle -> TurtleCommand ()
- left :: Float -> Turtle -> TurtleCommand ()
- lt :: Float -> Turtle -> TurtleCommand ()
- right :: Float -> Turtle -> TurtleCommand ()
- rt :: Float -> Turtle -> TurtleCommand ()
- circle :: Float -> Float -> Turtle -> TurtleCommand ()
- goto :: Point -> Turtle -> TurtleCommand ()
- setPosition :: Point -> Turtle -> TurtleCommand ()
- home :: Turtle -> TurtleCommand ()
- setHeading :: Float -> Turtle -> TurtleCommand ()
- setSpeed :: Float -> Turtle -> TurtleCommand ()
- setRotationSpeed :: Float -> Turtle -> TurtleCommand ()
- stamp :: Turtle -> TurtleCommand ()
- representation :: Turtle -> TurtleCommand Picture
- position :: Turtle -> TurtleCommand Point
- heading :: Turtle -> TurtleCommand Float
- speed :: Turtle -> TurtleCommand Float
- rotationSpeed :: Turtle -> TurtleCommand Float
- penColor :: Turtle -> TurtleCommand Color
- penDown :: Turtle -> TurtleCommand Bool
- penSize :: Turtle -> TurtleCommand Float
- visible :: Turtle -> TurtleCommand Bool
- setPenColor :: Color -> Turtle -> TurtleCommand ()
- setPenDown :: Bool -> Turtle -> TurtleCommand ()
- setPenSize :: Float -> Turtle -> TurtleCommand ()
- setRepresentation :: Picture -> Turtle -> TurtleCommand ()
- setVisible :: Bool -> Turtle -> TurtleCommand ()
- clear :: TurtleCommand ()
- sleep :: Float -> TurtleCommand ()
- east :: Float
- north :: Float
- west :: Float
- south :: Float
Creating a turtle.
The Turtle that is drawn on the canvas! Create a new turtle using
makeTurtle
.
:: Point | Initial position of the turtle. |
-> Float | Initial heading of the turtle. |
-> Color | Color of the turtle and the turtle's pen. |
-> TurtleCommand Turtle | The generated 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 :: TurtleCommand () myCommand = do t1 <- makeTurtle' (0, 0) 0 green t2 <- makeTurtle' (0, 0) 90 red forward 90 t1 <|> forward 90 t2
See makeTurtle
.
Movement commands.
:: Float | Distance to move the turtle. |
-> Turtle | The turtle to move. |
-> TurtleCommand () |
Move the turtle forward by the specified distance
, in the direction the
turtle is headed.
:: Float | Distance to move the turtle. |
-> Turtle | The turtle to move. |
-> TurtleCommand () |
Move the turtle backward by the specified distance
, in the direction the
turtle is headed.
:: Float | Rotation amount to apply to turtle. |
-> Turtle | The turtle to rotate. |
-> TurtleCommand () |
Turn a turtle left by the given degrees amount.
:: Float | Rotation amount to apply to turtle. |
-> Turtle | The turtle to rotate. |
-> TurtleCommand () |
Turn a turtle right by the given degrees amount.
:: Float | Radius of the circle. |
-> Float | Angle to travel in degrees.
For example: |
-> Turtle | Turtle to move in a circle. |
-> 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.
:: Point | Position to warp to. |
-> Turtle | Turtle to modify. |
-> TurtleCommand () |
Warps the turtle to a new position. The turtle jumps to this new position with no animation. If the pen is down then a line is drawn.
This does not affect the turtle's heading.
setPosition :: Point -> Turtle -> TurtleCommand () Source #
Alias of goto
.
home :: Turtle -> TurtleCommand () Source #
Warps the turtle to its starting position (0, 0)
and resets the
orientation to North
(90 degrees). No line is drawn moving the turtle.
:: Float | Heading to apply. |
-> Turtle | Turtle to modify. |
-> TurtleCommand () |
Sets the turtle's heading. See heading
.
:: Float | New speed. |
-> Turtle | Turtle to modify. |
-> TurtleCommand () |
Sets the turtle's speed.
See speed
.
:: Float | New rotation speed. |
-> Turtle | Turtle to modify. |
-> TurtleCommand () |
Sets the turtle's rotation speed.
See rotationSpeed
.
Styling commands.
:: Turtle | The turtle with the shape to be copied. |
-> TurtleCommand () |
Stamp a copy of the turtle shape onto the canvas at the current turtle position.
:: Turtle | Turtle to query. |
-> TurtleCommand Picture |
Gets the turtle's representation as a Picture
.
Query turtle's state.
:: Turtle | Turtle to query. |
-> TurtleCommand Point | Returned current point. |
Returns the turtle's current position.
Default (starting) position is (0, 0)
.
:: Turtle | Turtle to query. |
-> TurtleCommand Float | Returned heading as angle in degrees. |
Returns the turtle's heading.
0
is along the positive x-axis, going anticlockwise. So:
- East is
0
degrees. - North is
90
degrees. - West is
180
degrees. - South is
270
degrees.
The default heading is North (90
degrees).
:: Turtle | Turtle to query. |
-> TurtleCommand Float | Speed of turtle. |
Returns the turtle's current speed.
Speed is is distance
per second.
A speed of 0 is equivalent to no animation being performed and instant
movement.
The default value is 200
.
:: Turtle | Turtle to query. |
-> TurtleCommand Float | Rotation speed of turtle. |
Returns the turtle's current rotation speed.
Rotation speed is is the speed in seconds it takes to do a full revolution.
A speed of 0 is equivalent to no animation being performed and instant
rotation.
The default value is 20
.
:: Turtle | Turtle to query. |
-> TurtleCommand Color | Returned current pen color. |
Returns the turtle's pen color.
The color of the turtle's pen.The default color is black
.
:: Turtle | Turtle to query. |
-> TurtleCommand Bool | True if pen is down, false if not. |
Returns whether the turtle's pen is down.
When the turtle's pen is down it will draw a line when it moves.
The default value is True
.
:: Turtle | Turtle to query. |
-> TurtleCommand Float | Size of turtle's pen. |
Returns the turtle's pen size.
Defaults to 2
.
:: Turtle | Turtle to query. |
-> TurtleCommand Bool |
|
Returns whether the turtle is visible.
The default value is True
.
Set rendering state.
:: Color | New pen color to apply |
-> Turtle | Turtle to modify. |
-> TurtleCommand () |
Set the turtle's pen color.
See penColor
.
:: Bool | New state for pen flag. True for down. False for up. |
-> Turtle | Turtle to modify. |
-> TurtleCommand () |
Sets the turtle's pen to down or up.
See penDown
.
:: Float | New size for turtle's pen. |
-> Turtle | Turtle to modify. |
-> TurtleCommand () |
Sets the turtle's pen size.
See penSize
.
:: Picture | Picture to apply. |
-> Turtle | Turtle to modify. |
-> TurtleCommand () |
Sets the turtle's representation to a Picture
.
See representation
.
For example, to set the turtle as a red circle:
import Graphics.WorldTurtle import qualified Graphics.Gloss.Data.Picture as G myCommand :: TurtleCommand () myCommand = do t <- makeTurtle setPenColor red t setRepresentation (G.color red $ G.circleSolid 10) t forward 90 t
:: Bool | New state for visible flag. |
-> Turtle | Turtle to modify. |
-> TurtleCommand () |
Sets the turtle's visibility.
See visible
.
Canvas commands.
clear :: TurtleCommand () Source #
Clears all drawings form the canvas. Does not alter any turtle's state.
sleep :: Float -> TurtleCommand () Source #
Sleep for a given amount of time in seconds. When sleeping no animation
runs. A negative value will be clamped to 0
.