Introduction

Arrows come in many shapes and sizes and diagrams provides a wide variety of flexible and extensible tools for creating and using arrows. The diagram below gives a small taste of some of the different arrows that can be created easily with diagrams. The Diagrams.TwoD.Arrow module, along with Diagrams.TwD.Arrowheads, provides a collection of functions and options used to make arrows.

Optional and named parameters

Most of the arrow functions take an opts parameter (see Faking optional named parameters) of type ArrowOpts, these functions typically have companion functions that use a default set of ArrowOpts. For example the functions arrow' and arrow. The former takes an opts parameter and the latter does not. In this tutorial whenever we mention a function with a single quote (') at the end, there is a sister function without the quote that uses a default set of options.

Scale invariance

Arrowheads and -tails are the canonical example of scale invariant objects: they are not affected by scaling (though they are affected by other transformations such as rotation and translation). The scale-invariance section of the user manual has a good example showing why scale-invariance is necessary for the creation of arrowheads; detailed documentation explaining scale invariant objects is in Diagrams.TwoD.Transform.ScaleInv. The most important consequence for day-to-day diagramming with arrows is that arrowheads and -tails do not contribute to the envelope of an arrow (arrow shafts, on the other hand, do).

Arrowheads and tails do not contribute to the envelope of an arrow!

Connecting Points

A typical use case for an arrow is to connect two points, having an arrow pointing from one to the other. The function arrowBetween (and its cousin arrowBetween') connects two points.

> sPt = p2 (0.20, 0.20)
> ePt = p2 (2.85, 0.85)
>
> -- We use small blue and red circles to mark the start and end points.
> dot  = circle 0.02 # lw 0
> sDot = dot # fc blue # moveTo sPt
> eDot = dot # fc red  # moveTo ePt
>
> example = ( sDot <> eDot <> arrowBetween sPt ePt)
>           # centerXY # pad 1.1

1. Create a diagram which contains a circle of radius 1 with an arrow connecting the points on the circumference at 45 degrees and 180 degrees.

ArrowOpts

All of the arrow creation functions have a primed variant (e.g. arrowBetween and arrowBetween') which takes an additional opts parameter of type ArrowOpts. The opts record is the primary means of customizing the look of the arrow. It contains a substantial collection of options to control all of the aspects of an arrow. Here is the definition for reference:

> data ArrowOpts = ArrowOpts
>   , _arrowTail  :: ArrowHT
>   , _arrowShaft :: Trail R2
>   , _tailSize   :: Double
>   , _tailGap    :: Double
>   , _headStyle  :: Style R2
>   , _tailStyle  :: Style R2
>   , _shaftStyle :: Style R2
>   }

Don't worry if some of the field types in this record are not yet clear, we will walk through each field and occasionally point to the API reference for material that we don't cover in this tutorial.

The arrowHead and arrowTail fields contain information needed to construct the head and tail of the arrow, the most important aspect being the shape. So, for example, if we set arrowHead=spike and arrowTail=quill,

> arrowBetween' (with & arrowHead .~ spike & arrowTail .~ quill) sPt ePt

then the arrow from the previous example looks like this:

The Arrowheads package exports a number of standard arrowheads including, tri, dart, spike, thorn, missile, and noHead, with dart being the default. Also available are companion functions like arrowheadDart that allow finer control over the shape of a dart style head. For tails, in addition to quill are block and noTail. Again for more control are functions like, arrowtailQuill. Finally, any of the standard arrowheads can be used as tails by appending a single quote, so for example:

> arrowBetween' (with & arrowHead .~ thorn & arrowTail .~ thorn') sPt ePt

yields:

The shaft

The shaft of an arrow can be any arbitrary Trail R2 in addition to a simple straight line. For example, an arc makes a perfectly good shaft. The length of the trail is irrelevant, as the arrow is scaled to connect the starting point and ending point regardless of the length of the shaft. Modifying our example with the following code will make the arrow shaft into an arc:

> shaft = arc (0 @@ turn) (1/2 @@ turn)
>
> example = ( sDot <> eDot
>          <> arrowBetween' (with & arrowHead .~ spike & arrowTail .~ spike'
>                                 & arrowShaft .~shaft) sPt ePt)
>           # centerXY # pad 1.1

Arrows with curved shafts don't always render the way our intuition may lead us to expect. One could reasonably expect that the arc in the above example would produce an arrow curving upwards, not the downwards-curving one we see. To understand what's going on, imagine that the arc is Located. Suppose the arc goes from the point $$(0,0)$$ to $$(-1,0)$$. This is indeed an upwards curving arc with origin at $$(0,0)$$. Now suppose we want to connect points $$(0,0)$$ and $$(1,0)$$. We attach the arrow head and tail and rotate the arrow about its origin at $$(0,0)$$ until the tip of the head is touching $$(1,0)$$. This rotation flips the arrow vertically.

In order to get the arrow to curve upwards we might initially think we could create the shaft reversing the order of the angles, using arc (1/2 @@ turn) 0, but this won't work either, as it creates a downwards curving arc from, say, $$(0,0)$$ to $$(1,0)$$ that does not need to be rotated. The only way to achieve the desired result of making the arrow pointing from $$(0,0)$$ to $$(1,0)$$ curve upwards is to reverse the trail:

> shaft = arc (0 @@ turn) (1/2 @@ turn) # reverseTrail

If an arrow shaft does not appear as you expect, then try using reverseTrail.

Here are some exercises to try.

Construct each of the following arrows pointing from $$(1,1)$$ to $$(3,3)$$ inside a square with side $$4$$.

1. A straight arrow with no head and a spike shaped tail.

2. An arrow with a $$45$$ degree arc for a shaft, triangles for both head and tail, curving downwards.

3. The same as above, only now make it curve upwards.

Size, and gaps

The fields headSize and tailSize are for setting the size of the head and tail. The head and tail size are specified as the diameter of an imaginary circle that would circumscribe the head or tail. The default value is 0.3. The headGap and tailGap options are also fairly self explanatory: they leave space at the end or beginning of the arrow. Take a look at their effect in the following example. The default gaps are 0.

> sPt = p2 (0.20, 0.50)
> mPt = p2 (1.50, 0.50)
> ePt = p2 (2.80, 0.50)
>
> dot  = circle 0.02 # lw 0
> sDot = dot # fc blue  # moveTo sPt
> mDot = dot # fc green # moveTo mPt
> eDot = dot # fc red   # moveTo ePt
>
>
> leftArrow  = arrowBetween' (with & arrowHead .~ missile & arrowTail .~ spike'
>                                  & headSize .~ 0.15 & tailSize .~ 0.1
>                                  & shaftStyle %~ lw 0.02
>                                  & headGap .~ 0.05) sPt mPt
> rightArrow = arrowBetween' (with & arrowHead .~ tri & arrowTail .~ dart'
>                                  & headSize .~ 0.25 & tailSize .~ 0.2
>                                  & shaftStyle %~ lw 0.015
>                                  & tailGap .~ 0.1) mPt ePt
>
> example = ( sDot <> mDot <> eDot <> leftArrow <> rightArrow)
>           # centerXY # pad 1.1

The style options

By default, arrows are drawn using the current line color (including the head and tail). In addition, the shaft styling is taken from the current line styling attributes. For example:

> example = mconcat
>   [ square 2
>   , arrowAt origin unitX
>     # lc blue
>   ]
>   # dashing [0.05, 0.05] 0
>   # lw 0.03

The colors of the head, tail, and shaft may be individually overridden using headColor, tailColor, and shaftColor. More generally, the styles are controlled using headStyle, tailStyle, and shaftStyle. For example:

> dashedArrow = arrowBetween' (with & arrowHead .~ dart & arrowTail .~ spike'
>                                   & headColor .~ blue & tailColor .~ orange
>                                   & shaftStyle %~ dashing [0.04, 0.02] 0
>                                   . lw 0.01) sPt ePt
>

Note that when setting a style, one must generally use the %~ operator in order to apply something like dashing [0.04, 0.02] 0 which is a function that changes the style.

By default, the ambient line color is used for the head, tail, and shaft of an arrow. However, when setting the styles individually, the fill color should be used for the head and tail, and line color for the shaft. This issue can be avoided entirely by using, for example, headColor .~ blue to set the color instead of headStyle %~ fc blue.

Placing an arrow at a point

Sometimes we prefer to specify a starting point and vector from which the arrow takes its magnitude and direction. The arrowAt' and arrowAt functions are useful in this regard. The example below demonstrates how we might create a vector field using the arrowAt' function.

> locs   = [(x, y) | x <- [0.1, 0.3 .. 3.25], y <- [0.1, 0.3 .. 3.25]]
>
> -- create a list of points where the vectors will be place.
> points = map p2 locs
>
> -- The function to use to create the vector field.
> vectorField (x, y) = r2 (sin (y + 1), sin (x + 1))
>
> arrows = map arrowAtPoint locs
>
> arrowAtPoint (x, y) = arrowAt' opts (p2 (x, y)) (sL *^ vf) # alignTL
>   where
>     vf   = vectorField (x, y)
>     m    = magnitude $vectorField (x, y) > > -- Head size is a function of the length of the vector > -- as are tail size and shaft length. > hs = 0.08 * m > sW = 0.015 * m > sL = 0.01 + 0.1 * m > opts = (with & arrowHead .~ spike & headSize .~ hs & shaftStyle %~ lw sW) > > field = position$ zip points arrows
> example = ( field # translateY 0.05
>        <> ( square 3.5 # fc whitesmoke # lw 0.02 # alignBL))
>         # scaleX 2

Try using the above code to plot some other interesting vector fields.

Connecting diagrams with arrows

The workhorse of the Arrow package is the connect' function. connect' takes an opts record and the names of two diagrams, and places an arrow starting at the origin of the first diagram and ending at the origin of the second (unless gaps are specified).

> s  = square 2 # showOrigin # lw 0.02
> ds = (s # named "1") ||| strutX 3 ||| (s # named "2")
> t  = cubicSpline False (map p2 [(0, 0), (1, 0), (1, 0.2), (2, 0.2)])
>
> example = ds # connect' (with & arrowHead .~ dart & headSize .~ 0.6
>                               & tailSize .~ 0.6 & arrowTail .~ dart'
>                               & shaftStyle %~ lw 0.03 & arrowShaft .~ t) "1" "2"

Connecting points on the trace of diagrams

It is often convenient to be able to connect the points on the Trace of diagrams with arrows. The connectPerim and connectPerim' functions are used for this purpose. We pass connectPerim two names and two angles. The angles are used to determine points on the traces of the two diagrams, determined by shooting a ray from the local origin of each diagram in the direction of the given angle. The generated arrow stretches between these two points. Note that if the names are the same then the arrow connects two points on the same diagram.

> connectPerim "diagram1" "diagram2" (5/12 @@ turn) (1/12 @@ turn)
> connectPerim "diagram" "diagram" (2/12 @@ turn) (4/12 @@ turn)

Here is an example of a finite state automata that accepts real numbers. The code is a bit longer than what we have seen so far, but still very straightforward.

> import Data.Maybe (fromMaybe)
>
> state = circle 1 # lw 0.05 # fc silver
> fState = circle 0.85 # lw 0.05 # fc lightblue <> state
>
> points = map p2 [ (0, 3), (3, 3.4), (6, 3), (5.75, 5.75), (9, 3.75), (12, 3)
>                 , (11.75, 5.75), (3, 0), (2,2), (6, 0.5), (9, 0), (12.25, 0.25)]
>
> ds = [ (text "1" <> state)  # named "1"
>        , label "0-9" 0.5
>        , (text "2" <> state)  # named "2"
>        , label "0-9" 0.5
>        , label "." 1
>        , (text "3" <> fState) # named "3"
>        , label "0-9" 0.5
>        , (text "4" <> state)  # named "4"
>        , label "." 1
>        , label "0-9" 0.5
>        , (text "5" <> fState) # named "5"
>        , label "0-9" 0.5]
>
> label txt size = text txt # fontSize size
>
> states = position (zip points ds)
>
> shaft = arc (0 @@ turn) (1/6 @@ turn)
> shaft' = arc (0 @@ turn) (1/2 @@ turn) # scaleX 0.33
> line = trailFromOffsets [unitX]
>
> arrowStyle1 = (with  & arrowHead  .~ noHead & tailSize .~ 0.3
>                      & arrowShaft .~ shaft  & arrowTail .~ spike'
>                      & tailColor  .~ black)
>
> arrowStyle2  = (with  & arrowHead  .~ noHead &  tailSize .~ 0.3
>                       & arrowShaft .~ shaft' & arrowTail .~ spike'
>                       & tailColor  .~ black)
>
> arrowStyle3  = (with  & arrowHead  .~ noHead & tailSize .~ 0.3
>                       & arrowShaft .~ line & arrowTail .~ spike'
>                       & tailColor  .~ black)
>
> example = states # connectPerim' arrowStyle1
>                                  "2" "1" (5/12 @@ turn) (1/12 @@ turn)
>                  # connectPerim' arrowStyle3
>                                  "4" "1" (2/6 @@ turn) (5/6 @@ turn)
>                  # connectPerim' arrowStyle2
>                                  "2" "2" (2/12 @@ turn) (4/12 @@ turn)
>                  # connectPerim' arrowStyle1
>                                  "3" "2" (5/12 @@ turn) (1/12 @@ turn)
>                  # connectPerim' arrowStyle2
>                                  "3" "3" (2/12 @@ turn) (4/12 @@ turn)
>                  # connectPerim' arrowStyle1
>                                  "5" "4" (5/12 @@ turn) (1/12 @@ turn)
>                  # connectPerim' arrowStyle2
>                                  "5" "5" (-1/12 @@ turn) (1/12 @@ turn)

In the following exercise you can try connectPerim' for yourself.

Create a torus (donut) with $$16$$ curved arrows pointing from the outer ring to the inner ring at the same angle every 1/16 @@ turn.

Add a paragraph about connectOutside and refrence the Symmetry cube in the gallery.