Two.Vector

Extends: Two.Events

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

Constructor

ArgumentDescription
xAny number to represent the horizontal x component of the vector.
yAny 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

ArgumentDescription
v1First Two.Vector
v2Second Two.Vector

Add two vectors together.

sub

Returns: Two.Vector

ArgumentDescription
v1First Two.Vector
v2Second Two.Vector

Subtract two vectors: v2 from v1.

ratioBetween

Returns: Number

The ratio between two points v1 and v2.

ArgumentDescription
v1First Two.Vector
v2Second Two.Vector

angleBetween

Returns: Number

The angle between points v1 and v2.

ArgumentDescription
v1First Two.Vector
v2Second Two.Vector

distanceBetween

Returns: Number

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

ArgumentDescription
v1First Two.Vector
v2Second Two.Vector

distanceBetweenSquared

Returns: Number

The squared distance between points v1 and v2.

ArgumentDescription
v1First Two.Vector
v2Second Two.Vector

x

The horizontal x-component of the vector.

y

The vertical y-component of the vector.

set

ArgumentDescription
xValue of x component
yValue of y component

copy

ArgumentDescription
vThe Two.Vector to copy

Copy the x / y components of another object Two.Vector.

clear

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

clone

Returns: Two.Vector

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

add

Overloaded

ArgumentDescription
vThe Two.Vector to add

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

add

Overloaded

ArgumentDescription
nNumber to add

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

add

Overloaded

ArgumentDescription
xNumber to add to x component
yNumber to add to y component

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

sub

Overloaded

ArgumentDescription
vThe amount as a Two.Vector to subtract

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

sub

Overloaded

ArgumentDescription
nNumber to subtract

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

sub

Overloaded

ArgumentDescription
xNumber to subtract from x component
yNumber to subtract from y component

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

multiply

Overloaded

ArgumentDescription
vThe Two.Vector to multiply

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

multiply

Overloaded

ArgumentDescription
nThe number to multiply

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

multiply

Overloaded

ArgumentDescription
xThe number to multiply to x component
yThe number to multiply to y component

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

multiplyScalar

ArgumentDescription
sThe scalar to multiply by.

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

divide

Overloaded

ArgumentDescription
vThe Two.Vector to divide

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

divide

Overloaded

ArgumentDescription
nThe number to divide

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

divide

Overloaded

ArgumentDescription
xThe number to divide on the x component
yThe number to divide on the y component

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

divideScalar

ArgumentDescription
sThe 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 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

ArgumentDescription
llength to set vector to.

Set the length of a vector.

equals

Returns: Boolean

ArgumentDescription
vThe vector to compare against.
epsAn options epsilon for precision.

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

lerp

ArgumentDescription
vThe destination vector to step towards.
tThe 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 has a good thread about this.

isZero

Returns: Boolean

ArgumentDescription
epsOptional 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

ArgumentDescription
radiansThe amount to rotate the vector by in radians.

Rotate a vector.