# Two.Vector

Extends: Two.Events

A class to store x / y component vector data. In addition to storing data Two.Vector has suped up methods for commonplace mathematical operations.

# Constructor

Argument Description
x Any number to represent the horizontal x-component of the vector.
y Any number to represent the vertical y-component of the vector.

# zero

Handy reference to a vector with component values 0, 0 at all times.

# left

Handy reference to a vector with component values -1, 0 at all times.

Handy reference to a vector with component values 1, 0 at all times.

# up

Handy reference to a vector with component values 0, -1 at all times.

# down

Handy reference to a vector with component values 0, 1 at all times.

# add

Returns: Two.Vector

Argument Description
v1
v2

Add two vectors together.

# sub

Returns: Two.Vector

Argument Description
v1
v2

Subtract two vectors: v2 from v1.

# ratioBetween

Returns: Number

The ratio betwen two points v1 and v2.

Argument Description
v1
v2

# angleBetween

Returns: Number

The angle between points v1 and v2.

Argument Description
v1
v2

# distanceBetween

Returns: Number

The distance between points v1 and v2. Distance is always positive.

Argument Description
v1
v2

# distanceBetweenSquared

Returns: Number

The squared distance between points v1 and v2.

Argument Description
v1
v2

# x

The horizontal x-component of the vector.

# y

The vertical y-component of the vector.

# copy

Argument Description
v

Copy the x / y components of another object v.

# clear

Set the x / y component values of the vector to zero.

# clone

Create a new vector and copy the existing values onto the newly created instance.

# add

Overloaded

Argument Description
v

Add an object with x / y component values to the instance.

# add

Overloaded

Argument Description
v

Add the same number to both x / y component values of the instance.

# add

Overloaded

Argument Description
x
y

Add x / y values to their respective component value on the instance.

# sub

Overloaded

Argument Description
v

Subtract an object with x / y component values to the instance.

# sub

Overloaded

Argument Description
v

Subtract the same number to both x / y component values of the instance.

# sub

Overloaded

Argument Description
x
y

Subtract x / y values to their respective component value on the instance.

# multiply

Overloaded

Argument Description
v

Multiply an object with x / y component values to the instance.

# multiply

Overloaded

Argument Description
v

Multiply the same number to both x / y component values of the instance.

# multiply

Overloaded

Argument Description
x
y

Multiply x / y values to their respective component value on the instance.

# multiplyScalar

Argument Description
s The scalar to multiply by.

Mulitiply the vector by a single number. Shorthand to call Two.Vector.multiply directly.

# divide

Overloaded

Argument Description
v

Divide an object with x / y component values to the instance.

# divide

Overloaded

Argument Description
v

Divide the same number to both x / y component values of the instance.

# divide

Overloaded

Argument Description
x
y

Divide x / y values to their respective component value on the instance.

# divideScalar

Argument Description
s The scalar to divide by.

Divide the vector by a single number. Shorthand to call Two.Vector.divide directly.

# negate

Invert each component's sign value.

# dot

Returns: Number

Get the dot product (opens new window) of the vector.

# length

Returns: Number

Get the length of a vector.

# lengthSquared

Returns: Number

Get the length of the vector to the power of two. Widely used as less expensive than Two.Vector.length because it isn't square-rooting any numbers.

# normalize

Normalize the vector from negative one to one.

# distanceTo

Returns: Number

Get the distance between two vectors.

# distanceToSquared

Returns: Number

Get the distance between two vectors to the power of two. Widely used as less expensive than Two.Vector.distanceTo because it isn't square-rooting any numbers.

# setLength

Argument Description
l length to set vector to.

Set the length of a vector.

# equals

Returns: Boolean

Argument Description
v The vector to compare against.
eps An options epsilon for precision.

Qualify if one vector roughly equal another. With a margin of error defined by epsilon.

# lerp

Argument Description
v The destination vector to step towards.
t The zero to one value of how close the current vector gets to the destination vector.

Linear interpolate one vector to another by an amount t defined as a zero to one number.

See: Matt DesLauriers (opens new window) has a good thread about this.

# isZero

Returns: Boolean

Argument Description
eps Optional precision amount to check against.

Check to see if vector is roughly zero, based on the epsilon precision value.

# toString

Returns: String

Return a comma-separated string of x, y value. Great for storing in a database.

# toObject

Returns: Object

Return a JSON compatible plain object that represents the vector.

# rotate

Argument Description
radians The amount to rotate the vector by in radians.

Rotate a vector.