getUtoTmapping method

dynamic getUtoTmapping(
  1. dynamic u, [
  2. dynamic distance
])
inherited

Implementation

getUtoTmapping(u, [distance]) {
  var arcLengths = getLengths(null);

  int i = 0;
  int il = arcLengths.length;

  var targetArcLength; // The targeted u distance value to get

  if (distance != null) {
    targetArcLength = distance;
  } else {
    targetArcLength = u * arcLengths[il - 1];
  }

  // binary search for the index with largest value smaller than target u distance

  var low = 0, high = il - 1, comparison;

  while (low <= high) {
    i = Math.floor(low + (high - low) / 2)
        .toInt(); // less likely to overflow, though probably not issue here, JS doesn't really have integers, all numbers are floats

    comparison = arcLengths[i] - targetArcLength;

    if (comparison < 0) {
      low = i + 1;
    } else if (comparison > 0) {
      high = i - 1;
    } else {
      high = i;
      break;

      // DONE

    }
  }

  i = high;

  if (arcLengths[i] == targetArcLength) {
    return i / (il - 1);
  }

  // we could get finer grain at lengths, or use simple interpolation between two points

  var lengthBefore = arcLengths[i];
  var lengthAfter = arcLengths[i + 1];

  var segmentLength = lengthAfter - lengthBefore;

  // determine where we are between the 'before' and 'after' points

  var segmentFraction = (targetArcLength - lengthBefore) / segmentLength;

  // add that fractional amount to t

  var t = (i + segmentFraction) / (il - 1);

  return t;
}