maximize method

MaximizeResult maximize(
  1. Box cube,
  2. Direction direction,
  3. int first,
  4. int last,
  5. int wholeR,
  6. int wholeG,
  7. int wholeB,
  8. int wholeW,
)

Implementation

MaximizeResult maximize(Box cube, Direction direction, int first, int last,
    int wholeR, int wholeG, int wholeB, int wholeW) {
  int bottomR = bottom(cube, direction, momentsR);
  int bottomG = bottom(cube, direction, momentsG);
  int bottomB = bottom(cube, direction, momentsB);
  int bottomW = bottom(cube, direction, weights);

  double max = 0.0;
  int cut = -1;

  for (int i = first; i < last; i++) {
    int halfR = bottomR + top(cube, direction, i, momentsR);
    int halfG = bottomG + top(cube, direction, i, momentsG);
    int halfB = bottomB + top(cube, direction, i, momentsB);
    int halfW = bottomW + top(cube, direction, i, weights);

    if (halfW == 0) {
      continue;
    }

    double tempNumerator =
        ((halfR * halfR) + (halfG * halfG) + (halfB * halfB)).toDouble();
    double tempDenominator = halfW.toDouble();
    double temp = tempNumerator / tempDenominator;

    halfR = wholeR - halfR;
    halfG = wholeG - halfG;
    halfB = wholeB - halfB;
    halfW = wholeW - halfW;
    if (halfW == 0) {
      continue;
    }
    tempNumerator =
        ((halfR * halfR) + (halfG * halfG) + (halfB * halfB)).toDouble();
    tempDenominator = halfW.toDouble();
    temp += (tempNumerator / tempDenominator);

    if (temp > max) {
      max = temp;
      cut = i;
    }
  }
  return MaximizeResult(cutLocation: cut, maximum: max);
}