reallocate method

void reallocate(
  1. List<Cluster> clusters
)

Associate this instance with the nearest cluster in clusters.

Implementation

void reallocate(List<Cluster> clusters) {
  num? min;
  late Cluster argMin;
  for (Cluster c in clusters) {
    num d = distanceFrom(c);
    if (min == null || min > d) {
      min = d;
      argMin = c;
    }
  }
  if (cluster != null) {
    cluster!.instances.remove(this);
  }
  cluster = argMin;
  cluster!.instances.add(this);
}