-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 3e617a9
Showing
2 changed files
with
111 additions
and
0 deletions.
There are no files selected for viewing
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
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) | ||
} |
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
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 | ||
} |