Skip to content

Instantly share code, notes, and snippets.

@BrianHicks
Last active December 16, 2015 01:49
Show Gist options
  • Save BrianHicks/5358053 to your computer and use it in GitHub Desktop.
Save BrianHicks/5358053 to your computer and use it in GitHub Desktop.
(defn euclidian [a b]
(Math/sqrt (+
(Math/pow (- (a 0) (b 0)) 2)
(Math/pow (- (a 1) (b 1)) 2))))
// utility functions
function euclidian(a, b) {
return Math.sqrt(
Math.pow(b[0] - a[0], 2) + // ADD
Math.pow(b[1] - a[1], 2)
);
}
var nodes = [
// [x, y]
[1, 1],
[1, 2],
[2, 1],
[2, 2]
// etc...
]
function getEdges(nodes, max_dist) {
// node: array of points, max_dist: maximum distance for a connection
var edges = [];
// TODO: if it's a list of arrays or somesuch, get a list of cartesian coordinates here, then iterate over that instead
var coord_list = nodes;
for (var i = 0; i < coord_list.length; i++) {
// outermost iteration. here we store a coordinate
var root = coord_list[i];
for (var j = 0; j < coord_list.length; j++) {
if (j == i) { continue; } // skip the one we already have
var node = coord_list[j],
dist = euclidian(root, node);
if (dist > max_dist) { continue; } // skip ones that are too far apart
edge = [root, node];
edge.sort(); // to prevent duplicates going both ways, which they will
if (edges.indexOf(edge) == -1) { edges.append(edge); }
}
}
return edges
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment