Home :: GUI :: Math Objects

1. Introduction
2. Reference
1. Class Methods
2. Methods
3. Properties

## Introduction

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

## Reference

### Class Methods

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

### Methods

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(x, y);

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() : float;

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

dot(that) : float;

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

cross(that) : float;

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

unit() : Vector2D;

unit() returns the unit vector as new Vector2D

unitEquals() : 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(that) : Vector2D;

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(scalar) : Vector2D;

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(scalar) : Vector2D;

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.

### Properties

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

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