Skip to content

Instantly share code, notes, and snippets.

@zeffii
Created September 11, 2013 18:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zeffii/6527689 to your computer and use it in GitHub Desktop.
Save zeffii/6527689 to your computer and use it in GitHub Desktop.
coffee MST graph

[ Launch: coffee_template ] 6527689 by zeffii
[ Launch: coffee_template ] 6448693 by zeffii
[ Launch: boomstick_motion_wcolor_coffee ] 6399870 by zeffii
[ Launch: boomstick_motion_wcolor_coffee ] 6382272 by zeffii
[ Launch: boomstick_motion_wcolor_coffee ] 6382237 by zeffii
[ Launch: boomstick_motion_wcolor_coffee ] 6379220 by zeffii
[ Launch: boomstick_motion_wcolor ] 6376715 by zeffii
[ Launch: boomstick_motion2 ] 6365156 by zeffii
[ Launch: boomstick_motion ] 6364686 by zeffii
[ Launch: boomstick ] 6364584 by zeffii
[ Launch: zeffii default ] 6364028 by zeffii
[ Launch: zeffii default ] 5033869 by zeffii

{"description":"coffee MST graph","endpoint":"","display":"svg","public":true,"require":[],"fileconfigs":{"inlet.js":{"default":true,"vim":false,"emacs":false,"fontSize":12},"style.css":{"default":true,"vim":false,"emacs":false,"fontSize":12},"_.md":{"default":true,"vim":false,"emacs":false,"fontSize":12},"config.json":{"default":true,"vim":false,"emacs":false,"fontSize":12},"data2.csv":{"default":true,"vim":false,"emacs":false,"fontSize":12},"util.js":{"default":true,"vim":false,"emacs":false,"fontSize":12},"injet.coffee":{"default":true,"vim":false,"emacs":false,"fontSize":12},"inlet.coffee":{"default":true,"vim":false,"emacs":false,"fontSize":12},"util.coffee":{"default":true,"vim":false,"emacs":false,"fontSize":12}},"fullscreen":false,"play":false,"loop":false,"restart":false,"autoinit":true,"pause":true,"loop_type":"period","bv":false,"nclones":15,"clone_opacity":0.4,"duration":3000,"ease":"linear","dt":0.01,"thumbnail":"http://i.imgur.com/OqFYJQH.png"}
# multiple MST in the same graph
between = tributary.between
#d3.select("body").style "background-color", d3.rgb(25, 25, 25)
svg = d3.select("svg")
svg.append("rect").attr({width:"100%", height:"100%", fill: d3.rgb(25,25,25) });
group1 = svg.append("g").classed("group1", true)
text_style1 =
"fill": "#D8D8D8"
"font-size": 0.7 + "em"
"font-face": "sans-serif"
stroke: "none"
graph = [
{x: 20, y: 50},
{x: 120, y: 31},
{x: 5, y: 166},
{x: 95, y: 151},
{x: 152, y: 88},
]
edges = [
{v1: 1, v2: 2, w: 2},
{v1: 1, v2: 3, w: 2},
{v1: 2, v2: 3, w: 2},
{v1: 3, v2: 4, w: 2},
{v1: 4, v2: 5, w: 4},
{v1: 2, v2: 5, w: 4},
]
# must be sparse and the lower of the two must be first.
mst1 = [[1,2],[2,3],[3,4],[4,5]]
mst2 = [[1,3],[2,3],[3,4],[4,5]]
mst3 = [[1,2],[2,3],[3,4],[2,5]]
draw_graph = (group, graph, edges, mst, pos) ->
# All Edges
all_edges = group.append('g').classed('All_Edges', true)
.attr("transform", "translate(" + pos + ")")
.style
stroke: "#C55559"
'stroke-width': 5.3824
all_e = all_edges.selectAll('g').data(edges).enter()
all = all_e.append('g')
.classed("item_group2", true)
all.append('line')
.attr
x1: (d) -> graph[d.v1 - 1].x
y1: (d) -> graph[d.v1 - 1].y
x2: (d) -> graph[d.v2 - 1].x
y2: (d) -> graph[d.v2 - 1].y
# All MST Edges
graph_edges = group.append('g').classed('Edges', true)
.attr("transform", "translate(" + pos + ")")
.style
stroke: "#55B3C5"
'stroke-width': 5.3824
gedges = graph_edges.selectAll('g').data(mst).enter()
gedge = gedges.append('g')
.classed("item_group2", true)
gedge.append('line')
.attr
x1: (d) -> graph[d[0]-1].x
y1: (d) -> graph[d[0]-1].y
x2: (d) -> graph[d[1]-1].x
y2: (d) -> graph[d[1]-1].y
# Weights
weights = group.append('g').classed('Weights', true)
.attr("transform", "translate(" + pos + ")")
.style
fill: "#42424D"
stroke: "#C55555"
'stroke-width': 1.7696
wballs = weights.selectAll('g').data(edges).enter()
wball = wballs.append('g')
.classed("item_group2", true)
.attr
transform: (d) ->
p1 = graph[d.v1 - 1]
p2 = graph[d.v2 - 1]
p = between(p1, p2)
"translate(" + [p.x, p.y] + ")"
wball.append('circle')
.attr
r: 8
wball.append('text')
.text((d) -> d.w)
.style(text_style1)
.attr
transform: "translate(" + [-3, 4] + ")"
# Nodes
nodes = group.append('g').classed('Nodes', true)
.attr("transform", "translate(" + pos + ")")
.style
fill: "#42424D"
stroke: "#aeaeae"
'stroke-width': 3.7696
balls = nodes.selectAll('g').data(graph).enter()
ball = balls.append('g')
.classed("item_group", true)
.attr
transform: (d) -> "translate(" + [d.x, d.y] + ")"
ball.append('circle')
.attr
r: 11
ball.append('text')
.text((d, i ) -> i+1)
.style(text_style1)
.attr
transform: (d) -> "translate(" + [-4, 4] + ")"
pos1 = [62, 36]
pos2 = [62, 36+200]
pos3 = [62, 36+400]
draw_graph(group1, graph, edges, mst1, pos1)
draw_graph(group1, graph, edges, mst2, pos2)
draw_graph(group1, graph, edges, mst3, pos3)
.cm-s-elegant.CodeMirror { background: #1e2426; color: #696969; }
.cm-s-elegant div.CodeMirror-selected {background: #064968 !important;} /* 33322B*/
.cm-s-elegant span.cm-variable { color:#22EFFF; }
.cm-s-elegant span.cm-variable-2 { color: #FFCCB4; }
.cm-s-elegant span.cm-variable-3 { color: white; }
.cm-s-elegant span.cm-string { color: Chartreuse; }
.cm-s-elegant span.cm-string-2 {color: Chartreuse;}
.cm-s-elegant span.cm-def {color: #FFCCB4; opacity: 1.0}
.cm-s-elegant span.cm-bracket { color: #EBEFE7; }
.cm-s-elegant pre { color:#FFF; }
.cm-s-elegant span.cm-qualifier { color:#C0C0C0; }
.cm-s-elegant span.cm-comment { color: #AFB4B4;}
.cm-s-elegant span.cm-property {color: #FDA676;}
.cm-s-elegant span.cm-number { color: #FF92EE;}
.cm-s-elegant span.cm-keyword { color: #FFFF18; }
.cm-s-elegant .CodeMirror-cursor { border-left: 1px solid white !important; }
.cm-s-elegant .CodeMirror-gutters {background: #505050;}
.cm-s-elegant .CodeMirror-linenumber {color: #D3D3D3;}
tributary.between = (p1, p2) ->
x: (p1.x + p2.x) * 0.5
y: (p1.y + p2.y) * 0.5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment