Skip to content

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:

z(d)>z0+dtan⁡α+d22R

Where:

  • z0 is the cell's height, and z(d) the ground's height d towards the Sun
  • α is the Sun's elevation, and R the body's radius: d2/2R 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 NaN where 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 ​

ParameterTypeDescription
gridGridInterfaceElevation grid, heights in m.
sunAzimuthRadiansSun's azimuth in radians, clockwise from north (finite).
sunElevationRadiansSun's elevation above the horizon in radians (in [−π/2, π/2]).
radiusnumberThe body's radius, in m (> 0, or Infinity for flat ground).

Returns ​

GridInterface

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, sunElevation is outside [−π/2, π/2], or radius is 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]!);

See ​