From b948d1c709675d4ba88de39548921b1ef0654aa0 Mon Sep 17 00:00:00 2001 From: RJM Date: Wed, 30 Jun 2021 13:13:25 +1000 Subject: [PATCH] improving random generation --- example/lib/screens/canvas.dart | 38 +++++++++++++++++++-------------- 1 file changed, 22 insertions(+), 16 deletions(-) diff --git a/example/lib/screens/canvas.dart b/example/lib/screens/canvas.dart index 3076e00..6cde72b 100644 --- a/example/lib/screens/canvas.dart +++ b/example/lib/screens/canvas.dart @@ -18,6 +18,7 @@ class _CanvasScreenState extends State late final Ticker _ticker; late final f.ForceSimulation simulation; late final List edges; + int i = 0; @override void didChangeDependencies() { @@ -27,8 +28,8 @@ class _CanvasScreenState extends State final r = Random(); edges = [ for (final n in nodes) - if (r.nextDouble() < 0.1) ...[ - for (int i = 0; i < (r.nextDouble() * 10).toInt(); i++) + if (r.nextDouble() < 0.6) ...[ + for (int i = 0; i < (r.nextDouble() * 5).toInt(); i++) f.Edge( source: n, target: nodes[(nodes.length * r.nextDouble()).toInt()], @@ -37,14 +38,18 @@ class _CanvasScreenState extends State ]; simulation = f.ForceSimulation() ..nodes = nodes - ..setForce('collide', f.Collide(strength: 2, radius: 5)) - ..setForce('manyBody', f.ManyBody(strength: -5)) + ..setForce('collide', f.Collide(radius: 5)) + ..setForce('manyBody', f.ManyBody()) ..setForce( 'edges', - f.Edges(distance: 40, edges: edges), - ); + f.Edges(edges: edges, distance: 15), + ) + ..alpha = 1 + ..tick(10); _ticker = this.createTicker((_) { + i++; + // if (i % 10 != 0) return; setState(() { simulation.tick(); }); @@ -67,18 +72,19 @@ class _CanvasScreenState extends State child: SimulationCanvas( children: [ for (final node in simulation.nodes) - SimulationCanvasObject( - node: node, - edges: [...edges.where((e) => e.source == node)], - child: Container( - width: 10, - height: 10, - decoration: BoxDecoration( - color: Colors.black, - shape: BoxShape.circle, + if (!node.isNaN) + SimulationCanvasObject( + node: node, + edges: [...edges.where((e) => e.source == node)], + child: Container( + width: 10, + height: 10, + decoration: BoxDecoration( + color: Colors.black, + shape: BoxShape.circle, + ), ), ), - ), ], ), ),