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).
Where:
, , and are the cells round the position: the one at or before it, the next column, the next row, and both and 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,
NaNornoData, givesNaNif 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
| Parameter | Type | Description |
|---|---|---|
grid | GridInterface | The grid. |
position | GridPositionInterface | The 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