0

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?

fuji
  • 1,173
  • 1
  • 10
  • 27
  • 1
    What have you tried so far? Have a look at http://projects.haskell.org/diagrams/doc/quickstart.html Also: why do you want to use `diagrams` which has no global coordinate system for this task? Might be easier to generate an SVG directly... – mb21 Feb 15 '16 at 17:22
  • @mb21: diagrams does have a “global coordinate system”. Only, it's properly treated as an affine space instead of a vector space, and it's not _that_ global – any nesting of objects can override it by applying transformations. Still, it's perfectly possible to draw diagrams with absolute coordinates, though of course the real power of the library are the tools which allow you to get along _without_ needing to specify such coordinates. – leftaroundabout Feb 15 '16 at 19:09
  • @mb21 I chose ``diagrams`` because it looks pretty much like batteries included, but I am not fixated on this solution. I guess for writing SVGs directly, I would use the ``blaze-svg`` package? I think this is what ``diagrams`` is using on the SVG backend. – fuji Feb 16 '16 at 06:55

1 Answers1

2

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

leftaroundabout
  • 117,950
  • 5
  • 174
  • 319
  • how cool is the `plotWindow` function - diagrams is always full of surprises : +1 for that (also for the answer) – epsilonhalbe Feb 15 '16 at 22:01
  • Thank you very much for your answer, I did not know the ``moveTo`` function. The list comprehension is a really cool way to do handle multiple objects. – fuji Feb 16 '16 at 06:57