使用 haskell 图表绘制具有给定坐标的点和线

draw points and lines with given coordinates using haskell diagrams

我有一个具有绝对 x 和 y 坐标的点列表:

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

以及作为点元组的边列表:

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

我现在想使用 diagrams 包来绘制它,使用圆作为节点,使用线作为边。我找到了应该提供此功能的类型 Located。另一方面有 atPoints 功能,但它们似乎没有实现相同的功能(atPoints 仅移动本地原点)。

实现这一目标的常用方法是什么?如何使用 Located 类型?

您可以使用 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]

要将这些元素组合成一个图,您可以只使用幺半群实例,即 mconcat 列表。

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