Skip to content

Documentation / @interstellar-tools/maps / gridValueBetween

Function: gridValueBetween() ​

ts
function gridValueBetween(
  grid: GridInterface,
  position: GridPositionInterface
): number;

Defined in: terrain/grid-value-between.ts:50

The value between cells of a grid, at a fractional position: blended from the four cells round it (bilinear interpolation).

z=(1−v)[(1−u)z00+uz01]+v[(1−u)z10+uz11]

Where:

  • z00, z01, z10 and z11 are the cells round the position: the one at or before it, the next column, the next row, and both
  • u and v are how far the position is past the first cell's centre, across and down, from 0 to 1

Units

  • Positions are in grid coordinates: whole numbers at the cells' centres.
  • The value is in the grid's own units: heights in m for an elevation grid.

Notes

  • At a cell's centre it's that cell's value, as gridValueAt gives it.
  • The grid covers its cells' area, so a position can lie up to half a cell past its outer cells' centres. There, the outer cells' values carry on.
  • Only the cells with a weight count: a missing cell, NaN or noData, gives NaN if the position is between it and the others, and nothing if it's on the far side of a row or column of present cells.

Parameters ​

ParameterTypeDescription
gridGridInterfaceThe grid.
positionGridPositionInterfaceThe position, in grid coordinates (finite; within half a cell of the outer cells' centres).

Returns ​

number

The value there, or NaN next to a missing cell.

Throws ​

  • If a size, cell size or the position is not finite, or a weighted cell holds an infinite value.
  • If the grid's parts don't fit together, or the position is off the grid.

Example ​

ts
// Heights 0 m and 10 m in neighbouring cells: 2.5 m a quarter of the way
const grid = createGrid({
  rows: 1,
  columns: 2,
  values: [0, 10],
  cellSize: { x: 30, y: 30 }
});

gridValueBetween(grid, { row: 0, column: 0.25 }); // 2.5

See ​

gridValueAt