Vector4
A 4D vector using floating-point coordinates.
Description
Section titled “Description”A 4-element structure that can be used to represent 4D coordinates or any other quadruplet of numeric values.
It uses floating-point coordinates. By default, these floating-point values use 32-bit precision, unlike [float] which is always 64-bit. If double precision is needed, compile the engine with the option precision=double.
See Vector4i for its integer counterpart.
Note: In a boolean context, a Vector4 will evaluate to false if it’s equal to Vector4(0, 0, 0, 0). Otherwise, a Vector4 will always evaluate to true.
Properties
Section titled “Properties”| Type | Name | Default |
|---|---|---|
| float | w | 0.0 |
| float | x | 0.0 |
| float | y | 0.0 |
| float | z | 0.0 |
Methods
Section titled “Methods”| Returns | Method |
|---|---|
| Vector4 | abs() |
| Vector4 | ceil() |
| Vector4 | clamp(min, max) |
| Vector4 | clampf(min, max) |
| Vector4 | cubic_interpolate(b, pre_a, post_b, weight) |
| Vector4 | cubic_interpolate_in_time(b, pre_a, post_b, weight, b_t, pre_a_t, post_b_t) |
| Vector4 | direction_to(to) |
| float | distance_squared_to(to) |
| float | distance_to(to) |
| float | dot(with) |
| Vector4 | floor() |
| Vector4 | inverse() |
| bool | is_equal_approx(to) |
| bool | is_finite() |
| bool | is_normalized() |
| bool | is_zero_approx() |
| float | length() |
| float | length_squared() |
| Vector4 | lerp(to, weight) |
| Vector4 | max(with) |
| int | max_axis_index() |
| Vector4 | maxf(with) |
| Vector4 | min(with) |
| int | min_axis_index() |
| Vector4 | minf(with) |
| Vector4 | normalized() |
| Vector4 | posmod(mod) |
| Vector4 | posmodv(modv) |
| Vector4 | round() |
| Vector4 | sign() |
| Vector4 | snapped(step) |
| Vector4 | snappedf(step) |
Enumerations and constants
Section titled “Enumerations and constants”enum Axis
Section titled “enum Axis”- AXIS_X =
0— Enumerated value for the X axis. Returned bymax_axis_index()andmin_axis_index().
- AXIS_Y =
1— Enumerated value for the Y axis. Returned bymax_axis_index()andmin_axis_index().
- AXIS_Z =
2— Enumerated value for the Z axis. Returned bymax_axis_index()andmin_axis_index().
- AXIS_W =
3— Enumerated value for the W axis. Returned bymax_axis_index()andmin_axis_index().
- ZERO =
Vector4(0, 0, 0, 0)— Zero vector, a vector with all components set to0.
- ONE =
Vector4(1, 1, 1, 1)— One vector, a vector with all components set to1.
- INF =
Vector4(inf, inf, inf, inf)— Infinity vector, a vector with all components set to@Grost.INF.
Property descriptions
Section titled “Property descriptions”float w = 0.0
The vector’s W component. Also accessible by using the index position [3].
float x = 0.0
The vector’s X component. Also accessible by using the index position [0].
float y = 0.0
The vector’s Y component. Also accessible by using the index position [1].
float z = 0.0
The vector’s Z component. Also accessible by using the index position [2].
Method descriptions
Section titled “Method descriptions”Vector4 abs() const
Returns a new vector with all components in absolute values (i.e. positive).
Vector4 ceil() const
Returns a new vector with all components rounded up (towards positive infinity).
Vector4 clamp(min: Vector4, max: Vector4) const
Returns a new vector with all components clamped between the components of min and max, by running @GlobalScope.clamp() on each component.
clampf
Section titled “clampf”Vector4 clampf(min: float, max: float) const
Returns a new vector with all components clamped between min and max, by running @GlobalScope.clamp() on each component.
cubic_interpolate
Section titled “cubic_interpolate”Vector4 cubic_interpolate(b: Vector4, pre_a: Vector4, post_b: Vector4, weight: float) const
Performs a cubic interpolation between this vector and b using pre_a and post_b as handles, and returns the result at position weight. weight is on the range of 0.0 to 1.0, representing the amount of interpolation.
cubic_interpolate_in_time
Section titled “cubic_interpolate_in_time”Vector4 cubic_interpolate_in_time(b: Vector4, pre_a: Vector4, post_b: Vector4, weight: float, b_t: float, pre_a_t: float, post_b_t: float) const
Performs a cubic interpolation between this vector and b using pre_a and post_b as handles, and returns the result at position weight. weight is on the range of 0.0 to 1.0, representing the amount of interpolation.
It can perform smoother interpolation than cubic_interpolate() by the time values.
direction_to
Section titled “direction_to”Vector4 direction_to(to: Vector4) const
Returns the normalized vector pointing from this vector to to. This is equivalent to using (b - a).normalized().
distance_squared_to
Section titled “distance_squared_to”float distance_squared_to(to: Vector4) const
Returns the squared Euclidean distance between this vector and to.
This method runs faster than distance_to(), so prefer it if you need to compare vectors or need the squared distance for some formula.
distance_to
Section titled “distance_to”float distance_to(to: Vector4) const
Returns the Euclidean distance between this vector and to.
float dot(with: Vector4) const
Returns the dot product of this vector and with.
Vector4 floor() const
Returns a new vector with all components rounded down (towards negative infinity).
inverse
Section titled “inverse”Vector4 inverse() const
Returns the inverse of the vector. This is the same as Vector4(1.0 / v.x, 1.0 / v.y, 1.0 / v.z, 1.0 / v.w).
is_equal_approx
Section titled “is_equal_approx”bool is_equal_approx(to: Vector4) const
Returns true if this vector and to are approximately equal, by running @GlobalScope.is_equal_approx() on each component.
is_finite
Section titled “is_finite”bool is_finite() const
Returns true if this vector is finite, by calling @GlobalScope.is_finite() on each component.
is_normalized
Section titled “is_normalized”bool is_normalized() const
Returns true if the vector is normalized, i.e. its length is approximately equal to 1.
is_zero_approx
Section titled “is_zero_approx”bool is_zero_approx() const
Returns true if this vector’s values are approximately zero, by running @GlobalScope.is_zero_approx() on each component.
This method is faster than using is_equal_approx() with one value as a zero vector.
length
Section titled “length”float length() const
Returns the length (magnitude) of this vector.
length_squared
Section titled “length_squared”float length_squared() const
Returns the squared length (squared magnitude) of this vector.
This method runs faster than length(), so prefer it if you need to compare vectors or need the squared distance for some formula.
Vector4 lerp(to: Vector4, weight: float) const
Returns the result of the linear interpolation between this vector and to by amount weight. weight is on the range of 0.0 to 1.0, representing the amount of interpolation.
Vector4 max(with: Vector4) const
Returns the component-wise maximum of this and with, equivalent to Vector4(maxf(x, with.x), maxf(y, with.y), maxf(z, with.z), maxf(w, with.w)).
max_axis_index
Section titled “max_axis_index”int max_axis_index() const
Returns the axis of the vector’s highest value. See AXIS_* constants. If all components are equal, this method returns AXIS_X.
Vector4 maxf(with: float) const
Returns the component-wise maximum of this and with, equivalent to Vector4(maxf(x, with), maxf(y, with), maxf(z, with), maxf(w, with)).
Vector4 min(with: Vector4) const
Returns the component-wise minimum of this and with, equivalent to Vector4(minf(x, with.x), minf(y, with.y), minf(z, with.z), minf(w, with.w)).
min_axis_index
Section titled “min_axis_index”int min_axis_index() const
Returns the axis of the vector’s lowest value. See AXIS_* constants. If all components are equal, this method returns AXIS_W.
Vector4 minf(with: float) const
Returns the component-wise minimum of this and with, equivalent to Vector4(minf(x, with), minf(y, with), minf(z, with), minf(w, with)).
normalized
Section titled “normalized”Vector4 normalized() const
Returns the result of scaling the vector to unit length. Equivalent to v / v.length(). Returns (0, 0, 0, 0) if v.length() == 0. See also is_normalized().
Note: This function may return incorrect values if the input vector length is near zero.
posmod
Section titled “posmod”Vector4 posmod(mod: float) const
Returns a vector composed of the @GlobalScope.fposmod() of this vector’s components and mod.
posmodv
Section titled “posmodv”Vector4 posmodv(modv: Vector4) const
Returns a vector composed of the @GlobalScope.fposmod() of this vector’s components and modv’s components.
Vector4 round() const
Returns a new vector with all components rounded to the nearest integer, with halfway cases rounded away from zero.
Vector4 sign() const
Returns a new vector with each component set to 1.0 if it’s positive, -1.0 if it’s negative, and 0.0 if it’s zero. The result is identical to calling @GlobalScope.sign() on each component.
snapped
Section titled “snapped”Vector4 snapped(step: Vector4) const
Returns a new vector with each component snapped to the nearest multiple of the corresponding component in step. This can also be used to round the components to an arbitrary number of decimals.
snappedf
Section titled “snappedf”Vector4 snappedf(step: float) const
Returns a new vector with each component snapped to the nearest multiple of step. This can also be used to round the components to an arbitrary number of decimals.