diff --git a/lib/main_graph.dart b/lib/main_graph.dart index 7da7b8ac..ac761eb4 100644 --- a/lib/main_graph.dart +++ b/lib/main_graph.dart @@ -159,7 +159,7 @@ class Graph extends ChangeNotifier { List nodes = []; List edges = []; - Map> _neighbours = {}; + Map> _neighbours = {}; Map _nodeIndexes; void notify() { @@ -177,7 +177,7 @@ class Graph extends ChangeNotifier { var _nodes = _neighbours[n.label]; if (_nodes != null) { - return _nodes; + return _nodes.union(computeOverlappingNodes(n)).toList(); } var nodes = {}; @@ -193,8 +193,30 @@ class Graph extends ChangeNotifier { } } - _nodes = nodes.toList(); _neighbours[n.label] = _nodes; + return nodes.union(computeOverlappingNodes(n)).toList(); + } + +// These nodes aren't actually neighbours, but we don't want nodes to + // ever overlap, so I'm making the ones that are close by neighbours + Set computeOverlappingNodes(Node n) { + var _nodes = {}; + for (var i = 0; i < nodes.length; i++) { + var node = nodes[i]; + if (node.label == n.label) { + continue; + } + + var dx = node.x - n.x; + var dy = node.y - n.y; + + var dist = sqrt((dx * dx) + (dy * dy)); + if (dist <= 60) { + // print('${node.label} and ${n.label} are too close - $dist'); + _nodes.add(i); + } + } + return _nodes; }