Particularly useful for programmatic generation of large numbers of diagrams. For instance Bracelets. Backends exist for SVG, PNG, PDF, HTML 5 canvas and GTK.
Functions are defined with a type signature:
example :: Diagram B
defines a function to return a Diagram B
. Both Diagram
and B
are defined by the diagrams module.
Functions are implemented as such:
example = circle 1
example :: Diagram B
example = circle 1 ||| square 1 ||| pentagon 1
See TwoD-Shapes documentation for full details.
Diagrams doesn't conflate 2d points with 2d vectors! Many, many graphics APIs do -- and that annoys me.
r2 (3, 3) -- a vector
p2 (3, 3) -- a point
Rather than side-by side positioning we can:
example :: Diagram B
example = beside (r2 (0.5, 0.5)) (circle 1) (square 1)
example3 :: Diagram B
example3 = (circle 2 === square 1) ||| pentagon 3
example2 :: Diagram B
example2 = circle 2 # lc blue ||| pentagon 3
#
is simply a postfix function so circle 1 # lc blue
is lc blue (circle 1)
example4 :: Diagram B
example4 = ((circle 2 # lc blue === square 1) # centerY ||| pentagon 3 # centerY)
example5 :: Diagram B
example5 = hcat [(circle 2 # lc blue === square 1)
, pentagon 3, triangle 4]
example6 :: Diagram B
example6 = vcat [circle 2 # lc blue
, square 1
, pentagon 3]
> example6 :: Diagram B
> example6 = vcat [circle 2 # lc blue # showEnvelope' (with & eColor .~ green)
> , square 1
> , pentagon 3] # centerY # showEnvelope' (with & eColor .~ red)
We'll create a tree and visualise it's recursive structure.
The following is one way of implementing a binary tree.
data Tree = Leaf | Branch Tree Tree
We'll visualise each leaf as a square:
square 1
We can use haskell's pattern matching to build our visualisation.
boxes :: Tree -> Diagram B
boxes Leaf = ...
boxes (Branch l r) = ...
Live demo from here on -- I shouldn't do this, but here we go.