Documentation / @interstellar-tools/maps / sunlitGrid
Function: sunlitGrid()
ts
function sunlitGrid(
grid: GridInterface,
sunAzimuth: Radians,
sunElevation: Radians,
radius: number
): GridInterface;Defined in: terrain/sunlit.ts:63
Which cells of an elevation grid the Sun reaches, and which lie in the shadow of other terrain, such as a crater's rim.
From each cell, the line to the Sun is followed a cell at a time, until it is higher than any ground or leaves the grid, with the ground's height at each point on it from gridValueBetween, or the nearest cell's own next to a missing one. The cell is in shadow if ground on the way rises above it:
Where:
is the cell's height, and the ground's height towards the Sun is the Sun's elevation, and the body's radius:is how far its curve drops far ground
Units
- Heights and the radius in m; angles in radians, the azimuth clockwise from north.
Notes
- The result is 1 where the Sun reaches a cell and 0 where it doesn't, the same shape as the grid, with
NaNwhere it's missing. Missing cells cast no shadow. - With the Sun at or below the horizon, every cell is in shadow.
- The Sun is a point here: its disc, half a degree across from Earth, would soften the shadows' edges.
- It finds cast shadows only. For how brightly the Sun lights the ground it reaches, multiply by hillshadeGrid.
- Each cell's ray stops once it's higher than the highest ground, or leaves the grid, so the work grows with the cells times that distance in cells. A high Sun is quick; a Sun a degree or two up over a large grid with one tall peak walks most rays to the grid's edge, millions of steps for every million cells. For that, work on a downsampleGrid first.
Parameters
| Parameter | Type | Description |
|---|---|---|
grid | GridInterface | Elevation grid, heights in m. |
sunAzimuth | Radians | Sun's azimuth in radians, clockwise from north (finite). |
sunElevation | Radians | Sun's elevation above the horizon in radians (in [−π/2, π/2]). |
radius | number | The body's radius, in m (> 0, or Infinity for flat ground). |
Returns
A grid of the same shape: 1 lit, 0 in shadow, NaN missing.
Throws
- If a size, cell size or Sun angle is not finite, or a cell holds an infinite value.
- If the grid's parts don't fit together,
sunElevationis outside [−π/2, π/2], orradiusis not > 0.
Example
ts
// Light and shadow, with the Sun 10° up in the east
const lit = sunlitGrid(grid, toRad(90), toRad(10), 6_371_000);
const shade = hillshadeGrid(grid, toRad(90), toRad(10));
const light = lit.values.map((on, i) => on * shade.values[i]!);