Capítulo 369 de 859

Chapter 369: Vector2

Core Idea

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 vector instance will yield its components (x, y) in the corresponding order.

Key Concepts

  • height : number: Alias for Vector2#y.
  • width : number: Alias for Vector2#x.
  • x : number: The x value of this vector.
  • y : number: The y value of this vector.

Code Examples

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 );
  • What it demonstrates: Typical usage of Vector2.

Reference Tables

Constructor

Signature
new Vector2( x : number, y : number )

Properties

PropertyDescription
.height : numberAlias for Vector2#y.
.width : numberAlias for Vector2#x.
.x : numberThe x value of this vector.
.y : numberThe y value of this vector.

Methods

MethodDescription
.add( v : Vector2 ) : Vector2Adds the given vector to this instance.
.addScalar( s : number ) : Vector2Adds the given scalar value to all components of this instance.
.addScaledVector( v : Vector2, s : number ) : Vector2Adds the given vector scaled by the given factor to this instance.
.addVectors( a : Vector2, b : Vector2 ) : Vector2Adds the given vectors and stores the result in this instance.
.angle() : numberComputes the angle in radians of this vector with respect to the positive x-axis.
.angleTo( v : Vector2 ) : numberReturns the angle between the given vector and this instance in radians.
.applyMatrix3( m : Matrix3 ) : Vector2Multiplies this vector (with an implicit 1 as the 3rd component) by the given 3x3 matrix.
.ceil() : Vector2The components of this vector are rounded up to the nearest integer value.
.clamp( min : Vector2, max : Vector2 ) : Vector2If 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…
.clampLength( min : number, max : number ) : Vector2If 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( minVal : number, maxVal : number ) : Vector2If 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() : Vector2Returns a new vector with copied values from this instance.
.copy( v : Vector2 ) : Vector2Copies the values of the given vector to this instance.
.cross( v : Vector2 ) : numberCalculates the cross product of the given vector with this instance.
.distanceTo( v : Vector2 ) : numberComputes the distance from the given vector to this instance.
.distanceToSquared( v : Vector2 ) : numberComputes the squared distance from the given vector to this instance. If you are just comparing the distance with another distance, you should compare the distance squared instead as it is slightly m…
.divide( v : Vector2 ) : Vector2Divides this instance by the given vector.
.divideScalar( scalar : number ) : Vector2Divides this vector by the given scalar.
.dot( v : Vector2 ) : numberCalculates the dot product of the given vector with this instance.
.equals( v : Vector2 ) : booleanReturns true if this vector is equal with the given one.
.floor() : Vector2The components of this vector are rounded down to the nearest integer value.
.fromArray( array : Array.<number>, offset : number ) : Vector2Sets this vector's x value to be array[ offset ] and y value to be array[ offset + 1 ].
.fromBufferAttribute( attribute : BufferAttribute, index : number ) : Vector2Sets the components of this vector from the given buffer attribute.
.getComponent( index : number ) : numberReturns the value of the vector component which matches the given index.
.length() : numberComputes the Euclidean length (straight-line length) from (0, 0) to (x, y).
.lengthSq() : numberComputes 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 …
.lerp( v : Vector2, alpha : number ) : Vector2Linearly interpolates between the given vector and this instance, where alpha is the percent distance along the line - alpha = 0 will be this vector, and alpha = 1 will be the given one.
.lerpVectors( v1 : Vector2, v2 : Vector2, alpha : number ) : Vector2Linearly interpolates between the given vectors, where alpha is the percent distance along the line - alpha = 0 will be first vector, and alpha = 1 will be the second one. The result is stored in thi…
.manhattanDistanceTo( v : Vector2 ) : numberComputes the Manhattan distance from the given vector to this instance.
.manhattanLength() : numberComputes the Manhattan length of this vector.
.max( v : Vector2 ) : Vector2If this vector's x or y value is less than the given vector's x or y value, replace that value with the corresponding max value.
.min( v : Vector2 ) : Vector2If this vector's x or y value is greater than the given vector's x or y value, replace that value with the corresponding min value.
.multiply( v : Vector2 ) : Vector2Multiplies the given vector with this instance.
.multiplyScalar( scalar : number ) : Vector2Multiplies the given scalar value with all components of this instance.
.negate() : Vector2Inverts this vector - i.e. sets x = -x and y = -y.
.normalize() : Vector2Converts this vector to a unit vector - that is, sets it equal to a vector with the same direction as this one, but with a vector length of 1.
.random() : Vector2Sets each component of this vector to a pseudo-random value between 0 and 1, excluding 1.
.rotateAround( center : Vector2, angle : number ) : Vector2Rotates this vector around the given center by the given angle.
.round() : Vector2The components of this vector are rounded to the nearest integer value
.roundToZero() : Vector2The components of this vector are rounded towards zero (up if negative, down if positive) to an integer value.
.set( x : number, y : number ) : Vector2Sets the vector components.
.setComponent( index : number, value : number ) : Vector2Allows to set a vector component with an index.
.setLength( length : number ) : Vector2Sets this vector to a vector with the same direction as this one, but with the specified length.
.setScalar( scalar : number ) : Vector2Sets the vector components to the same value.
.setX( x : number ) : Vector2Sets the vector's x component to the given value
.setY( y : number ) : Vector2Sets the vector's y component to the given value
.sub( v : Vector2 ) : Vector2Subtracts the given vector from this instance.
.subScalar( s : number ) : Vector2Subtracts the given scalar value from all components of this instance.
.subVectors( a : Vector2, b : Vector2 ) : Vector2Subtracts the given vectors and stores the result in this instance.
.toArray( array : Array.<number>, offset : number ) : Array.<number>Writes the components of this vector to the given array. If no array is provided, the method returns a new instance.

Key Takeaways

  1. Most relevant properties: height, width, x, y.
  2. Key methods: add, addScalar, addScaledVector, addVectors.

Connects To