getNearestSurroundingPair static method

List<Coordinate> getNearestSurroundingPair(
  1. List<Coordinate> line,
  2. double x
)

Finds points which X are is between on X axis, closest . IMPORTANT: Does not sort: Assumes order of coordinates is in increaing values of X. Will include is equal, so inclusive. @returnValue Array with pair.

Implementation

static List<Coordinate> getNearestSurroundingPair(List<Coordinate> line, double x)
{
  if (line.length < 2) {
    throw new Exception("Algebra.getNearestSurroundingPair: At least two coordinates must be defined in the line.");
  }

  // Find closest points where x is between.
  int i=0;
  Coordinate? nextPoint = null;
  for (; i < line.length - 1; i++)
  {
    nextPoint = line[i+1];

    if (nextPoint.x > x) {
      break;
    }
  }

  // Return the path.
  List<Coordinate> surroundingPair = [line[i], nextPoint!];
  return surroundingPair;
}