A vector with three components, (xyz)\begin{pmatrix} x \ y \ z \end{pmatrix}. Can also be used to represent a two-component vector.

A Vec3 is immutable.

import { Vec3 } from '@js-draw/math';

console.log('Vector addition:', Vec3.of(1, 2, 3).plus(Vec3.of(0, 1, 0)));
console.log('Scalar multiplication:', Vec3.of(1, 2, 3).times(2));
console.log('Cross products:', Vec3.unitX.cross(Vec3.unitY));
console.log('Magnitude:', Vec3.of(1, 2, 3).length(), 'or', Vec3.of(1, 2, 3).magnitude());
console.log('Square Magnitude:', Vec3.of(1, 2, 3).magnitudeSquared());
console.log('As an array:', Vec3.unitZ.asArray());
interface Vec3 {
    x: number;
    xy: {
        x: number;
        y: number;
    };
    y: number;
    z: number;
    angle(): number;
    asArray(): [number, number, number];
    at(i: number): number;
    cross(v: Vec3): Vec3;
    distanceTo(p: Vec3): number;
    dot(v: Vec3): number;
    eq(other: Vec3, tolerance?: number): boolean;
    extend(distance: number, direction: Vec3): Vec3;
    length(): number;
    lerp(target: Vec3, fractionTo: number): Vec3;
    magnitude(): number;
    magnitudeSquared(): number;
    map(fn: ((component: number, index: number) => number)): Vec3;
    maximumEntryMagnitude(): number;
    minus(v: Vec3): Vec3;
    normalized(): Vec3;
    normalizedOrZero(): Vec3;
    orthog(): Vec3;
    plus(v: Vec3): Vec3;
    scale(other: number | Vec3): Vec3;
    squareDistanceTo(other: Vec3): number;
    times(c: number): Vec3;
    toString(): string;
    zip(other: Vec3, zip: ((componentInThis: number, componentInOther: number) => number)): Vec3;
}

Properties

x: number
xy: {
    x: number;
    y: number;
}

Returns the x, y components of this. May be implemented as a getter method.

y: number
z: number

Methods

  • Return this' angle in the XY plane (treats this as a Vec2).

    This is equivalent to Math.atan2(vec.y, vec.x).

    As such, observing that Math.atan2(-0, -1) π\approx -\pi and Math.atan2(0, -1)$\approx \pi$ the resultant angle is in the range [π,pi][-\pi, pi].

    Example:

    import { Vec2 } from '@js-draw/math';
    console.log(Vec2.of(-1, -0).angle()); // atan2(-0, -1)
    console.log(Vec2.of(-1, 0).angle());  // atan2(0, -1)
    

    Returns number

  • Returns [number, number, number]

  • Returns the vector's idxth component. For example, Vec3.of(1, 2, 3).at(1) → 2.

    Parameters

    • i: number

    Returns number

  • Interpreting this vector as a point in ℝ³, returns the distance to the point p.

    Equivalent to .minus(p).magnitude().

    Parameters

    Returns number

  • Computes the scalar product between this and v.

    In particular, a.dot(b) is equivalent to a.x * b.x + a.y * b.y + a.z * b.z.

    Parameters

    Returns number

  • Parameters

    • other: Vec3
    • Optionaltolerance: number

      The maximum difference between two components for this and [other] to be considered equal.

    Returns boolean

    Vec3.of(1, 2, 3).eq(Vec3.of(4, 5, 6), 100);  // → true
    Vec3.of(1, 2, 3).eq(Vec3.of(4, 5, 6), 0.1); // → false
    Vec3.of(1, 2, 3).eq(Vec3.of(4, 5, 6), 3); // → true
    Vec3.of(1, 2, 3).eq(Vec3.of(4, 5, 6), 3.01); // → true
    Vec3.of(1, 2, 3).eq(Vec3.of(4, 5, 6), 2.99); // → false
  • Returns this plus a vector of length distance in direction.

    Parameters

    • distance: number
    • direction: Vec3

    Returns Vec3

  • Alias for .magnitude.

    Returns number

  • Returns a vector fractionTo of the way to target from this.

    Parameters

    • target: Vec3
    • fractionTo: number

    Returns Vec3

  • Returns the length of this vector in ℝ^3.

    Returns number

  • Returns number

  • Returns a vector with each component acted on by fn.

    Parameters

    • fn: ((component: number, index: number) => number)
        • (component, index): number
        • Parameters

          • component: number
          • index: number

          Returns number

    Returns Vec3

    import { Vec3 } from '@js-draw/math';
    console.log(Vec3.of(1, 2, 3).map(val => val + 1)); // → Vec(2, 3, 4)
    
  • Returns the entry of this with the greatest magnitude.

    In other words, returns maxx:xv\max { |x| : x \in {\bf v} }, where v{\bf v} is the set of all entries of this vector.

    Example:

    import { Vec3 } from '@js-draw/math';
    console.log(Vec3.of(-1, -10, 8).maximumEntryMagnitude()); // -> 10
    

    Returns number

  • Returns a unit vector in the same direction as this.

    If this has zero length, the resultant vector has NaN components.

    Returns Vec3

  • Returns a vector orthogonal to this. If this is a Vec2, returns this rotated 90 degrees counter-clockwise.

    Returns Vec3

  • If other is a Vec3, multiplies this component-wise by other. Otherwise, if other is a number`, returns the result of scalar multiplication.

    Parameters

    • other: number | Vec3

    Returns Vec3

    Vec3.of(1, 2, 3).scale(Vec3.of(2, 4, 6)); // → Vec3(2, 8, 18)
    
  • Interpreting this vector as a point in ℝ^3, computes the square distance to another point, p.

    Equivalent to .minus(p).magnitudeSquared().

    Parameters

    Returns number

  • Parameters

    • c: number

    Returns Vec3

    A copy of this multiplied by a scalar.

  • zip Maps a component of this and a corresponding component of other to a component of the output vector.

    Parameters

    • other: Vec3
    • zip: ((componentInThis: number, componentInOther: number) => number)
        • (componentInThis, componentInOther): number
        • Parameters

          • componentInThis: number
          • componentInOther: number

          Returns number

    Returns Vec3

    const a = Vec3.of(1, 2, 3);
    const b = Vec3.of(0.5, 2.1, 2.9);

    const zipped = a.zip(b, (aComponent, bComponent) => {
    return Math.min(aComponent, bComponent);
    });

    console.log(zipped.toString()); // → Vec(0.5, 2, 2.9)
OpenSource licenses