getIndexOfPosition method

bool getIndexOfPosition(
  1. double x,
  2. double y,
  3. List<int> result,
  4. bool clamp,
)

Get the index of a local position on the heightfield. The indexes indicate the rectangles, so if your terrain is made of N x N height data points, you will have rectangle indexes ranging from 0 to N-1. @param result Two-element array @param clamp If the position should be clamped to the heightfield edge.

Implementation

bool getIndexOfPosition(double x, double y, List<int> result, bool clamp) {
  // Get the index of the data points to test against
  final w = elementSize;
  final data = this.data;
  int xi = (x / w).floor();
  int yi = (y / w).floor();
  result.addAll([xi,yi]);

  if (clamp) {
    // Clamp index to edges
    if (xi < 0) {
      xi = 0;
    }
    if (yi < 0) {
      yi = 0;
    }
    if (xi >= data.length - 1) {
      xi = data.length - 1;
    }
    if (yi >= data[0].length - 1) {
      yi = data[0].length - 1;
    }
  }

  // Bail out if we are out of the terrain
  if (xi < 0 || yi < 0 || xi >= data.length - 1 || yi >= data[0].length - 1) {
    return false;
  }

  return true;
}