Vector Class
A 2d Vector implementation stolen directly from mrdoob's THREE.js Vector2d
Constructor
Vector
-
x
-
y
Parameters:
-
x
NumberThe x component of the vector
-
y
NumberThe y component of the vector
Methods
add
-
vector
Adds a vector to this one
Parameters:
-
vector
VectorThe vector to add to this one
Returns:
Returns itself.
addScalar
-
scalar
Adds a scalar value to the x and y components of this vector
Parameters:
-
scalar
NumberThe scalar value to add
Returns:
Returns itself.
addVectors
-
vector1
-
vector2
Adds two vectors to each other and stores the result in this vector
Returns:
Returns itself.
clone
()
Vector
Creates a new instance of Vector, with the same components as this vector
Returns:
Returns a new Vector with the same values
copy
-
vector
Copies the passed vector's components to this vector
Parameters:
-
vector
VectorThe vector to copy the values from
Returns:
Returns itself.
distanceTo
-
vector
Calculates the distance to the passed vector
Parameters:
-
vector
VectorThe vector to check distance to
Returns:
The distance
distanceToSquared
-
vector
Calculates the square distance to the passed vector
Parameters:
-
vector
VectorThe vector to check distance to
Returns:
The square distance
divideScalar
-
scalar
Divides the x and y components of this vector by a scalar value
Parameters:
-
scalar
NumberThe value to divide by
Returns:
Returns itself.
dot
-
vector
Performs the dot product between this vector and the passed one and returns the result
Parameters:
-
vector
Vector
Returns:
Returns the dot product
equals
-
vector
Checks if this vector is equal to another
Parameters:
-
vector
VectorThe vector to compare with
Returns:
Returns itself.
getComponent
-
index
Gets a component value of the vector
Parameters:
-
index
NumberThe index of the component to set (0 = x, 1 = y)
Returns:
Returns the component value
length
()
Number
Calculates the length of the vector
Returns:
Returns the length of the vector
lengthSq
()
Number
Calculates the square length of the vector
Returns:
Returns the square length of the vector
lerp
-
vector
-
alpha
Performs a linear interpolation between this vector and the passed vector
Parameters:
-
vector
VectorThe vector to interpolate with
-
alpha
NumberThe amount to interpolate [0-1] or extrapolate (1-]
Returns:
Returns itself.
max
-
vector
Sets this vector components to the maximum value when compared to the passed vector's components
Parameters:
-
vector
VectorThe vector to compare to
Returns:
Returns itself.
max
-
min
-
max
Clamps the vectors components to be between min and max
Parameters:
Returns:
Returns itself.
min
-
vector
Sets this vector components to the minimum value when compared to the passed vector's components
Parameters:
-
vector
VectorThe vector to compare to
Returns:
Returns itself.
multiplyScalar
-
scalar
Multiplies the x and y components of this vector by a scalar value
Parameters:
-
scalar
NumberThe value to multiply by
Returns:
Returns itself.
normalize
()
Vector
Normalizes this vector (divides by its length)
Returns:
Returns the normalized vector
rotate
-
angle
-
anchor
Rotates the vector by an arbitrary angle around an arbitrary point in space
Parameters:
-
angle
NumberThe angle in radians to rotate by
-
anchor
VectorThe anchor point to rotate around
Returns:
Returns itself.
set
-
x
-
y
Sets the value of the vector
Parameters:
-
x
NumberThe x component of the vector
-
y
NumberThe y component of the vector
Returns:
Returns itself.
setComponent
-
index
-
value
Sets a component value of the vector
Parameters:
-
index
NumberThe index of the component to set (0 = x, 1 = y)
-
value
NumberThe value to set the component to
Returns:
Returns itself.
setLength
-
length
Sets the length of the vector
Parameters:
-
length
NumberThe length to set this vector to
Returns:
Returns itself.
setX
-
x
Sets the X value of the vector
Parameters:
-
x
NumberThe x component of the vector
Returns:
Returns itself.
setY
-
y
Sets the Y value of the vector
Parameters:
-
y
NumberThe y component of the vector
Returns:
Returns itself.
sub
-
vector
Subtracts a vector from this one
Parameters:
-
vector
VectorThe vector to subtract from this one
Returns:
Returns itself.