[name]
Class representing a 2D [link:https://en.wikipedia.org/wiki/Vector_space 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
[link:https://en.wikipedia.org/wiki/Euclidean_distance 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.
Example
var a = new THREE.Vector2( 0, 1 );
//no arguments; will be initialised to (0, 0)
var b = new THREE.Vector2( );
var d = a.distanceTo( b );
Constructor
[name]( [param:Float x], [param:Float y] )
[page:Float x] - the x value of the vector. Default is *0*.
[page:Float y] - the y value of the vector. Default is *0*.
Creates a new [name].
Properties
[property:Boolean isVector2]
Used to check whether this or derived classes are Vector2s. Default is *true*.
You should not change this, as it is used internally for optimisation.
[property:Float height]
Alias for [page:.y y].
[property:Float width]
Alias for [page:.x x].
[property:Float x]
[property:Float y]
Methods
[method:Vector2 add]( [param:Vector2 v] )
Adds [page:Vector2 v] to this vector.
[method:Vector2 addScalar]( [param:Float s] )
Adds the scalar value [page:Float s] to this vector's [page:.x x] and [page:.y y] values.
[method:Vector2 addScaledVector]( [param:Vector2 v], [param:Float s] )
Adds the multiple of [page:Vector2 v] and [page:Float s] to this vector.
[method:Vector2 addVectors]( [param:Vector2 a], [param:Vector2 b] )
Sets this vector to [page:Vector2 a] + [page:Vector2 b].
[method:Float angle]()
Computes the angle in radians of this vector with respect to the positive x-axis.
[method:Vector2 applyMatrix3]( [param:Matrix3 m] )
Multiplies this vector (with an implicit 1 as the 3rd component) by m.
[method:Vector2 ceil]()
The [page:.x x] and [page:.y y] components of the vector are rounded up to the nearest integer value.
[method:Vector2 clamp]( [param:Vector2 min], [param:Vector2 max] )
[page:Vector2 min] - the minimum x and y values.
[page:Vector2 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.
[method:Vector2 clampLength]( [param:Float min], [param:Float max] )
[page:Float min] - the minimum value the length will be clamped to
[page:Float 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.
[method:Vector2 clampScalar]( [param:Float min], [param:Float max] )
[page:Float min] - the minimum value the components will be clamped to
[page:Float 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.
[method:Vector2 clone]()
Returns a new Vector2 with the same [page:.x x] and [page:.y y] values as this one.
[method:Vector2 copy]( [param:Vector2 v] )
Copies the values of the passed Vector2's [page:.x x] and [page:.y y]
properties to this Vector2.
[method:Float distanceTo]( [param:Vector2 v] )
Computes the distance from this vector to [page:Vector2 v].
[method:Float manhattanDistanceTo]( [param:Vector2 v] )
Computes the [link:https://en.wikipedia.org/wiki/Taxicab_geometry Manhattan distance] from this vector to [page:Vector2 v].
[method:Float distanceToSquared]( [param:Vector2 v] )
Computes the squared distance from this vector to [page:Vector2 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.
[method:Vector2 divide]( [param:Vector2 v] )
Divides this vector by [page:Vector2 v].
[method:Vector2 divideScalar]( [param:Float s] )
Divides this vector by scalar [page:Float s].
Sets vector to *( 0, 0 )* if [page:Float s] = 0.
[method:Float dot]( [param:Vector2 v] )
Calculates the [link:https://en.wikipedia.org/wiki/Dot_product dot product] of this
vector and [page:Vector2 v].
[method:Boolean equals]( [param:Vector2 v] )
Checks for strict equality of this vector and [page:Vector2 v].
[method:Vector2 floor]()
The components of the vector are rounded down to the nearest integer value.
[method:Vector2 fromArray]( [param:Array array], [param:Integer offset] )
[page:Array array] - the source array.
[page:Integer offset] - (optional) offset into the array. Default is 0.
Sets this vector's [page:.x x] value to be array[ offset ] and [page:.y y] value to be array[ offset + 1 ].
[method:Vector2 fromBufferAttribute]( [param:BufferAttribute attribute], [param:Integer index] )
[page:BufferAttribute attribute] - the source attribute.
[page:Integer index] - index in the attribute.
Sets this vector's [page:.x x] and [page:.y y] values from the [page:BufferAttribute attribute].
[method:Float getComponent]( [param:Integer index] )
[page:Integer index] - 0 or 1.
If index equals 0 returns the [page:.x x] value.
If index equals 1 returns the [page:.y y] value.
[method:Float length]()
Computes the [link:https://en.wikipedia.org/wiki/Euclidean_distance Euclidean length]
(straight-line length) from (0, 0) to (x, y).
[method:Float manhattanLength]()
Computes the [link:http://en.wikipedia.org/wiki/Taxicab_geometry Manhattan length] of this vector.
[method:Float lengthSq]()
Computes the square of the [link:https://en.wikipedia.org/wiki/Euclidean_distance 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.
[method:Vector2 lerp]( [param:Vector2 v], [param:Float alpha] )
[page:Vector2 v] - [page:Vector2] to interpolate towards.
alpha - interpolation factor in the closed interval [0, 1].
Linearly interpolates between this vector and [page:Vector2 v], where alpha is the
distance along the line - alpha = 0 will be this vector, and alpha = 1 will be [page:Vector2 v].
[method:Vector2 lerpVectors]( [param:Vector2 v1], [param:Vector2 v2], [param:Float alpha] )
[page:Vector2 v1] - the starting [page:Vector2].
[page:Vector2 v2] - [page:Vector2] to interpolate towards.
[page:Float alpha] - interpolation factor in the closed interval [0, 1].
Sets this vector to be the vector linearly interpolated between [page:Vector2 v1] and
[page:Vector2 v2] where alpha is the distance along the line connecting the two vectors
- alpha = 0 will be [page:Vector2 v1], and alpha = 1 will be [page:Vector2 v2].
[method:Vector2 negate]()
Inverts this vector - i.e. sets x = -x and y = -y.
[method:Vector2 normalize]()
Converts this vector to a [link:https://en.wikipedia.org/wiki/Unit_vector unit vector] - that is, sets it equal to the vector with the same direction
as this one, but [page:.length length] 1.
[method:Vector2 max]( [param:Vector2 v] )
If this vector's x or y value is less than [page:Vector2 v]'s x or y value, replace
that value with the corresponding max value.
[method:Vector2 min]( [param:Vector2 v] )
If this vector's x or y value is greater than [page:Vector2 v]'s x or y value, replace
that value with the corresponding min value.
[method:Vector2 multiply]( [param:Vector2 v] )
Multiplies this vector by [page:Vector2 v].
[method:Vector2 multiplyScalar]( [param:Float s] )
Multiplies this vector by scalar [page:Float s].
[method:Vector2 rotateAround]( [param:Vector2 center], [param:float angle] )
[page:Vector2 center] - the point around which to rotate.
[page:float angle] - the angle to rotate, in radians.
Rotates the vector around [page:Vector2 center] by [page:float angle] radians.
[method:Vector2 round]()
The components of the vector are rounded to the nearest integer value.
[method:Vector2 roundToZero]()
The components of the vector are rounded towards zero (up if negative, down if positive) to an integer value.
[method:Vector2 set]( [param:Float x], [param:Float y] )
Sets the [page:.x x] and [page:.y y] components of this vector.
[method:null setComponent]( [param:Integer index], [param:Float value] )
[page:Integer index] - 0 or 1.
[page:Float value] - [page:Float]
If index equals 0 set [page:.x x] to [page:Float value].
If index equals 1 set [page:.y y] to [page:Float value]
[method:Vector2 setLength]( [param:Float l] )
Sets this vector to the vector with the same direction as this one, but [page:.length length]
[page:Float l].
[method:Vector2 setScalar]( [param:Float scalar] )
Sets the [page:.x x] and [page:.y y] values of this vector both equal to [page:Float scalar].
[method:Vector2 setX]( [param:Float x] )
Replaces this vector's [page:.x x] value with [page:Float x].
[method:Vector2 setY]( [param:Float y] )
Replaces this vector's [page:.y y] value with [page:Float y].
[method:Vector2 sub]( [param:Vector2 v] )
Subtracts [page:Vector2 v] from this vector.
[method:Vector2 subScalar]( [param:Float s] )
Subtracts [page:Float s] from this vector's [page:.x x] and [page:.y y] components.
[method:Vector2 subVectors]( [param:Vector2 a], [param:Vector2 b] )
Sets this vector to [page:Vector2 a] - [page:Vector2 b].
[method:Array toArray]( [param:Array array], [param:Integer offset] )
[page:Array array] - (optional) array to store the vector to. If this is not provided, a new array will be created.
[page:Integer offset] - (optional) optional offset into the array.
Returns an array [x, y], or copies x and y into the provided [page:Array array].
Source
[link:https://github.com/mrdoob/three.js/blob/master/src/[path].js src/[path].js]