Skip to content

Instantly share code, notes, and snippets.

@zeffii
Created September 20, 2013 12:32
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/6636768 to your computer and use it in GitHub Desktop.
Save zeffii/6636768 to your computer and use it in GitHub Desktop.
coffee MST graph min

[ Launch: coffee MST graph min ] 6636768 by zeffii
[ Launch: coffee MST graph ] 6563511 by zeffii
[ 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 min","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/IGxlOfe.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]]
pos1 = [62, 36]
pos2 = [62, 36+200]
pos3 = [62, 36+400]
draw_graph = (group, graph, edges, mst, pos) ->
# edges
path_all_edges = (d) ->
'M' + [graph[d.v1-1].x, graph[d.v1-1].y, graph[d.v2-1].x, graph[d.v2-1].y]
path_all_MST = (d) ->
'M' + [graph[d[0]-1].x, graph[d[0]-1].y, graph[d[1]-1].x, graph[d[1]-1].y]
draw_edges = (name, color, path_func, data) ->
all_edges = group.append('g').classed(name, true)
.attr("transform", "translate(" + pos + ")")
.style
stroke: color
'stroke-width': 5.3824
all_e = all_edges.selectAll('g').data(data).enter()
all = all_e.append('g')
all.append('path')
.attr d: (d) -> path_func(d)
draw_edges('All_Edges', "#C55559", path_all_edges, edges)
draw_edges('MST_Edges', "#55B3C5", path_all_MST, mst)
# nodes
gtran_2 = (d) ->
p1 = graph[d.v1 - 1]
p2 = graph[d.v2 - 1]
p = between(p1, p2)
[p.x, p.y]
tfunc_2 = (d,i) -> d.w
gtran_1 = (d) -> [d.x, d.y]
tfunc_1 = (d,i) -> i+1
draw_circles = (c_name, data, g_name, gtrans_func, ttrans, text_func, r) ->
nodes = group.append('g').classed(c_name, true)
.attr("transform", "translate(" + pos + ")")
balls = nodes.selectAll('g').data(data).enter()
ball = balls.append('g')
.classed(g_name, true)
.attr
transform: (d) -> "translate(" + gtrans_func(d) + ")"
ball.append('circle')
.attr
r: r
ball.append('text')
.text((d, i ) -> text_func(d, i))
.style(text_style1)
.attr
transform: (d) -> "translate(" + ttrans + ")"
draw_circles('Weights', edges, 'item_group2', gtran_2, [-3,4], tfunc_2, 8)
draw_circles('Nodes', graph, 'item_group', gtran_1, [-4,4], tfunc_1, 11)
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;}
.Weights {
fill: #42424D;
stroke: #C55555;
stroke-width: 2px;
}
.Nodes {
fill: #42424D;
stroke: #aeaeae;
stroke-width: 3.7px;
}
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