draw points and lines with given coordinates using haskell diagrams

987 views Asked by At

I have a list of points with absolute x and y coordinates:

points :: [(1,1), (2,2), (3,3), (-105, -42.42)]

And a list of edges as tuple of points:

edges :: [((1,1), (2,2)), ((1,1),(-105, -42.42)), ((3,3), (-105, -42.42))]

I now want to draw this using the diagrams package, using circles for the nodes and lines for the edges. I have found the type Located which should provide this functionality. On the other hand there is the atPoints function, however they don't seem to achieve the same thing (atPoints only moves the local origin).

What would be the idomatic way to achieve this? How do I use the Located type?

1

There are 1 answers

2
leftaroundabout On BEST ANSWER

You can use moveTo to, well, move some object to an absolute-specified position. And to draw edges between absolutely-defined vertices, you can use fromVertices. Note that both don't accept tuples as arguments but points – but it's easy enough to convert to these.

> :m +Diagrams.Prelude Graphics.Dynamic.Plot.R2
> plotWindow $ (shapePlot <$> [circle 1 & moveTo (p2 p) | p<-points]
                           ++ [fromVertices [p2 p, p2 q] | (p,q)<-edges]) 
            ++ [dynamicAxes]

Auto-coloured view with dynamic axes

To combine these elements to a single diagram, you can just use the monoid instance, i.e. mconcat the list.

plotWindow [shapePlot . mconcat $ [circle 1 & fcA transparent & moveTo (p2 p) | p<-points] ++ [fromVertices [p2 p, p2 q] | (p,q)<-edges]]

Flat combined view