Constructor
new Vector4(…params)
Constructs a new four-dimensional vector.
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
params |
any |
<repeatable> |
Dimensional values. See the examples below. |
Examples
// Creates a (0, 0, 0, 0) vector.
var vec = new Vector4();
// Creates a (4.2, 4.2, 4.2, 4.2) vector.
var vec = new Vector4(4.2);
// Creates a (-1, 2.03, -3.5, -2) vector.
var vec = new Vector4(-1, 2.03, -3.5, -2);
// Expands a two-dimensional (1, 2) vector into four-dimensional (1, 2, 3, 4) vector.
var baseVec = new Vector2(1, 2);
var vec = new Vector4(baseVec, 3, 4);
// Expands a three-dimensional (1, 2, 3) vector into four-dimensional (1, 2, 3, 4) vector.
var baseVec = new Vector3(1, 2, 3);
var vec = new Vector4(baseVec, 4);
// Copies a (1, 2, 3, 4) vector.
var originalVec = new Vector4(1, 2, 3, 4);
var vec = new Vector4(originalVec);
Members
length :double
The length of the vector.
Type:
- double
(readonly) normalized :Vector4
Gets the normalized vector.
Type:
w :double
The W-dimensional value of the vector.
Type:
- double
x :double
The X-dimensional value of the vector.
Type:
- double
y :double
The Y-dimensional value of the vector.
Type:
- double
z :double
The Z-dimensional value of the vector.
Type:
- double
Methods
add(param) → {Vector4}
Adds the vector instance and a number or another vector.
Parameters:
Name | Type | Description |
---|---|---|
param |
double | Vector4 | A number or a four-dimensional vector. |
Returns:
New vector containing the result.
- Type
- Vector4
Examples
// Adds (1, 2, 3, 4) and (4, 3, 2, 1), which results in (5, 5, 5, 5).
var vec = new Vector4(1, 2, 3, 4);
var result = vec.add(new Vector4(4, 3, 2, 1));
// Adds (1, 2, 3, 4) and 3, which results in (4, 5, 6, 7).
var vec = new Vector4(1, 2, 3, 4);
var result = vec.add(3);
clone() → {Vector4}
Clones the vector.
Returns:
Vector clone.
- Type
- Vector4
div(param) → {Vector4}
Divides the vector instance and a number or another vector. Same as Vector4.divide() method.
Parameters:
Name | Type | Description |
---|---|---|
param |
double | Vector4 | A number or a four-dimensional vector. |
Returns:
New vector containing the result.
- Type
- Vector4
Examples
// Divides (1, 2, 3, 4) and (4, 3, 2, 1), which results in (0.25, 0.66666..., 1.5, 4).
var vec = new Vector4(1, 2, 3, 4);
var result = vec.div(new Vector4(4, 3, 2, 1));
// Divides (1, 2, 3, 4) and 3, which results in (0.33333..., 0.66666..., 1, 1.33333...).
var vec = new Vector4(1, 2, 3, 4);
var result = vec.div(3);
divide(param) → {Vector4}
Divides the vector instance and a number or another vector. Same as Vector4.div() method.
Parameters:
Name | Type | Description |
---|---|---|
param |
double | Vector4 | A number or a four-dimensional vector. |
Returns:
New vector containing the result.
- Type
- Vector4
Examples
// Divides (1, 2, 3, 4) and (4, 3, 2, 1), which results in (0.25, 0.66666..., 1.5, 4).
var vec = new Vector4(1, 2, 3, 4);
var result = vec.divide(new Vector4(4, 3, 2, 1));
// Divides (1, 2, 3, 4) and 3, which results in (0.33333..., 0.66666..., 1, 1.33333...).
var vec = new Vector4(1, 2, 3, 4);
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 |
Vector4 | Another four-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 Vector4(1, 1, 1, 1);
var b = new Vector4(1, 1, 1, 1.001);
Debug.log(a.equals(b).toString());
// Outputs 'true' to the debug console.
var a = new Vector4(1, 1, 1, 1);
var b = new Vector4(1, 1, 1, 1.001);
Debug.log(a.equals(b, 0.01).toString());
mod(param) → {Vector4}
Reduces the modulo of dividing the vector instance and a number or another vector. Same as Vector4.modulo() method.
Parameters:
Name | Type | Description |
---|---|---|
param |
double | Vector4 | A number or a four-dimensional vector. |
Returns:
New vector containing the result.
- Type
- Vector4
Examples
// Reduces the modulo of dividing (1, 2, 3, 4) and (4, 3, 2, 1), which results in (1, 2, 1, 0).
var vec = new Vector4(1, 2, 3, 4);
var result = vec.mod(new Vector4(4, 3, 2, 1));
// Reduces the modulo of dividing (1, 2, 3, 4) and 3, which results in (1, 2, 0, 1).
var vec = new Vector4(1, 2, 3, 4);
var result = vec.mod(3);
modulo(param) → {Vector4}
Reduces the modulo of dividing the vector instance and a number or another vector. Same as Vector4.mod() method.
Parameters:
Name | Type | Description |
---|---|---|
param |
double | Vector4 | A number or a four-dimensional vector. |
Returns:
New vector containing the result.
- Type
- Vector4
Examples
// Reduces the modulo of dividing (1, 2, 3, 4) and (4, 3, 2, 1), which results in (1, 2, 1, 0).
var vec = new Vector4(1, 2, 3, 4);
var result = vec.modulo(new Vector4(4, 3, 2, 1));
// Reduces the modulo of dividing (1, 2, 3, 4) and 3, which results in (1, 2, 0, 1).
var vec = new Vector4(1, 2, 3, 4);
var result = vec.modulo(3);
mul(param) → {Vector4}
Multiplies the vector instance and a number or another vector. Same as Vector4.multiply() method.
Parameters:
Name | Type | Description |
---|---|---|
param |
double | Vector4 | A number or a four-dimensional vector. |
Returns:
New vector containing the result.
- Type
- Vector4
Examples
// Multiplies (1, 2, 3, 4) and (4, 3, 2, 1), which results in (4, 6, 6, 4).
var vec = new Vector4(1, 2, 3, 4);
var result = vec.mul(new Vector4(4, 3, 2, 1));
// Multiplies (1, 2, 3, 4) and 3, which results in (3, 6, 9, 12).
var vec = new Vector4(1, 2, 3, 4);
var result = vec.mul(3);
multiply(param) → {Vector4}
Multiplies the vector instance and a number or another vector. Same as Vector4.mul() method.
Parameters:
Name | Type | Description |
---|---|---|
param |
double | Vector4 | A number or a four-dimensional vector. |
Returns:
New vector containing the result.
- Type
- Vector4
Examples
// Multiplies (1, 2, 3, 4) and (4, 3, 2, 1), which results in (4, 6, 6, 4).
var vec = new Vector4(1, 2, 3, 4);
var result = vec.multiply(new Vector4(4, 3, 2, 1));
// Multiplies (1, 2, 3, 4) and 3, which results in (3, 6, 9, 12).
var vec = new Vector4(1, 2, 3, 4);
var result = vec.multiply(3);
sub(param) → {Vector4}
Subtracts the vector instance and a number or another vector. Same as Vector4.subtract() method.
Parameters:
Name | Type | Description |
---|---|---|
param |
double | Vector4 | A number or a four-dimensional vector. |
Returns:
New vector containing the result.
- Type
- Vector4
Examples
// Subtracts (1, 2, 3, 4) and (4, 3, 2, 1), which results in (-3, -1, 1, 3).
var vec = new Vector4(1, 2, 3, 4);
var result = vec.sub(new Vector4(4, 3, 2, 1));
// Subtracts (1, 2, 3, 4) and 3, which results in (-2, -1, 0, 1).
var vec = new Vector4(1, 2, 3, 4);
var result = vec.sub(3);
subtract(param) → {Vector4}
Subtracts the vector instance and a number or another vector. Same as Vector4.sub() method.
Parameters:
Name | Type | Description |
---|---|---|
param |
double | Vector4 | A number or a four-dimensional vector. |
Returns:
New vector containing the result.
- Type
- Vector4
Examples
// Subtracts (1, 2, 3, 4) and (4, 3, 2, 1), which results in (-3, -1, 1, 3).
var vec = new Vector4(1, 2, 3, 4);
var result = vec.subtract(new Vector4(4, 3, 2, 1));
// Subtracts (1, 2, 3, 4) and 3, which results in (-2, -1, 0, 1).
var vec = new Vector4(1, 2, 3, 4);
var result = vec.subtract(3);
toString() → {string}
Gets the string representation of a vector instance.
Returns:
Vector string representation.
- Type
- string
Example
// Outputs '[Vector4 (1, 2, 5, -1)]' to the debug console.
var vec = new Vector4(1, 2, 5, -1);
Debug.log(vec.toString());
toVec2() → {Vector2}
Creates a new two-dimensional vector instance by truncating this vector instance. Same as Vector4.toVector2() method.
Returns:
Two-dimensional vector.
- Type
- Vector2
Example
// Creates a (-2, 4) vector from (-2, 4, 8, -2) vector.
var a = new Vector4(-2, 4, 8, -2);
var b = a.toVec2();
toVec3(homogeneousDivideopt) → {Vector3}
Creates a new three-dimensional vector instance by truncating this vector instance. Same as Vector4.toVector3() method.
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
homogeneousDivide |
bool |
<optional> |
false | If true, the remaining three dimensional values will be divided by the W-dimensional value. |
Returns:
Three-dimensional vector.
- Type
- Vector3
Examples
// Creates a (-2, 4, 8) vector from (-2, 4, 8, -2) vector.
var a = new Vector4(-2, 4, 8, -2);
var b = a.toVec3();
// Creates a (1, -2, -4) vector from (-2, 4, 8, -2) vector (homogeneous division).
var a = new Vector4(-2, 4, 8, -2);
var b = a.toVec3(true);
toVector2() → {Vector2}
Creates a new two-dimensional vector instance by truncating this vector instance. Same as Vector4.toVec2() method.
Returns:
Two-dimensional vector.
- Type
- Vector2
Example
// Creates a (-2, 4) vector from (-2, 4, 8, -2) vector.
var a = new Vector4(-2, 4, 8, -2);
var b = a.toVector2();
toVector3(homogeneousDivideopt) → {Vector3}
Creates a new three-dimensional vector instance by truncating this vector instance. Same as Vector4.toVec3() method.
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
homogeneousDivide |
bool |
<optional> |
false | If true, the remaining three dimensional values will be divided by the W-dimensional value. |
Returns:
Three-dimensional vector.
- Type
- Vector3
Examples
// Creates a (-2, 4, 8) vector from (-2, 4, 8, -2) vector.
var a = new Vector4(-2, 4, 8, -2);
var b = a.toVector3();
// Creates a (1, -2, -4) vector from (-2, 4, 8, -2) vector (homogeneous division).
var a = new Vector4(-2, 4, 8, -2);
var b = a.toVector3(true);