# Crafty.math.Vector2D Class

Vector2D has the following constructors:

`public {Vector2D} new Vector2D();`
[Returns]

A new vector with x and y equal to 0

`public {Vector2D} new Vector2D(Number x, Number y);`
x

The initial x value

y

The initial y value

[Returns]
• A new vector with the given x and y values
`public {Vector2D} new Vector2D(Vector2D vector);`
vector

A vector to copy

[Returns]

A new vector with the copied x and y values

#### Example

``````var v1 = new Crafty.math.Vector2D(3, 5);
var v2 = new Crafty.math.Vector2D(v1);``````

#### Methods

Adds the passed vector to this vector

`public {Vector2D} add(Vector2D vecRH);`
vecRH

[Returns]

The resulting modified vector

## .angleBetween()

Calculates the angle between the passed vector and this vector, using <0,0> as the point of reference. Angles returned have the range (−π, π].

`public {Number} angleBetween(Vector2D vecRH);`
vecRH

The vector to compare

[Returns]

the angle between the two vectors in radians

## .angleTo()

Calculates the angle to the passed vector from this vector, using this vector as the point of reference.

`public {Number} angleTo(Vector2D vecRH);`
vecRH

The vector to compare

[Returns]

the angle to the passed vector in radians

## .clone()

Creates and exact, numeric copy of this vector

`public {Vector2D} clone();`
[Returns]

the new vector

## .crossProduct()

Calculates the z component of the cross product of the two vectors augmented to 3D.

`public {Number} crossProduct(Vector2D vecRH);`
vecRH

The passed vector

[Returns]

the resultant cross product

## .distance()

Calculates the distance from this vector to the passed vector.

`public {Number} distance(Vector2D vecRH);`
vecRH

The passed vector

[Returns]

the distance between the two vectors

## .distanceSq()

Calculates the squared distance from this vector to the passed vector. This function avoids calculating the square root, thus being slightly faster than .distance( ).

`public {Number} distanceSq(Vector2D vecRH);`
vecRH

The passed vector

[Returns]

the squared distance between the two vectors

## .divide()

Divides this vector by the passed vector.

`public {Vector2D} divide(Vector2D vecRH);`
vecRH

The passed vector

[Returns]

this vector after dividing

## .dotProduct()

Calculates the dot product of this and the passed vectors

`public {Number} dotProduct(Vector2D vecRH);`
vecRH

The passed vector

[Returns]

the resultant dot product

## .equals()

Determines if this vector is numerically equivalent to the passed vector.

`public {Boolean} equals(Vector2D vecRH);`
vecRH

The passed vector

[Returns]

true if the vectors are equivalent

## .getNormal()

Calculates a new right-handed unit vector that is perpendicular to the line created by this and the passed vector.

`public {Vector2D} getNormal(Vector2D vecRH[, Vector2D result]);`
vecRH

The passed vector

[result]

An optional parameter to save the result in

[Returns]

the new normal vector

## .isZero()

Determines if this vector is equal to <0,0>

`public {Boolean} isZero();`
[Returns]

true if this vector is equal to <0,0>

## .magnitude()

Calculates the magnitude of this vector. Note: Function objects in JavaScript already have a 'length' member, hence the use of magnitude instead.

`public {Number} magnitude();`
[Returns]

the magnitude of this vector

## .magnitudeSq()

Calculates the square of the magnitude of this vector. This function avoids calculating the square root, thus being slightly faster than .magnitude( ).

`public {Number} magnitudeSq();`
[Returns]

the square of the magnitude of this vector

## .multiply()

Multiplies this vector by the passed vector, using component-wise multiplciation

`public {Vector2D} multiply(Vector2D vecRH);`
vecRH

The passed vector

[Returns]

this vector after multiplying

## .negate()

Negates this vector (ie. <-x,-y>)

`public {Vector2D} negate();`
[Returns]

this vector after negation

## .normalize()

Normalizes this vector (scales the vector so that its new magnitude is 1) For vectors where magnitude is 0, <1,0> is returned.

`public {Vector2D} normalize();`
[Returns]

this vector after normalization

## .perpendicular()

Calculates a new vector that is perpendicular to this vector. The perpendicular vector has the same magnitude as this vector and is obtained by a counter-clockwise rotation of 90° of this vector.

`public {Vector2D} perpendicular([Vector2D result]);`
[result]

An optional parameter to save the result in

[Returns]

the perpendicular vector

## .scale()

Scales this vector by the passed amount(s)

`public {Vector2D} scale(Number scale);`
scale

The amount to scale by

[Returns]

this after scaling

`public {Vector2D} scale(Number scalarX, Number scalarY);`
scalarX

The amount to scale x by

[scalarY]

The amount to scale y by

[Returns]

this after scaling

## .scaleToMagnitude()

Scales this vector such that its new magnitude is equal to the passed value.

`public {Vector2D} scaleToMagnitude(Number mag);`
mag

The desired magnitude

[Returns]

this vector after scaling

## .setValues()

Sets the values of this vector using a passed vector or pair of numbers.

`public {Vector2D} setValues(Vector2D vector);`
vector

a vector to copy

[Returns]

this vector after copying the values

`public {Vector2D} setValues(Number x, Number y);`
x

The x value to set

y

The y value to set

[Returns]

this vector after setting the values

## .subtract()

Subtracts the passed vector from this vector.

`public {Vector2D} subtract(Vector2D vecRH);`
vecRH

the passed vector to subtract

[Returns]

this vector after subtracting

## .toString()

Returns a string representation of this vector.

`public {String} toString();`
[Returns]

A representation like "Vector2D(4, 7)"

## .translate()

Translates (moves) this vector by the passed amounts. If dy is omitted, dx is used for both axes.

`public {Vector2D} translate(Number dx[, Number dy]);`
dx

The amount to shift by

[dy]

The amount to shift along the y axis

[Returns]

this vector after translating

## .tripleProduct()

Calculates the triple product of three vectors. triple vector product = b(a•c) - a(b•c)

`public {Vector2D} tripleProduct(Vector2D a, Vector2D b, Vector2D c, [Vector2D result]);`
a

The first vector

b

The second vector

c

The third vector

[result]

An optional parameter to save the result in

[Returns]

the triple product as a new vector