vec2

cruft/math/vec2.js
This class defines a 2d vector.

Constructors#

vec2( x, y )#

x - Initial x value of the vector.
y - Initial y value of the vector.

let a = new vec2(1, 2);
let b = new vec2(3, 4);

static zero()#

Constructs the zero vector.

let c = vec2.zero();

Properties#

.x - Value of the x component.
.y - Value of the y component.

Static Methods#

static add( a, b )#

Adds a and b, returning the result.

let c = vec2.add(a, b);

static addScalar( a, s )#

Adds scalar value s to a, returning the result.

let c = vec2.addScalar(a, 3);

static clone( a )#

Returns clone of vector a.

let c = vec2.clone(a);

static cross( a, b )#

Calculates a cross b, returning the result.

let c = vec2.cross(a, b);

static crossScalar( a, s, left )#

Calculates the cross product on a vector and a scalar. If left, then calculates s cross a, otherwise a cross s.

let c = vec2.crossScalar(a, 5);
let d = vec2.crossScalar(b, 3, true);

static dot( a, b )#

Calculates a dot b, returning the result.

let c = vec2.dot(a, b);

static length( a )#

Calculates the length of vector a.

let len = vec2.length(a);

static lengthSquared( a )#

Calculates the length squared of vector a.

let lensq = vec.lengthSquared(a);

static negate( a )#

Returns the inverse of vector a.

let inv = vec2.negate(a);

static normalize( a )#

Calclates the normalized vector of a.

let norm = vec2.normalize(a);

static scale( a, s )#

Scales vector a by s.

let c = vec2.scale(a, 10);

static sub( a, b )#

Calculates a - b.

let c = vec2.sub(a, b);

Methods#

add( b )#

Adds vector b to this, returning this.

a.add(b);

addScalar( s )#

Adds scalar value s to this, returning this.

a.addScalar(3);

clone()#

Returns clone of this.

let c = a.clone();

copy( b )#

Copies vec2 b into this.

a.copy(b);

cross( b )#

Calculates this cross b, returning this.

a.cross(b);

crossScalar( s, left )#

Calculates the cross product on this and a scalar. If left, then calculates s cross this, otherwise this cross s.

a.crossScalar(5);
b.crossScalar(3, true);

dot( b )#

Calculates this dot b, returning this.

a.dot(b);

equals( b )#

Checks for strict equality of this and b.

let eq = a.equals(b);

length()#

Calculates the length of this.

let len = a.length();

lengthSquared()#

Calculates the length squared of this.

let lensq = a.lengthSquared();

negate()#

Inverts this vector.

a.negate();

normalize()#

Normalizes this vector.

a.normalize();

scale( s )#

Scales this by s.

a.scale(10);

sub( b )#

Subtracts this by vector b.

a.sub(b);