Diagramming a path from a list of vectors
In this recipe, we will use the diagrams
package to draw a path from driving directions. We simply categorize all possible travel headings into eight cardinal directions with an associated distance. We use directions provided by Google Maps in the following screenshot and reconstruct the directions from a text file:
Getting ready
Install the diagrams
library as follows:
$ cabal install diagrams
Create a text file called input.txt
that contains one of the eight cardinal directions followed by the distance, with each step separated by a new line:
$ cat input.txt N 0.2 W 0.1 S 0.6 W 0.05 S 0.3 SW 0.1 SW 0.2 SW 0.3 S 0.3
How to do it…
- Import the relevant libraries as follows:
{-# LANGUAGE NoMonomorphismRestriction #-} import Diagrams.Prelude import Diagrams.Backend.SVG.CmdLine (mainWith, B)
- Draw a line-connected path from a list of vectors as follows:
drawPath :: [(Double, Double)] -> Diagram B R2 drawPath vectors = fromOffsets . map r2 $ vectors...