bisectNotationAtX method

ChantNotationElement bisectNotationAtX(
  1. double x, {
  2. bool useMidpoint = true,
})

Implementation

ChantNotationElement bisectNotationAtX(double x, {bool useMidpoint = true}) {
  int minIndex = -1;
  int maxIndex = (numNotationsOnLine < double.infinity)
      ? numNotationsOnLine
      : 0;
  int curIndex = minIndex + ((maxIndex - minIndex) >> 1);
  final notations = score.notations.sublist(
    notationsStartIndex,
    notationsStartIndex + numNotationsOnLine,
  );

  while (minIndex < curIndex) {
    final notation = notations[curIndex];
    final notationX = notation.bounds.x;
    if (notationX > x) {
      maxIndex = curIndex;
    } else {
      minIndex = curIndex;
    }
    curIndex = minIndex + ((maxIndex - minIndex) >> 1);
  }
  final notation = notations[curIndex];
  if (useMidpoint &&
      notation.bounds.width == 0 &&
      curIndex + 1 < notations.length) {
    final nextNotation = notations[curIndex + 1];
    final closenessToLeft = x - notation.bounds.x;
    final closenessToRight = nextNotation.bounds.x - x;
    if (closenessToRight < closenessToLeft) {
      curIndex++;
    }
  }
  return notation;
}