module Math.Sym.Plot
(
Plot, plotPerm, plotPerms, savePlot
) where
import Diagrams.Prelude
import Diagrams.Backend.Cairo
import Data.Perm (toList)
import Math.Sym
type Plot = Diagram Cairo R2
grid :: Int -> Plot
grid n = stripes `atop` rotateBy (1/4) stripes
where
stripes = centerXY . hcat' with {sep = 1} . replicate n . vrule $ fromIntegral n
plotPerm :: Permutation a => a -> Plot
plotPerm w = centerXY plot `atop` grid n `atop` background
where
n = size w
v = map fromIntegral . toList $ st w
plot = position $ zip (zipWith (curry p2) [0..] v) dots
dots = repeat $ circle 0.2 # fc black
background = square (fromIntegral n) # fc whitesmoke # lc white
chunk :: Int -> [a] -> [[a]]
chunk k [] = []
chunk k xs = ys : chunk k zs where (ys, zs) = splitAt k xs
plotPerms :: Permutation a => Int -> [a] -> Plot
plotPerms cols = vcat . map hcat . chunk cols . map (pad 1.1 . plotPerm)
savePlot :: Double -> String -> Plot -> IO ()
savePlot width filePath = renderCairo filePath (Width width)