worldturtle-0.3.1.0: LOGO-like Turtle graphics with a monadic interface.
Copyright(c) Archibald Neil MacDonald 2020
LicenseBSD3
Maintainerarchibaldnmac@gmail.com
Stabilityexperimental
PortabilityPOSIX
Safe HaskellNone
LanguageHaskell2010

Graphics.WorldTurtle.Commands

Description

This module contains all commands used to create, move and manipulate a turtle.

Synopsis

Types

data Turtle Source #

The Turtle that is drawn on the canvas! Create a new turtle using makeTurtle.

Instances

Instances details
Eq Turtle Source # 
Instance details

Defined in Graphics.WorldTurtle.Internal.Sequence

Methods

(==) :: Turtle -> Turtle -> Bool

(/=) :: Turtle -> Turtle -> Bool

Ord Turtle Source # 
Instance details

Defined in Graphics.WorldTurtle.Internal.Sequence

Methods

compare :: Turtle -> Turtle -> Ordering

(<) :: Turtle -> Turtle -> Bool

(<=) :: Turtle -> Turtle -> Bool

(>) :: Turtle -> Turtle -> Bool

(>=) :: Turtle -> Turtle -> Bool

max :: Turtle -> Turtle -> Turtle

min :: Turtle -> Turtle -> Turtle

type Point = (Float, Float) #

A point on the x-y plane.

WorldCommand commands.

Creating a turtle.

makeTurtle :: WorldCommand Turtle Source #

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' Source #

Arguments

:: Point

Initial position of the turtle.

-> Float

Initial heading of the turtle.

-> Color

Color of the turtle and the turtle's pen.

-> WorldCommand 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 :: WorldCommand ()
 myCommand = do
   t1 <- makeTurtle' (0, 0)  0 green
   t2 <- makeTurtle' (0, 0) 90 red
   (t1 >/> forward 90) >!> (t2 >/> forward 90)

See makeTurtle.

Canvas commands.

clear :: WorldCommand () Source #

Clears all drawings form the canvas. Does not alter any turtle's state.

sleep Source #

Arguments

:: Float

Amount of time to sleep in seconds.

-> 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.

TurtleCommand commands.

Movement commands.

forward Source #

Arguments

:: Float

Distance to move the turtle.

-> TurtleCommand () 

Move the turtle forward by the specified distance, in the direction the turtle is headed.

fd :: Float -> TurtleCommand () Source #

Shorthand for forward.

backward Source #

Arguments

:: Float

Distance to move the turtle.

-> TurtleCommand () 

Move the turtle backward by the specified distance, in the direction the turtle is headed.

bk :: Float -> TurtleCommand () Source #

Shorthand for backward.

left Source #

Arguments

:: Float

Rotation amount to apply to turtle.

-> TurtleCommand () 

Turn a turtle left by the given degrees amount.

lt :: Float -> TurtleCommand () Source #

Shorthand for left.

right Source #

Arguments

:: Float

Rotation amount to apply to turtle.

-> TurtleCommand () 

Turn a turtle right by the given degrees amount.

rt :: Float -> TurtleCommand () Source #

Shorthand for right.

circle Source #

Arguments

:: Float

Radius of the circle.

-> 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.

arc Source #

Arguments

:: Float

Radius of the circle.

-> Float

Angle to travel in degrees. For example: 360 for a full circle or 180 for a semicircle.

-> 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.

jump Source #

Arguments

:: Point

Position to warp to.

-> 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.

goto Source #

Arguments

:: Point

Position to warp to.

-> 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.

home :: 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.

setHeading Source #

Arguments

:: Float

Heading to apply.

-> TurtleCommand () 

Sets the turtle's heading. See heading.

setSpeed Source #

Arguments

:: Float

New speed.

-> TurtleCommand () 

Sets the turtle's speed. See speed.

setRotationSpeed Source #

Arguments

:: Float

New rotation speed.

-> TurtleCommand () 

Sets the turtle's rotation speed. See rotationSpeed.

wait Source #

Arguments

:: Float

Amount of time to wait in seconds.

-> 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.

repeatFor Source #

Arguments

:: Int

Number of times to repeat a command.

-> TurtleCommand a

Command to repeat.

-> 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_

Styling commands.

stamp :: TurtleCommand () Source #

Stamp a copy of the turtle shape onto the canvas at the current turtle position.

representation :: TurtleCommand Picture Source #

Gets the turtle's representation as a Picture.

label Source #

Arguments

:: String

String to write to screen.

-> TurtleCommand () 

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' Source #

Arguments

:: Float

Scale of text to draw.

-> String

String to write to screen.

-> TurtleCommand () 

Variant of label which takes a scale argument to scale the size of the drawn text.

Query turtle's state.

position Source #

Arguments

:: TurtleCommand Point

Returned current point.

Returns the turtle's current position. Default (starting) position is (0, 0).

heading Source #

Arguments

:: 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).

speed Source #

Arguments

:: 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.

rotationSpeed Source #

Arguments

:: 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 720.

penColor Source #

Arguments

:: TurtleCommand Color

Returned current pen color.

Returns the turtle's pen color. The color of the turtle's pen.The default color is black.

penDown Source #

Arguments

:: 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.

penSize Source #

Arguments

:: TurtleCommand Float

Size of turtle's pen.

Returns the turtle's pen size. Defaults to 2.

visible Source #

Arguments

:: TurtleCommand Bool

True if turtle is visible, False if not.

Returns whether the turtle is visible. The default value is True.

Mutate turtle's state.

branch :: TurtleCommand a -> TurtleCommand a Source #

Given a command, runs the command, then resets the turtle's state back to what the state was before the command was run.

setPenColor Source #

Arguments

:: Color

New pen color to apply

-> TurtleCommand () 

Set the turtle's pen color. See penColor.

setPenDown :: TurtleCommand () Source #

Sets the turtle's pen to down. Turtle will draw as it moves. See penDown and setPenUp.

setPenUp :: TurtleCommand () Source #

Sets the turtle's pen to up. Turtle will not draw as it moves. See penDown and setPenDown.

setPenSize Source #

Arguments

:: Float

New size for turtle's pen.

-> TurtleCommand () 

Sets the turtle's pen size. See penSize.

setRepresentation Source #

Arguments

:: Picture

Picture to apply.

-> 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
   setPenColor red
   setRepresentation (G.color red $ G.circleSolid 10)
   forward 90

setVisible :: TurtleCommand () Source #

Sets the turtle's visibility to visible.

The turtle's representation will be drawn to canvas.

See visible and setInvisible.

setInvisible :: TurtleCommand () Source #

Sets the turtle's visibility to invisible.

The turtle's representation will not be drawn to canvas.

See visible and setVisible.

Common constants

east :: Float Source #

0 degrees.

north :: Float Source #

90 degrees.

west :: Float Source #

180 degrees.

south :: Float Source #

270 degrees.