Skip to content

Commit

Permalink
improving random generation
Browse files Browse the repository at this point in the history
  • Loading branch information
rlch committed Jun 30, 2021
1 parent 720a35b commit b948d1c
Showing 1 changed file with 22 additions and 16 deletions.
38 changes: 22 additions & 16 deletions example/lib/screens/canvas.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class _CanvasScreenState extends State<CanvasScreen>
late final Ticker _ticker;
late final f.ForceSimulation simulation;
late final List<f.Edge> edges;
int i = 0;

@override
void didChangeDependencies() {
Expand All @@ -27,8 +28,8 @@ class _CanvasScreenState extends State<CanvasScreen>
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()],
Expand All @@ -37,14 +38,18 @@ class _CanvasScreenState extends State<CanvasScreen>
];
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();
});
Expand All @@ -67,18 +72,19 @@ class _CanvasScreenState extends State<CanvasScreen>
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,
),
),
),
),
],
),
),
Expand Down

0 comments on commit b948d1c

Please sign in to comment.