Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
semibran committed Mar 4, 2018
0 parents commit 3e617a9
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 0 deletions.
43 changes: 43 additions & 0 deletions lib/emst.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
module.exports = function span(graph) {
var tree = { nodes: [ graph.nodes[0] ], edges: [] }
while (tree.nodes.length < graph.nodes.length) {
var shortest = Infinity
var origin = -1
var target = null
for (var i = 0; i < tree.nodes.length; i++) {
var node = tree.nodes[i]
for (var j = 0; j < graph.edges.length; j++) {
var edge = graph.edges[j]
var o = -1
if (edge[0] === i) {
o = edge[1]
} else if (edge[1] === i) {
o = edge[0]
}
var other = graph.nodes[o]
if (other && tree.nodes.indexOf(other) === -1) {
var distance = euclidean(node, other)
if (distance < shortest) {
shortest = distance
origin = i
target = other
}
}
}
}
var index = tree.nodes.length
tree.nodes.push(target)
tree.edges.push([ origin, index ])
}
return tree
}

function euclidean(a, b) {
var quadrance = 0
var dimensions = Math.max(a.length, b.length)
for (var i = 0; i < dimensions; i++) {
var distance = (b[i] || 0) - (a[i] || 0)
quadrance += distance * distance
}
return Math.sqrt(quadrance)
}
68 changes: 68 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
const span = require("./lib/emst")
const radius = 128
const graph = {
edges: [],
nodes: new Array(32)
.fill()
.map(_ => {
let direction = Math.random() * 2 * Math.PI
let magnitude = Math.sqrt(Math.random())
return [
Math.cos(direction) * magnitude * radius,
Math.sin(direction) * magnitude * radius
]
})
}

for (let i = 0; i < graph.nodes.length - 1; i++) {
for (let j = i + 1; j < graph.nodes.length; j++) {
graph.edges.push([ i, j ])
}
}

let tree = span(graph)
let state = {
graph: tree,
viewport: {
halfsize: [ radius * 2, radius * 2 ],
position: [ 0, 0 ]
}
}

let context = render(state)
document.body.appendChild(context.canvas)

function render({ graph, viewport }, context) {
let canvas = null
if (context) {
canvas = context.canvas
} else {
canvas = document.createElement("canvas")
context = canvas.getContext("2d")
}
canvas.width = viewport.halfsize[0] * 2
canvas.height = viewport.halfsize[1] * 2
context.fillStyle = "black"
context.fillRect(0, 0, canvas.width, canvas.height)
for (let edge of graph.edges) {
let nodes = edge.map(i =>
graph.nodes[i].map((x, i) =>
x + viewport.halfsize[i]
)
)
context.beginPath()
context.moveTo(...nodes[0])
context.lineTo(...nodes[1])
context.strokeStyle = "white"
context.stroke()
}
for (let node of graph.nodes) {
let x = node[0] + viewport.halfsize[0]
let y = node[1] + viewport.halfsize[1]
context.beginPath()
context.arc(x, y, 2, 0, 2 * Math.PI)
context.fillStyle = "white"
context.fill()
}
return context
}

0 comments on commit 3e617a9

Please sign in to comment.