UNPKG

flexible-vector

Version:

Javascript vector library with flexible accessors

303 lines (302 loc) 6.89 kB
declare class BaseVector { protected components: number[]; constructor(..._components: number[]); [Symbol.iterator]: () => Generator<number, void, unknown>; inspect(): string; /** * returns the components in an array. Note you can also use the iterator pattern */ toArray(): number[]; /** * get a component from the vector */ get(index: number): number; /** * set a component of the vector */ set(index: number, value: number): void; /** * the dimensionality of the vector */ get size(): number; /** * check whether a vector equals another */ equals(other: Vector): boolean; /** * squared norm of the vector */ n2(): number; /** * norm of the vector */ norm(): number; /** * inner product with another vector */ dot(other: Vector): number; /** * adds another vector to this one */ plus(other: Vector): Vector; /** * multiplies by a scalar */ times(scalar: number): Vector; /** * subtracts another vector from this one */ minus(other: Vector): Vector; /** * normalizes the vector to length 1 or 0 if it was already 0 */ normalize(): Vector; } declare class BaseVector2 extends BaseVector { /** * the x (0) component */ get x(): number; set x(value: number); /** * the y (1) component */ get y(): number; set y(value: number); } declare class RealVector2 extends BaseVector2 { /** * rotate the vector */ rotate(angle: number): Vector2; cross(other: Vector2): number; angle(): number; } declare class BaseVector3 extends BaseVector2 { /** * the z (2) component */ get z(): number; set z(value: number); /** * the red (0) component */ get r(): number; set r(value: number); /** * the green (1) component */ get g(): number; set g(value: number); /** * the blue (2) component */ get b(): number; set b(value: number); } declare class BaseVector4 extends BaseVector3 { /** * the alpha (3) component */ get a(): number; set a(value: number); } export interface Vector extends BaseVector { } interface BaseVector2WithAccessors extends BaseVector2 { /** * the x and y component as Vector2 */ xy: Vector2; /** * the x and z component as Vector2 */ xz: Vector2; /** * the y and x component as Vector2 */ yx: Vector2; /** * the y and z component as Vector2 */ yz: Vector2; /** * the z and x component as Vector2 */ zx: Vector2; /** * the z and y component as Vector2 */ zy: Vector2; } export interface Vector2 extends BaseVector2WithAccessors, RealVector2 { plus(other: Vector2): Vector2; times(scalar: number): Vector2; minus(other: Vector2): Vector2; normalize(): Vector2; } interface BaseVector3WithAccessors extends BaseVector2WithAccessors, BaseVector3 { /** * the red and green component as Vector2 */ rg: Vector2; /** * the red and blue component as Vector2 */ rb: Vector2; /** * the green and red component as Vector2 */ gr: Vector2; /** * the green and blue component as Vector2 */ gb: Vector2; /** * the blue and red component as Vector2 */ br: Vector2; /** * the blue and green component as Vector2 */ bg: Vector2; /** * the red, green and blue component as Vector3 */ rgb: Vector3; /** * the red, blue and green component as Vector3 */ rbg: Vector3; /** * the green, red and blue component as Vector3 */ grb: Vector3; /** * the green, blue and red component as Vector3 */ gbr: Vector3; /** * the blue, red and green component as Vector3 */ brg: Vector3; /** * the blue, green and red component as Vector3 */ bgr: Vector3; } export interface Vector3 extends BaseVector3WithAccessors { plus(other: Vector3): Vector3; times(scalar: number): Vector3; minus(other: Vector3): Vector3; normalize(): Vector3; } interface BaseVector4WithAccessors extends BaseVector3WithAccessors, BaseVector4 { /** * the red and alpha component as Vector2 */ ra: Vector2; /** * the green and alpha component as Vector2 */ ga: Vector2; /** * the blue and alpha component as Vector2 */ ba: Vector2; /** * the alpha and red component as Vector2 */ ar: Vector2; /** * the alpha and green component as Vector2 */ ag: Vector2; /** * the alpha and blue component as Vector2 */ ab: Vector2; /** * the red, green and alpha component as Vector3 */ rga: Vector3; /** * the red, blue and alpha component as Vector3 */ rba: Vector3; /** * the red, alpha and green component as Vector3 */ rag: Vector3; /** * the red, alpha and blue component as Vector3 */ rab: Vector3; /** * the green, red and alpha component as Vector3 */ gra: Vector3; /** * the green, blue and alpha component as Vector3 */ gba: Vector3; /** * the green, alpha and red component as Vector3 */ gar: Vector3; /** * the green, alpha and blue component as Vector3 */ gab: Vector3; /** * the blue, red and alpha component as Vector3 */ bra: Vector3; /** * the blue, green and alpha component as Vector3 */ bga: Vector3; /** * the blue, alpha and red component as Vector3 */ bar: Vector3; /** * the blue, alpha and green component as Vector3 */ bag: Vector3; /** * the alpha, red and green component as Vector3 */ arg: Vector3; /** * the alpha, red and blue component as Vector3 */ arb: Vector3; /** * the alpha, green and red component as Vector3 */ agr: Vector3; /** * the alpha, green and blue component as Vector3 */ agb: Vector3; /** * the alpha, blue and red component as Vector3 */ abr: Vector3; /** * the alpha, blue and green component as Vector3 */ abg: Vector3; } export interface Vector4 extends BaseVector4WithAccessors { plus(other: Vector4): Vector4; times(scalar: number): Vector4; minus(other: Vector4): Vector4; normalize(): Vector4; } export declare const vector: (...components: number[]) => Vector; export declare const vector2: (...components: number[]) => Vector2; export declare const vector3: (...components: number[]) => Vector3; export declare const vector4: (...components: number[]) => Vector4; export default vector;