Crafty.math.Vector2D Class

Vector2D uses the following form:

public {Vector2D} Vector2D();
public {Vector2D} Vector2D(Vector2D);
public {Vector2D} Vector2D(Number, Number);
{Vector2D|Number=0} x
{Number=0} y

Methods

Back to top

.add()

Adds the passed vector to this vector

public {Vector2D} add(Vector2D);
{vector2D} vecRH
[Returns]

{Vector2D} this after adding

Back to top

.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);
{Vector2D} vecRH
[Returns]

{Number} the angle between the two vectors in radians

Back to top

.angleTo()

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

public {Number} angleTo(Vector2D);
{Vector2D} vecRH
[Returns]

{Number} the angle to the passed vector in radians

Back to top

.clone()

Creates and exact, numeric copy of this vector

public {Vector2D} clone();
[Returns]

{Vector2D} the new vector

Back to top

.crossProduct()

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

public {Number} crossProduct(Vector2D);
{Vector2D} vecRH
[Returns]

{Number} the resultant cross product

Back to top

.distance()

Calculates the distance from this vector to the passed vector.

public {Number} distance(Vector2D);
{Vector2D} vecRH
[Returns]

{Number} the distance between the two vectors

Back to top

.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);
{Vector2D} vecRH
[Returns]

{Number} the squared distance between the two vectors

See Also

Back to top

.divide()

Divides this vector by the passed vector.

public {Vector2D} divide(Vector2D);
{Vector2D} vecRH
[Returns]

{Vector2D} this vector after dividing

Back to top

.dotProduct()

Calculates the dot product of this and the passed vectors

public {Number} dotProduct(Vector2D);
{Vector2D} vecRH
[Returns]

{Number} the resultant dot product

Back to top

.equals()

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

public {Boolean} equals(Vector2D);
{Vector2D} vecRH
[Returns]

{Boolean} true if the vectors are equivalent

Back to top

.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[, Vector2D]);
{Vector2D} vecRH
{Vector2D} [result]

An optional parameter to save the result in

[Returns]

{Vector2D} the new normal vector

Back to top

.isZero()

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

public {Boolean} isZero();
[Returns]

{Boolean} true if this vector is equal to <0,0>

Back to top

.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]

{Number} the magnitude of this vector

Back to top

.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]

{Number} the square of the magnitude of this vector

See Also

Back to top

.multiply()

Multiplies this vector by the passed vector

public {Vector2D} multiply(Vector2D);
{Vector2D} vecRH
[Returns]

{Vector2D} this vector after multiplying

Back to top

.negate()

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

public {Vector2D} negate();
[Returns]

{Vector2D} this vector after negation

Back to top

.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]

{Vector2D} this vector after normalization

Back to top

.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]);
{Vector2D} [result]

An optional parameter to save the result in

[Returns]

{Vector2D} the perpendicular vector

Back to top

.scale()

Scales this vector by the passed amount(s) If scalarY is omitted, scalarX is used for both axes

public {Vector2D} scale(Number[, Number]);
{Number} scalarX
{Number} [scalarY]
[Returns]

{Vector2D} this after scaling

Back to top

.scaleToMagnitude()

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

public {Vector2D} scaleToMagnitude(Number);
{Number} mag
[Returns]

{Vector2D} this vector after scaling

Back to top

.setValues()

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

public {Vector2D} setValues(Vector2D);
public {Vector2D} setValues(Number, Number);
{Number|Vector2D} x
{Number} y
[Returns]

{Vector2D} this vector after setting of values

Back to top

.subtract()

Subtracts the passed vector from this vector.

public {Vector2D} subtract(Vector2D);
{Vector2D} vecRH
[Returns]

{vector2D} this vector after subtracting

Back to top

.toString()

Returns a string representation of this vector.

public {String} toString();
[Returns]

{String}

Back to top

.translate()

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

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

{Vector2D} this vector after translating

Back to top

.tripleProduct()

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

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

An optional parameter to save the result in

[Returns]

{Vector2D} the triple product as a new vector