getCenterYAmongIncomes method

int getCenterYAmongIncomes(
  1. NodeOutput item,
  2. Matrix mtx
)

Implementation

int getCenterYAmongIncomes(NodeOutput item, Matrix mtx) {
  final incomes = item.passedIncomes.map((e) => e).toList();
  if (incomes.length == 0) {
    return 0;
  }
  incomes.sort((keyA, keyB) {
    List<int>? coordsA = mtx.find((NodeOutput itm) {
      return itm.id == keyA;
    });
    List<int>? coordsB = mtx.find((NodeOutput itm) {
      return itm.id == keyB;
    });
    if (coordsA?.length != 2)
      throw "cannot find coordinates for passed income: $keyA";
    if (coordsB?.length != 2)
      throw "cannot find coordinates for passed income: $keyB";

    return coordsA![1] > coordsB![1] ? 1 : -1;
  });
  int centerIndex = (incomes.length.toDouble() / 2).ceil() - 1;
  String centerKey = incomes[centerIndex];
  List<int>? coords = mtx.find((NodeOutput itm) {
    return itm.id == centerKey;
  });
  if (coords?.length != 2)
    throw "cannot find coordinates for passed center income: $centerKey";
  return coords![1];
}