Last active
October 4, 2015 13:18
-
-
Save rjkat/2642664 to your computer and use it in GitHub Desktop.
Divisibility Graph
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{- | |
DivisibilityGraph.hs | |
Uses Graphviz http://www.graphviz.org/ and the haskell library http://projects.haskell.org/graphviz/ | |
to draw divisibility graphs for numbers. | |
To install the haskell package just use | |
$ cabal install graphviz | |
Original idea from David Wilson http://blog.tanyakhovanova.com/?p=262 | |
Inspired by Brent Yorgey's bracelets http://mathlesstraveled.com/2009/11/27/m-bracelets-code/ | |
Usage: | |
$ ./DivisibilityGraph N | |
where N is the number you wish to produce a graph for. | |
The resulting image will be produced in the same directory and named "graphN".png | |
-} | |
import Data.Graph.Inductive | |
import Data.GraphViz | |
import Data.GraphViz.Attributes | |
import Data.GraphViz.Attributes.Complete | |
import System.Environment | |
main = do | |
(n:_) <- fmap (fmap read) getArgs | |
addExtension (runGraphviz (divGraph n)) Png ("graph" ++ show n) | |
divGraph :: Int -> DotGraph Int | |
divGraph n = graphElemsToDot graphParams (createNodes n) (createEdges n) | |
createNodes :: Int -> [(Int, String)] | |
createNodes n = map (\x -> (x, "")) [0..n-1] | |
createEdges :: Int -> [(Int, Int, Bool)] | |
createEdges n = concatMap (\x -> [ (x, (x + 1) `mod` n, True), | |
(x, (x * 10) `mod` n, False) ]) [0..n-1] | |
graphParams :: GraphvizParams Int String Bool () String | |
graphParams = Params { isDirected = True, | |
globalAttributes = [], | |
clusterBy = N, | |
clusterID = const (Int 0), | |
fmtCluster = const [], | |
fmtNode = nodeStyle, | |
fmtEdge = edgeStyle | |
} | |
colors = cycle $ [ LightBlue, Red, Orange, Yellow, Green, RoyalBlue, Purple, Brown, Pink, Gray] | |
edgeStyle (_, _, x) = if x then [arrowTo normal] else [arrowTo oNormal] | |
where oNormal = AType [(ArrMod OpenArrow BothSides, Normal)] | |
nodeStyle (n, _) = [ toLabel (show n), | |
style filled, | |
fillColor c, | |
color c, | |
shape Circle ] | |
where c = colors !! n | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment