apply method

  1. @override
void apply(
  1. ForceSimulation simulation
)
override

Applies the force to the simulation.

Implementation

@override
void apply(ForceSimulation simulation) {
  final nodes = simulation.nodes;

  for (int iter = 0; iter < iterations; iter++) {
    for (int i = 0; i < nodes.length; i++) {
      final node = nodes[i];
      if (node.fixed) continue;

      for (int j = i + 1; j < nodes.length; j++) {
        final other = nodes[j];

        var dx = other.x - node.x;
        var dy = other.y - node.y;
        var dist = math.sqrt(dx * dx + dy * dy);

        final minDist = node.radius + other.radius + radius;

        if (dist < minDist) {
          if (dist == 0) {
            dist = 0.001;
            dx = dist;
          }

          final overlap = (minDist - dist) / dist * strength;
          dx *= overlap / 2;
          dy *= overlap / 2;

          if (!node.fixed) {
            node.x -= dx;
            node.y -= dy;
          }

          if (!other.fixed) {
            other.x += dx;
            other.y += dy;
          }
        }
      }
    }
  }
}