Skip to content
hhh edited this page Feb 2, 2022 · 5 revisions

Vector

/**
 * Type of vector-like objects.
 */
interface VectorLike {

    /**
     * The x component of the vector.
     */
    x: number;

    /**
     * The y component of the vector.
     */
    y: number;
}

/**
 * The class of 2D vectors.
 */
class Vector {

    /**
     * Create a vector from a vector-like object.
     */
    static from(vector: VectorLike): Vector;

    /**
     * Get a random normalized vector.
     * (Default `startAngle`: 0; `endAngle`: )
     */
    static random(startAngle?: number, endAngle?: number): Vector;

    /**
     * vector1 += vector0 * (k1 / (Math.abs(k1) + Math.abs(k2))) * scale
     * vector2 += vector0 * (k2 / (Math.abs(k1) + Math.abs(k2))) * scale
     * (Default scale: 1)
     */
    static distribute(vector0: VectorLike, vector1: VectorLike, vector2: VectorLike, k1: number, k2: number, scale?: number): void;

    /**
     * @returns Norm of (vector1 - vector2).
     */
    static distance(vector1: VectorLike, vector2: VectorLike): number;

    /**
     * Constructor of {@link Vector}.
     */
    constructor();

    /**
     * Constructor of {@link Vector}.
     */
    constructor(x: number, y: number);

    /**
     * The x component of the vector.
     */
    x: number;

    /**
     * The y component of the vector.
     */
    y: number;

    /**
     * Get the norm of the vector.
     */
    get norm(): number;

    /**
     * Set the norm of the vector.
     */
    set norm(targetNorm: number);

    /**
     * Get a copy of this vector.
     */
    clone(): Vector;

    /**
     * Normalize the vector.
     */
    normalize(): this;

    /**
     * Returns `true` if the vector is a zero vector.
     */
    isZero(): boolean;

    /**
     * Set the vector to (x, y).
     */
    set(x: number, y: number): this;

    /**
     * Set this vector to the given vector.
     */
    setVector(vector: VectorLike): this;

    /**
     * Add (dx, dy) to the vector.
     */
    add(dx: number, dy: number): this;

    /**
     * Add the given vector to this vector.
     */
    addVector(vector: VectorLike, scale?: number): this;

    /**
     * Subtract (dx, dy) from the vector.
     */
    sub(dx: number, dy: number): this;

    /**
     * Subtract the given vector from this vector.
     */
    subVector(vector: VectorLike, scale?: number): this;

    /**
     * Scale the vector.
     */
    scale(scale: number): this;

    /**
     * Scale the vector.
     */
    scale(scaleX: number, scaleY: number): this;

    /**
     * Scale the vector by providing a vector (scaleX, scaleY).
     */
    scaleVector(vector: VectorLike): this;

    /**
     * Reverse the vector.
     * ((x, y) -> (-x, -y))
     */
    reverse(): this;

    /**
     * Rotate anticlockwisely by `rad`.
     */
    rotate(rad: number): this;

    /**
     * Rotate the vector by 90 degrees.
     * (Default direction: anticlockwise)
     */
    tangent(clockwise?: boolean): this;

    /**
     * Returns the dot production of `this` and `vector`.
     */
    dot(vector: VectorLike): number;

    /**
     * Returns the cross production of `this` and `vector`.
     */
    cross(vector: VectorLike): number;

    /**
     * Get the projection of this vector on specific direction.
     */
    project(direction: VectorLike): number;

    /**
     * Get the vector projection of this vector on specific direction.
     */
    projectVector(direction: VectorLike): Vector;

    /**
     * @override Object.toString
     */
    toString(fractionDigits?: number): string;
}

Documentation of canvasom

Clone this wiki locally