vector3.unit

The vector scaled to length 1, or null when it has no direction.

GeometricSince 0.3.0

Usage

TypeScript
Vector3.unit(v: Vector3): Vector3 | null

Parameters

  • vVector3

    The vector to normalise; any non-zero finite length. Required. In model.

Returns

A vector of length 1 in the direction of v; null when v has zero length or a non-finite length (NaN or infinite component).

Guarantees

  • a non-null result has length 1 to within a few ulps (Math.abs(Vector3.length(u) - 1) < 1e-15)
  • a non-null result points the same way as v (Vector3.angleRad(u, v) is 0 to rounding)
  • null exactly when Vector3.length(v) is 0 or not finite
  • The same result on every machine.

Example

TypeScript
Vector3.unit(Vector3.of(0, 0, -3)) // → { x: 0, y: 0, z: -1 }
Vector3.unit(Vector3.zero) // → null

See also