Options
All
• Public
• Public/Protected
• All

# Class Vector

A 2D vector on a plane.

• Vector

## Constructors

### constructor

• new Vector(x: number, y: number): Vector
• #### Parameters

• ##### x: number

X component of the Vector

• ##### y: number

Y component of the Vector

## Properties

### x

x: number

X component of the Vector

### y

y: number

Y component of the Vector

### Static Down

Down: Vector = new Vector(0, 1)

A unit vector pointing down (0, 1)

### Static Half

Half: Vector = new Vector(0.5, 0.5)

A (0.5, 0.5) vector

### Static Left

Left: Vector = new Vector(-1, 0)

A unit vector pointing left (-1, 0)

### Static One

One: Vector = new Vector(1, 1)

A (1, 1) vector

### Static Right

Right: Vector = new Vector(1, 0)

A unit vector pointing right (1, 0)

### Static Up

Up: Vector = new Vector(0, -1)

A unit vector pointing up (0, -1)

### Static Zero

Zero: Vector = new Vector(0, 0)

A (0, 0) vector

## Methods

• Adds one vector to another

#### Parameters

• ##### v: Vector

The vector to add

#### Returns Vector

• Adds one vector to this one modifying the original

#### Parameters

• ##### v: Vector

The vector to add

### average

• Returns the average (midpoint) between the current point and the specified

### clone

• Creates new vector that has the same values as the previous.

### cross

• Performs a 2D cross product with scalar. 2D cross products with a scalar return a vector.

#### Parameters

• ##### v: number

The scalar to cross

#### Returns Vector

• Performs a 2D cross product with another vector. 2D cross products return a scalar value not a vector.

#### Parameters

• ##### v: Vector

The vector to cross

### distance

• distance(v?: Vector): number
• The distance to another vector. If no other Vector is specified, this will return the magnitude.

#### Parameters

• ##### Optional v: Vector

The other vector. Leave blank to use origin vector.

### dot

• Performs a dot product with another vector

#### Parameters

• ##### v: Vector

The vector to dot

### equals

• equals(vector: Vector, tolerance?: number): boolean
• Compares this point against another and tests for equality

### magnitude

• magnitude(): number
• The magnitude (size) of the Vector

### negate

• Negate the current vector

### normal

• Returns the normal vector to this one, same as the perpendicular of length 1

### normalize

• Normalizes a vector to have a magnitude of 1.

### perpendicular

• Returns the perpendicular vector to this one

### rotate

• Rotates the current vector around a point by a certain number of degrees in radians

### scale

• Scales a vector's by a factor of size

#### Parameters

• ##### size: number

The factor to scale the magnitude by

### scaleEqual

• scaleEqual(size: number): Vector
• Scales this vector by a factor of size and modifies the original

### setTo

• setTo(x: number, y: number): void
• Sets the x and y components at once

### sub

• Subtracts a vector from another, if you subract vector `B.sub(A)` the resulting vector points from A -> B

#### Parameters

• ##### v: Vector

The vector to subtract

### subEqual

• Subtracts a vector from this one modifying the original

parallel

v The vector to subtract

### toAngle

• toAngle(): number
• Returns the angle of this vector.

### toString

• toString(): string
• Returns a string repesentation of the vector.

### Static distance

• Calculates distance between two Vectors

### Static fromAngle

• fromAngle(angle: number): Vector
• Returns a vector of unit length in the direction of the specified angle in Radians.

#### Parameters

• ##### angle: number

The angle to generate the vector

### Static isValid

• isValid(vec: Vector): boolean
• Checks if vector is not null, undefined, or if any of its components are NaN or Infinity.