Vector2D is a JavaScript object used to encapsulate a 2-dimensional vector. This code is used in the 2D Geometry section of this site.

fromPoints(*p1, p2*) : Vector2D;

fromPoints() returns the vector that is created by the difference of two points. p1 is subtracted from p2 to create the vector

**p1** is a Point2D that serves as the origin of the vector

**p2** is a Point2D that serves as the end-point of the vector

constructor - new Vector2D(*x, y*);

This method creates a new Vector2D object. All initialization is handled by the init() method. All parameters for this method are described below

init() initialized all properties for this object.

**x** is a number that represents the x-component of a vector.

**y** is a number that represents the y-component of a vector.

length() returns the length, or magnitude, of the vector.

dot() returns the dot product of this object and another Vector2D object.

cross() returns the cross product of this object and another Vector2D object.

unit() returns the unit vector as new Vector2D

unitEquals() converts the current vector to a unit vector. The object is returned to allow multiple vector operations to be chained together.

add() returns a new Vector2D that is the addition of this object and another Vector2D.

addEquals() adds another Vector2D to this object. The object is returned to allow multiple vector operations to be chained together.

subtract() returns a new Vector2D that is the subtraction of another Vector2D from this object.

subtractEquals(*that*): Vector2D;

subtractEquals() subtracts another Vector2D from this object. The object is returned to allow multiple vector operations to be chained together.

multiply() returns a new Vector2D equal to this object multiplied by the specified scalar amount.

multiplyEquals(*scalar*): Vector2D;

multiplyEquals() multiplies this vector's components by the specified amount. The object is returned to allow multiple vector operations to be chained together.

divide() returns a new Vector2D equal to this object divided by the specified scalar amount.

divideEquals(*scalar*): Vector2D;

divideEquals() divides this vector's components by the specified amount. The object is returned to allow multiple vector operations to be chained together.