UNPKG

@graphty/layout

Version:

graph layout algorithms based on networkx

27 lines 961 B
/** * Random layout algorithm */ import { _processParams } from '../../utils/params'; import { getNodesFromGraph } from '../../utils/graph'; import { RandomNumberGenerator } from '../../utils/random'; /** * Position nodes uniformly at random in the unit square. * * @param G - Graph or list of nodes * @param center - Coordinate pair around which to center the layout * @param dim - Dimension of layout * @param seed - Random seed for reproducible layouts * @returns Positions dictionary keyed by node */ export function randomLayout(G, center = null, dim = 2, seed = null) { const processed = _processParams(G, center, dim); const nodes = getNodesFromGraph(processed.G); center = processed.center; const rng = new RandomNumberGenerator(seed ?? undefined); const pos = {}; nodes.forEach((node) => { pos[node] = rng.rand(dim).map((val, i) => val + center[i]); }); return pos; } //# sourceMappingURL=random.js.map