createBoxes method

CreateBoxesResult createBoxes(
  1. int maxColorCount
)

Implementation

CreateBoxesResult createBoxes(int maxColorCount) {
  cubes = List<Box>.generate(maxColorCount, (index) => Box());
  cubes[0] = Box(
      r0: 0, r1: maxIndex, g0: 0, g1: maxIndex, b0: 0, b1: maxIndex, vol: 0);

  List<double> volumeVariance =
      List.filled(maxColorCount, 0.0, growable: false);
  int next = 0;
  int generatedColorCount = maxColorCount;
  for (int i = 1; i < maxColorCount; i++) {
    if (cut(cubes[next], cubes[i])) {
      volumeVariance[next] =
          (cubes[next].vol > 1) ? variance(cubes[next]) : 0.0;
      volumeVariance[i] = (cubes[i].vol > 1) ? variance(cubes[i]) : 0.0;
    } else {
      volumeVariance[next] = 0.0;
      i--;
    }

    next = 0;
    double temp = volumeVariance[0];
    for (var j = 1; j <= i; j++) {
      if (volumeVariance[j] > temp) {
        temp = volumeVariance[j];
        next = j;
      }
    }
    if (temp <= 0.0) {
      generatedColorCount = i + 1;
      break;
    }
  }

  return CreateBoxesResult(
      requestedCount: maxColorCount, resultCount: generatedColorCount);
}