Vector2
Class representing a 2D vector. A 2D vector is an ordered pair of numbers (labeled x and y), which can be used to represent a number of things, such as:
- A point in 2D space (i.e. a position on a plane).
- A direction and length across a plane. In three.js the length will always be the Euclidean distance (straight-line distance) from
(0, 0)to(x, y)and the direction is also measured from(0, 0)towards(x, y). - Any arbitrary ordered pair of numbers.
There are other things a 2D vector can be used to represent, such as momentum vectors, complex numbers and so on, however these are the most common uses in three.js.
Iterating through a Vector2 instance will yield its components (x, y) in the
corresponding order.
Code Example
const a = new THREE.Vector2( 0, 1 ); //no arguments; will be initialised to
(0, 0) const b = new THREE.Vector2( ); const d = a.distanceTo( b );
Constructor
Vector2
function Vector2( x: Float, y: Float ): void;
x - the x value of this vector. Default is 0.
y - the y value of this vector. Default is 0.
Creates a new Vector2.
Properties
height
height: Float;
Alias for .y.
isVector2
isVector2: Boolean;
Read-only flag to check if a given object is of type Vector2.
width
width: Float;
Alias for .x.
x
x: Float;
y
y: Float;
Methods
add
function add( v: Vector2 ): this;
Adds v to this vector.
addScalar
function addScalar( s: Float ): this;
Adds the scalar value .loat to this vector's .x and .y values.
addScaledVector
function addScaledVector( v: Vector2, s: Float ): this;
Adds the multiple of v and s to this vector.
addVectors
function addVectors( a: Vector2, b: Vector2 ): this;
angle
function angle( ): Float;
Computes the angle in radians of this vector with respect to the positive x-axis.
angleTo
function angleTo( v: Vector2 ): Float;
Returns the angle between this vector and vector v in radians.
applyMatrix3
function applyMatrix3( m: Matrix3 ): this;
Multiplies this vector (with an implicit 1 as the 3rd component) by m.
ceil
function ceil( ): this;
The .x and .y components of this vector are rounded up to the nearest integer value.
clamp
function clamp( min: Vector2, max: Vector2 ): this;
min - the minimum x and y values.
max - the maximum x and y values in the desired range
If this vector's x or y value is greater than the max vector's x or y value, it is replaced by the corresponding value.
If this vector's x or y value is less than the min vector's x or y value, it is replaced by the corresponding value.
clampLength
function clampLength( min: Float, max: Float ): this;
min - the minimum value the length will be clamped to
max - the maximum value the length will be clamped to
If this vector's length is greater than the max value, it is replaced by the max value.
If this vector's length is less than the min value, it is replaced by the min value.
clampScalar
function clampScalar( min: Float, max: Float ): this;
min - the minimum value the components will be clamped to
max - the maximum value the components will be clamped to
If this vector's x or y values are greater than the max value, they are replaced by the max value.
If this vector's x or y values are less than the min value, they are replaced by the min value.
clone
function clone( ): Vector2;
Returns a new Vector2 with the same .x and .y values as this one.
copy
function copy( v: Vector2 ): this;
Copies the values of the passed Vector2's .x and .y properties to this Vector2.
distanceTo
function distanceTo( v: Vector2 ): Float;
Computes the distance from this vector to v.
manhattanDistanceTo
function manhattanDistanceTo( v: Vector2 ): Float;
Computes the Manhattan distance from this vector to v.
distanceToSquared
function distanceToSquared( v: Vector2 ): Float;
Computes the squared distance from this vector to v. If you are just comparing the distance with another distance, you should compare the distance squared instead as it is slightly more efficient to calculate.
divide
function divide( v: Vector2 ): this;
Divides this vector by v.
divideScalar
function divideScalar( s: Float ): this;
Divides this vector by scalar s.
dot
function dot( v: Vector2 ): Float;
Calculates the dot product of this vector and v.
cross
function cross( v: Vector2 ): Float;
Calculates the cross product of this vector and v. Note that a 'cross- product' in 2D is not well-defined. This function computes a geometric cross- product often used in 2D graphics
equals
function equals( v: Vector2 ): Boolean;
Returns true if the components of this vector and v
are strictly equal; false otherwise.
floor
function floor( ): this;
The components of this vector are rounded down to the nearest integer value.
fromArray
function fromArray( array: Array, offset: Integer ): this;
array - the source array.
offset - (optional) offset into the array. Default is 0.
Sets this vector's .x value to be array[ offset ] and .y value
to be array[ offset + 1 ].
fromBufferAttribute
function fromBufferAttribute( attribute: BufferAttribute, index: Integer ):
this;
attribute - the source attribute.
index - index in the attribute.
Sets this vector's .x and .y values from the .ufferAttribute.
getComponent
function getComponent( index: Integer ): Float;
index - 0 or 1.
If index equals 0 returns the .x value.
If index equals 1 returns the .y value.
length
function length( ): Float;
Computes the Euclidean length (straight-line length) from (0, 0) to (x, y).
manhattanLength
function manhattanLength( ): Float;
Computes the Manhattan length of this vector.
lengthSq
function lengthSq( ): Float;
Computes the square of the Euclidean length (straight-line length) from (0, 0) to (x, y). If you are comparing the lengths of vectors, you should compare the length squared instead as it is slightly more efficient to calculate.
lerp
function lerp( v: Vector2, alpha: Float ): this;
v - Vector2 to interpolate
towards.
alpha - interpolation factor, typically in the closed interval [0, 1].
Linearly interpolates between this vector and v, where alpha is the percent distance along the line - alpha = 0 will be this vector, and alpha = 1 will be v.
lerpVectors
function lerpVectors( v1: Vector2, v2: Vector2, alpha: Float ): this;
v1 - the starting Vector2.
v2 - Vector2 to interpolate
towards.
alpha - interpolation factor, typically in the closed interval [0, 1].
Sets this vector to be the vector linearly interpolated between v1 and v2 where alpha is the percent distance along the line connecting the two vectors - alpha = 0 will be v1, and alpha = 1 will be v2.
negate
function negate( ): this;
Inverts this vector - i.e. sets x = -x and y = -y.
normalize
function normalize( ): this;
Converts this vector to a unit vector - that is, sets it equal to a vector with the same direction as this one, but .length 1.
max
function max( v: Vector2 ): this;
If this vector's x or y value is less than v's x or y value, replace that value with the corresponding max value.
min
function min( v: Vector2 ): this;
If this vector's x or y value is greater than v's x or y value, replace that value with the corresponding min value.
multiply
function multiply( v: Vector2 ): this;
Multiplies this vector by v.
multiplyScalar
function multiplyScalar( s: Float ): this;
Multiplies this vector by scalar s.
rotateAround
function rotateAround( center: Vector2, angle: Float ): this;
center - the point around which to rotate.
angle - the angle to rotate, in radians.
Rotates this vector around center by angle radians.
round
function round( ): this;
The components of this vector are rounded to the nearest integer value.
roundToZero
function roundToZero( ): this;
The components of this vector are rounded towards zero (up if negative, down if positive) to an integer value.
set
function set( x: Float, y: Float ): this;
Sets the .x and .y components of this vector.
setComponent
function setComponent( index: Integer, value: Float ): this;
If index equals 0 set .x to .loat.
If index equals 1 set .y to .loat
setLength
function setLength( l: Float ): this;
Sets this vector to a vector with the same direction as this one, but .length .loat.
setScalar
function setScalar( scalar: Float ): this;
Sets the .x and .y values of this vector both equal to .loat.
setX
function setX( x: Float ): this;
Replaces this vector's .x value with .loat.
setY
function setY( y: Float ): this;
Replaces this vector's .y value with .loat.
sub
function sub( v: Vector2 ): this;
Subtracts v from this vector.
subScalar
function subScalar( s: Float ): this;
Subtracts .loat from this vector's .x and .y components.
subVectors
function subVectors( a: Vector2, b: Vector2 ): this;
toArray
function toArray( array: Array, offset: Integer ): Array;
array - (optional) array to store this vector to. If this is not
provided, a new array will be created.
offset - (optional) optional offset into the array.
Returns an array [x, y], or copies x and y into the provided array.
random
function random( ): this;
Sets each component of this vector to a pseudo-random value between 0 and
1, excluding 1.