DeepAR Scripting API v5.6.15

Class: Vector2

Vector2(…params)

Represents a two-dimensional vector.

Constructor

new Vector2(…params)

Constructs a new two-dimensional vector.
Parameters:
Name Type Attributes Description
params any <repeatable>
Dimensional values. See the examples below.
Examples
// Creates a (0, 0) vector.
var vec = new Vector2();
// Creates a (1.28, 1.28) vector.
var vec = new Vector2(1.28);
// Creates a (5, 8.3) vector.
var vec = new Vector2(5, 8.3);
// Copies a (1, 2) vector.
var originalVec = new Vector2(1, 2);
var vec = new Vector2(originalVec);
// Truncates a three-dimensional (1, 2, 3) vector into two-dimensional (1, 2) vector.
var baseVec = new Vector3(1, 2, 3);
var vec = new Vector2(baseVec);
// Truncates a four-dimensional (1, 2, 3, 4) vector into two-dimensional (1, 2) vector.
var baseVec = new Vector4(1, 2, 3, 4);
var vec = new Vector2(baseVec);

Members

length :double

The length of the vector.
Type:
  • double

(readonly) normalized :Vector2

Gets the normalized vector.
Type:

x :double

The X-dimensional value of the vector.
Type:
  • double

y :double

The Y-dimensional value of the vector.
Type:
  • double

Methods

add(param) → {Vector2}

Adds the vector instance and a number or another vector.
Parameters:
Name Type Description
param double | Vector2 A number or a two-dimensional vector.
Returns:
New vector containing the result.
Type
Vector2
Examples
// Adds (1, 2) and (2, 1), which results in (3, 3).
var vec = new Vector2(1, 2);
var result = vec.add(new Vector2(2, 1));
// Adds (1, 2) and 3, which results in (4, 5).
var vec = new Vector2(1, 2);
var result = vec.add(3);

clone() → {Vector2}

Clones the vector.
Returns:
Vector clone.
Type
Vector2

div(param) → {Vector2}

Divides the vector instance and a number or another vector. Same as Vector2.divide() method.
Parameters:
Name Type Description
param double | Vector2 A number or a two-dimensional vector.
Returns:
New vector containing the result.
Type
Vector2
Examples
// Divides (1, 2) and (2, 1), which results in (0.5, 2).
var vec = new Vector2(1, 2);
var result = vec.div(new Vector2(2, 1));
// Divides (1, 2) and 3, which results in (0.33333..., 0.66666...).
var vec = new Vector2(1, 2);
var result = vec.div(3);

divide(param) → {Vector2}

Divides the vector instance and a number or another vector. Same as Vector2.div() method.
Parameters:
Name Type Description
param double | Vector2 A number or a two-dimensional vector.
Returns:
New vector containing the result.
Type
Vector2
Examples
// Divides (1, 2) and (2, 1), which results in (0.5, 2).
var vec = new Vector2(1, 2);
var result = vec.divide(new Vector2(2, 1));
// Divides (1, 2) and 3, which results in (0.33333..., 0.66666...).
var vec = new Vector2(1, 2);
var result = vec.divide(3);

equals(vector, epsilonopt) → {bool}

Compares the vector instance with another vector and checks if they are equal.
Parameters:
Name Type Attributes Default Description
vector Vector2 Another two-dimensional vector.
epsilon double <optional>
1e-6 Epsilon tolerance.
Returns:
True if equal, false otherwise.
Type
bool
Examples
// Outputs 'false' to the debug console.
var a = new Vector2(1, 1);
var b = new Vector2(1, 1.001);
Debug.log(a.equals(b).toString());
// Outputs 'true' to the debug console.
var a = new Vector2(1, 1);
var b = new Vector2(1, 1.001);
Debug.log(a.equals(b, 0.01).toString());

mod(param) → {Vector2}

Reduces the modulo of dividing the vector instance and a number or another vector. Same as Vector2.modulo() method.
Parameters:
Name Type Description
param double | Vector2 A number or a two-dimensional vector.
Returns:
New vector containing the result.
Type
Vector2
Examples
// Reduces the modulo of dividing (5, 3) and (2, 3), which results in (1, 0).
var vec = new Vector2(5, 3);
var result = vec.mod(new Vector2(2, 3));
// Reduces the modulo of dividing (5, 4) and 3, which results in (2, 1).
var vec = new Vector2(5, 4);
var result = vec.mod(3);

modulo(param) → {Vector2}

Reduces the modulo of dividing the vector instance and a number or another vector. Same as Vector2.mod() method.
Parameters:
Name Type Description
param double | Vector2 A number or a two-dimensional vector.
Returns:
New vector containing the result.
Type
Vector2
Examples
// Reduces the modulo of dividing (5, 3) and (2, 3), which results in (1, 0).
var vec = new Vector2(5, 3);
var result = vec.modulo(new Vector2(2, 3));
// Reduces the modulo of dividing (5, 4) and 3, which results in (2, 1).
var vec = new Vector2(5, 4);
var result = vec.modulo(3);

mul(param) → {Vector2}

Multiplies the vector instance and a number or another vector. Same as Vector2.multiply() method.
Parameters:
Name Type Description
param double | Vector2 A number or a two-dimensional vector.
Returns:
New vector containing the result.
Type
Vector2
Examples
// Multiplies (1, 2) and (2, 1), which results in (2, 2).
var vec = new Vector2(1, 2);
var result = vec.mul(new Vector2(2, 1));
// Multiplies (1, 2) and 3, which results in (3, 6).
var vec = new Vector2(1, 2);
var result = vec.mul(3);

multiply(param) → {Vector2}

Multiplies the vector instance and a number or another vector. Same as Vector2.mul() method.
Parameters:
Name Type Description
param double | Vector2 A number or a two-dimensional vector.
Returns:
New vector containing the result.
Type
Vector2
Examples
// Multiplies (1, 2) and (2, 1), which results in (2, 2).
var vec = new Vector2(1, 2);
var result = vec.multiply(new Vector2(2, 1));
// Multiplies (1, 2) and 3, which results in (3, 6).
var vec = new Vector2(1, 2);
var result = vec.multiply(3);

sub(param) → {Vector2}

Subtracts the vector instance and a number or another vector. Same as Vector2.subtract() method.
Parameters:
Name Type Description
param double | Vector2 A number or a two-dimensional vector.
Returns:
New vector containing the result.
Type
Vector2
Examples
// Subtracts (1, 2) and (2, 1), which results in (-1, 1).
var vec = new Vector2(1, 2);
var result = vec.sub(new Vector2(2, 1));
// Subtracts (1, 2) and 3, which results in (-2, -1).
var vec = new Vector2(1, 2);
var result = vec.sub(3);

subtract(param) → {Vector2}

Subtracts the vector instance and a number or another vector. Same as Vector2.sub() method.
Parameters:
Name Type Description
param double | Vector2 A number or a two-dimensional vector.
Returns:
New vector containing the result.
Type
Vector2
Examples
// Subtracts (1, 2) and (2, 1), which results in (-1, 1).
var vec = new Vector2(1, 2);
var result = vec.subtract(new Vector2(2, 1));
// Subtracts (1, 2) and 3, which results in (-2, -1).
var vec = new Vector2(1, 2);
var result = vec.subtract(3);

toString() → {string}

Gets the string representation of a vector instance.
Returns:
Vector string representation.
Type
string
Example
// Outputs '[Vector2 (5, -3)]' to the debug console.
var vec = new Vector2(5, -3);
Debug.log(vec.toString());

toVec3(z) → {Vector3}

Creates a new three-dimensional vector instance by extending this vector instance. Same as Vector2.toVector3() method.
Parameters:
Name Type Description
z double The Z-dimensional value of the vector.
Returns:
Three-dimensional vector.
Type
Vector3
Example
// Creates a (2, -3, 1) vector from (2, -3) vector.
var a = new Vector2(2, -3);
var b = a.toVec3(1);

toVec4(z, w) → {Vector4}

Creates a new four-dimensional vector instance by extending this vector instance. Same as Vector2.toVector4() method.
Parameters:
Name Type Description
z double The Z-dimensional value of the vector.
w double The W-dimensional value of the vector.
Returns:
Four-dimensional vector.
Type
Vector4
Example
// Creates a (2, -3, 1, 0.2) vector from (2, -3) vector.
var a = new Vector2(2, -3);
var b = a.toVec4(1, 0.2);

toVector3(z) → {Vector3}

Creates a new three-dimensional vector instance by extending this vector instance. Same as Vector2.toVec3() method.
Parameters:
Name Type Description
z double The Z-dimensional value of the vector.
Returns:
Three-dimensional vector.
Type
Vector3
Example
// Creates a (2, -3, 1) vector from (2, -3) vector.
var a = new Vector2(2, -3);
var b = a.toVector3(1);

toVector4(z, w) → {Vector4}

Creates a new four-dimensional vector instance by extending this vector instance. Same as Vector2.toVec4() method.
Parameters:
Name Type Description
z double The Z-dimensional value of the vector.
w double The W-dimensional value of the vector.
Returns:
Four-dimensional vector.
Type
Vector4
Example
// Creates a (2, -3, 1, 0.2) vector from (2, -3) vector.
var a = new Vector2(2, -3);
var b = a.toVector4(1, 0.2);