UNPKG

spectral-clustering-js

Version:
64 lines (63 loc) 2.15 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var Node_1 = require("./Node"); var Graph = /** @class */ (function () { function Graph() { this.nodes = new Map(); } Graph.prototype.generateRandom = function (maxCoords, nbNodes, nbLinksForeachNode) { var _this = this; this.nodes.clear(); var _loop_1 = function (i) { var randomCoords = new Array(); maxCoords.forEach(function (v) { var c = Math.floor(Math.random() * v); randomCoords.push(c); }); var n = new Node_1.Node(randomCoords); this_1.addNode(n); }; var this_1 = this; // generate two packets for (var i = 0; i < nbNodes; i++) { _loop_1(i); } this.nodes.forEach(function (node1) { var nearest = _this.getNearestNodes(node1, nbLinksForeachNode); nearest.forEach(function (n) { node1.addConnectedNode(n); n.addConnectedNode(node1); }); }); }; Graph.prototype.addNode = function (node) { this.nodes.set(node.getId(), node); }; Graph.prototype.getNodes = function () { return Array.from(this.nodes.values()); }; Graph.prototype.getNearestNodes = function (node, n) { var result = new Array(); // let's calculate the distance for every node var distances = new Map(); this.nodes.forEach(function (value, key) { if (value.getId() != node.getId()) { var distance = node.getPoint().euclieanDistanceTo(value.getPoint()); distances.set(key, distance); result.push(value); } }.bind(this)); result = result.sort(function (a, b) { var less = distances.get(a.getId()) < distances.get(b.getId()); if (less) { return -1; } else { return 1; } }.bind(this)); return result.slice(0, n); }; return Graph; }()); exports.Graph = Graph;