# 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. |
# sub
Two.Vector.sub
Returns: Two.Vector
Argument | Description |
---|---|
v1 | |
v2 |
Subtract two vectors: v2
from v1
.
# ratioBetween
Two.Vector.ratioBetween
Returns: Number
The ratio betwen two points v1
and v2
.
Argument | Description |
---|---|
v1 | |
v2 |
# angleBetween
Two.Vector.angleBetween
Returns: Number
The angle between points v1
and v2
.
Argument | Description |
---|---|
v1 | |
v2 |
# distanceBetween
Two.Vector.distanceBetween
Returns: Number
The distance between points v1
and v2
. Distance is always positive.
Argument | Description |
---|---|
v1 | |
v2 |
# distanceBetweenSquared
Two.Vector.distanceBetweenSquared
Returns: Number
The squared distance between points v1
and v2
.
Argument | Description |
---|---|
v1 | |
v2 |
# clone
Two.Vector.clone
Create a new vector and copy the existing values onto the newly created instance.
# add
Two.Vector.add
Overloaded
Argument | Description |
---|---|
v |
Add an object with x / y component values to the instance.
# add
Two.Vector.add
Overloaded
Argument | Description |
---|---|
v |
Add the same number to both x / y component values of the instance.
# add
Two.Vector.add
Overloaded
Argument | Description |
---|---|
x | |
y |
Add x
/ y
values to their respective component value on the instance.
# sub
Two.Vector.sub
Overloaded
Argument | Description |
---|---|
v |
Subtract an object with x / y component values to the instance.
# sub
Two.Vector.sub
Overloaded
Argument | Description |
---|---|
v |
Subtract the same number to both x / y component values of the instance.
# sub
Two.Vector.sub
Overloaded
Argument | Description |
---|---|
x | |
y |
Subtract x
/ y
values to their respective component value on the instance.
# multiply
Two.Vector.multiply
Overloaded
Argument | Description |
---|---|
v |
Multiply an object with x / y component values to the instance.
# multiply
Two.Vector.multiply
Overloaded
Argument | Description |
---|---|
v |
Multiply the same number to both x / y component values of the instance.
# multiply
Two.Vector.multiply
Overloaded
Argument | Description |
---|---|
x | |
y |
Multiply x
/ y
values to their respective component value on the instance.
# multiplyScalar
Two.Vector.multiplyScalar
Argument | Description |
---|---|
s | The scalar to multiply by. |
Mulitiply the vector by a single number. Shorthand to call Two.Vector.multiply directly.
# divide
Two.Vector.divide
Overloaded
Argument | Description |
---|---|
v |
Divide an object with x / y component values to the instance.
# divide
Two.Vector.divide
Overloaded
Argument | Description |
---|---|
v |
Divide the same number to both x / y component values of the instance.
# divide
Two.Vector.divide
Overloaded
Argument | Description |
---|---|
x | |
y |
Divide x
/ y
values to their respective component value on the instance.
# divideScalar
Two.Vector.divideScalar
Argument | Description |
---|---|
s | The scalar to divide by. |
Divide the vector by a single number. Shorthand to call Two.Vector.divide directly.
# lengthSquared
Two.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.
# distanceToSquared
Two.Vector.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
Two.Vector.setLength
Argument | Description |
---|---|
l | length to set vector to. |
Set the length of a vector.
# equals
Two.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
Two.Vector.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
Two.Vector.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
Two.Vector.toString
Returns: String
Return a comma-separated string of x, y value. Great for storing in a database.
# toObject
Two.Vector.toObject
Returns: Object
Return a JSON compatible plain object that represents the vector.
# rotate
Two.Vector.rotate
Argument | Description |
---|---|
radians | The amount to rotate the vector by in radians. |
Rotate a vector.
← Two.Events Two.Anchor →