Immutable 4D(x,y,z,w) Vector class with 64-bit float components.

This class provides the same functionality as Vector4 but with double precision floating-point components. Use this when higher precision is required for mathematical calculations or when working with very large or very small numbers.

const vec1 = Vector4d.fromCopy4(1.0, 2.0, 3.0, 1.0);
const vec2 = Vector4d.fromCopyArray4([2.0, 3.0, 3.0, 1.0]);
const dotProduct = vec1.dot(vec2);
const sum = Vector4d.add(vec1, vec2);

Hierarchy (view full)

  • Vector4_<Float64ArrayConstructor>
    • Vector4d

Properties

_v: TypedArray = ...

Internal typed array storage for vector components

Accessors

  • get bytesPerComponent(): number
  • Gets the number of bytes per component in the underlying typed array.

    Returns number

    The number of bytes per element (4 for Float32Array, 8 for Float64Array)

  • get className(): string
  • Gets the class name of this vector type.

    Returns string

    The string "Vector4"

  • get glslStrAsFloat(): string
  • Converts the vector to a GLSL vec4 string representation with float precision.

    Returns string

    A GLSL-compatible vec4 string (e.g., "vec4(1.0, 2.0, 3.0, 4.0)")

  • get glslStrAsInt(): string
  • Converts the vector to a GLSL ivec4 string representation with integer values.

    Returns string

    A GLSL-compatible ivec4 string (e.g., "ivec4(1, 2, 3, 4)")

  • get w(): number
  • Gets the W component of the vector.

    Returns number

    The W component value

  • get wgslStrAsFloat(): string
  • Converts the vector to a WGSL vec4f string representation with float precision.

    Returns string

    A WGSL-compatible vec4f string (e.g., "vec4f(1.0, 2.0, 3.0, 4.0)")

  • get wgslStrAsInt(): string
  • Converts the vector to a WGSL vec4i string representation with integer values.

    Returns string

    A WGSL-compatible vec4i string (e.g., "vec4i(1, 2, 3, 4)")

  • get x(): number
  • Gets the X component of the vector.

    Returns number

    The X component value

  • get y(): number
  • Gets the Y component of the vector.

    Returns number

    The Y component value

  • get z(): number
  • Gets the Z component of the vector.

    Returns number

    The Z component value

  • get compositionType(): CompositionTypeClass<"VEC4">
  • Gets the composition type identifier for this vector type.

    Returns CompositionTypeClass<"VEC4">

    The CompositionType.Vec4 identifier

Methods

  • Gets the component value at the specified index.

    Parameters

    • i: number

      The index (0=x, 1=y, 2=z, 3=w)

    Returns number

    The component value at the specified index

  • Calculates the dot product between this vector and another vector. The dot product is the sum of the products of corresponding components.

    Parameters

    • vec: IVector4

      The vector to calculate dot product with

    Returns number

    The dot product of the two vectors

  • Checks if this vector is approximately equal to another vector within a specified tolerance.

    Parameters

    • vec: IVector4

      The vector to compare with

    • delta: number = Number.EPSILON

      The tolerance value for comparison (default: Number.EPSILON)

    Returns boolean

    True if the vectors are approximately equal, false otherwise

  • Checks if this vector is strictly equal to another vector (exact component equality).

    Parameters

    • vec: IVector4

      The vector to compare with

    Returns boolean

    True if all components are exactly equal, false otherwise

  • Checks if the internal storage shares the same ArrayBuffer as the provided one. Useful for determining if vectors share underlying memory.

    Parameters

    • arrayBuffer: ArrayBuffer

      The ArrayBuffer to compare against

    Returns boolean

    True if the same ArrayBuffer is used, false otherwise

  • Calculates the squared length (magnitude) of the vector. This is more efficient than length() when only comparing magnitudes.

    Returns number

    The squared length of the vector

  • Calculates the distance from this vector to another vector.

    Parameters

    • vec: IVector4

      The target vector to calculate distance to

    Returns number

    The distance between the two vectors

  • Converts the vector to an approximately formatted string representation with financial precision. Each component is formatted to a fixed number of decimal places.

    Returns string

    A string with space-separated components followed by a newline

  • Gets the component value at the specified index. Alias for the at() method for convenience.

    Parameters

    • i: number

      The index of the component to retrieve

    Returns number

    The component value at the given index

  • Divides a vector by a scalar value and returns a new vector. If the divisor is zero, the result components will be set to Infinity and an error will be logged.

    Parameters

    • vec: IVector4

      The vector to divide

    • value: number

      The scalar value to divide by

    Returns Vector4d

    A new Vector4d with each component divided by the scalar

  • Divides a vector by a scalar value and stores the result in the output vector. If the divisor is zero, the result components will be set to Infinity and an error will be logged. This method modifies the output vector in-place for better performance.

    Parameters

    • vec: IVector4

      The vector to divide

    • value: number

      The scalar value to divide by

    • out: IMutableVector4

      The output vector to store the result (will be modified)

    Returns IMutableVector4

    The modified output vector with each component divided by the scalar

  • Divides the left vector by the right vector component-wise and returns a new vector. If any component of the right vector is zero, the corresponding result component will be set to Infinity.

    Parameters

    • l_vec: IVector4

      The left operand vector (dividend)

    • r_vec: IVector4

      The right operand vector (divisor)

    Returns Vector4d

    A new Vector4d with component-wise division result

  • Divides the left vector by the right vector component-wise and stores the result in the output vector. If any component of the right vector is zero, the corresponding result component will be set to Infinity. This method modifies the output vector in-place for better performance.

    Parameters

    • l_vec: IVector4

      The left operand vector (dividend)

    • r_vec: IVector4

      The right operand vector (divisor)

    • out: IMutableVector4

      The output vector to store the result (will be modified)

    Returns IMutableVector4

    The modified output vector with component-wise division result

  • Calculates the dot product of two vectors. The dot product is the sum of the products of corresponding components.

    Parameters

    Returns number

    The dot product of the two vectors

  • Creates a dummy vector with no components (empty array). This is used as a placeholder when a vector is needed but not yet initialized.

    Returns Vector4d

    A new dummy Vector4d with empty components

  • Creates a new Vector4d from an ArrayBuffer. The ArrayBuffer should contain at least 32 bytes (4 float64 values).

    Parameters

    • arrayBuffer: ArrayBuffer

      The ArrayBuffer containing vector data

    Returns Vector4d

    A new Vector4d instance using the ArrayBuffer data

  • Creates a new Vector4d from individual component values.

    Parameters

    • x: number

      The X component value

    • y: number

      The Y component value

    • z: number

      The Z component value

    • w: number

      The W component value

    Returns Vector4d

    A new Vector4d instance with the specified component values

  • Creates a new Vector4d by copying values from an array. Takes the first 4 elements from the array. If the array has fewer than 4 elements, the remaining components will be undefined.

    Parameters

    • array: Array4<number>

      Array containing numeric values (at least 4 elements recommended)

    Returns Vector4d

    A new Vector4d instance with copied values

  • Creates a new Vector4d from a 4-element array by copying the values.

    Parameters

    • array: Array4<number>

      Array containing exactly 4 numeric values [x, y, z, w]

    Returns Vector4d

    A new Vector4d instance with the copied values

  • Creates a new Vector4d from a Float64Array. The Float64Array is used directly without copying.

    Parameters

    • float64Array: Float64Array

      The Float64Array containing vector components

    Returns Vector4d

    A new Vector4d instance using the provided Float64Array

  • Calculates the squared length (magnitude) of a vector. This is more efficient than length() when only comparing magnitudes.

    Parameters

    • vec: IVector4

      The vector to calculate squared length for

    Returns number

    The squared length of the vector

  • Multiplies a vector by a scalar value and returns a new vector. This operation scales the vector while preserving its direction.

    Parameters

    • vec: IVector4

      The vector to multiply

    • value: number

      The scalar value to multiply by

    Returns Vector4d

    A new Vector4d with each component multiplied by the scalar

  • Multiplies a vector by a scalar value and stores the result in the output vector. This method modifies the output vector in-place for better performance.

    Parameters

    • vec: IVector4

      The vector to multiply

    • value: number

      The scalar value to multiply by

    • out: IMutableVector4

      The output vector to store the result (will be modified)

    Returns IMutableVector4

    The modified output vector with each component multiplied by the scalar

  • Multiplies two vectors component-wise and returns a new vector. This is also known as the Hadamard product or element-wise multiplication.

    Parameters

    Returns Vector4d

    A new Vector4d with each component being the product of corresponding components

  • Creates a normalized version of the given vector. A normalized vector has a length of 1 while maintaining its direction.

    Parameters

    Returns Vector4d

    A new normalized Vector4d

  • Subtracts the right vector from the left vector component-wise and returns a new vector.

    Parameters

    • l_vec: IVector4

      The left operand vector (minuend)

    • r_vec: IVector4

      The right operand vector (subtrahend)

    Returns Vector4d

    A new Vector4d containing the difference (l_vec - r_vec)

  • Subtracts the right vector from the left vector component-wise and stores the result in the output vector. This method modifies the output vector in-place for better performance.

    Parameters

    • l_vec: IVector4

      The left operand vector (minuend)

    • r_vec: IVector4

      The right operand vector (subtrahend)

    • out: IMutableVector4

      The output vector to store the result (will be modified)

    Returns IMutableVector4

    The modified output vector containing the difference