src/vector2.c file

Functions

auto vec2_zero(void) -> vector2
Returns a vector2 with both components set to 0.0f.
auto vec2_one(void) -> vector2
Returns a vector2 with both components set to 1.0f.
auto vec2_x_axis(const vm_float_t x) -> vector2
Returns a vector2 along the x-axis (y = 0.0f).
auto vec2_y_axis(const vm_float_t y) -> vector2
Returns a vector2 along the y-axis (x = 0.0f).
auto vec2_x_scale(const vm_float_t x) -> vector2
Returns a vector2 representing x-axis scaling (y = 1.0f).
auto vec2_y_scale(const vm_float_t y) -> vector2
Returns a vector2 representing y-axis scaling (x = 1.0f).
auto vec2_scale(const vector2 v, const vm_float_t s) -> vector2
Scales a vector by a scalar component-wise.
auto vec2_add(const vector2 a, const vector2 b) -> vector2
Adds two vectors component-wise.
auto vec2_sub(const vector2 a, const vector2 b) -> vector2
Subtracts the second vector from the first component-wise.
auto vec2_mul_scalar(const vector2 v, const vm_float_t s) -> vector2
Multiplies a vector by a scalar component-wise.
auto vec2_div_scalar(const vector2 v, const vm_float_t s) -> vector2
Divides a vector by a scalar component-wise.
auto vec2_mul(const vector2 a, const vector2 b) -> vector2
Multiplies two vectors component-wise (Hadamard product).
auto vec2_neg(const vector2 v) -> vector2
Negates the vector (multiplies each component by -1.0f).
auto vec2_abs(const vector2 v) -> vector2
Returns the absolute values of each component.
auto vec2_cross(const vector2 a, const vector2 b) -> vector2
Computes the 2D cross-product as a vector.
auto vec2_normalize(const vector2 v) -> vector2
Normalizes the vector to unit length.
auto vec2_min(const vector2 a, const vector2 b) -> vector2
Returns the component-wise minimum of two vectors.
auto vec2_max(const vector2 a, const vector2 b) -> vector2
Returns the component-wise maximum of two vectors.
auto vec2_sign(const vector2 v) -> vector2
Returns the sign of each component (+1.0f, -1.0f, or 0.0f).
auto vec2_floor(const vector2 v) -> vector2
Applies floor to each component.
auto vec2_ceil(const vector2 v) -> vector2
Applies ceil to each component.
auto vec2_round(const vector2 v) -> vector2
Applies round to each component.
auto vec2_perpendicular(const vector2 v) -> vector2
Returns the perpendicular vector (90 degrees counterclockwise).
auto vec2_reflect(const vector2 v, const vector2 normal) -> vector2
Reflects vector v across the normal, storing the result in res.
auto vec2_project(const vector2 a, const vector2 b) -> vector2
Projects the first vector onto the second.
auto vec2_tangent(const vector2 v) -> vector2
Returns the tangent vector perpendicular to the input (90 degrees clockwise).
auto vec2_rotate(const vector2 v, const vm_float_t radians) -> vector2
Rotates the input vector counterclockwise by the given angle (radians).
auto vec2_rotate_deg(const vector2 v, const vm_float_t degrees) -> vector2
Rotates a vector2 by the given angle in degrees.
auto vec2_slide(const vector2 v, const vector2 normal) -> vector2
Slides the input vector tangent to the normal (removes normal component).
auto vec2_clamp(const vector2 v, const vector2 min, const vector2 max) -> vector2
Clamps vector v component-wise between min and max.
auto vec2_dot(const vector2 a, const vector2 b) -> vm_float_t
Computes the dot product of two vectors.
auto vec2_length(const vector2 v) -> vm_float_t
Computes the length (magnitude) of the vector.
auto vec2_aspect_ratio(const vector2 v) -> vm_float_t
Computes the aspect ratio of the vector (x / y).
auto vec2_distance(const vector2 a, const vector2 b) -> vm_float_t
Computes the Euclidean distance between two vectors (treated as points).
auto vec2_angle(const vector2 a, const vector2 b) -> vm_float_t
Computes the angle between two vectors (in radians, range [0, PI]).
auto vec2_lerp(const vector2 a, const vector2 b, const vm_float_t t) -> vector2
Linearly interpolates from a to b by t.
auto vec2_div(const vector2 a, const vector2 b) -> vector2
Divides two vectors component-wise.
auto vec2_add_scalar(const vector2 v, const vm_float_t s) -> vector2
Adds a scalar to each component.
auto vec2_sub_scalar(const vector2 v, const vm_float_t s) -> vector2
Subtracts a scalar from each component.
auto vec2_clamp_scalar(const vector2 v, const vm_float_t min, const vm_float_t max) -> vector2
Clamps each component to the scalar range [min, max].
auto vec2_saturate(const vector2 v) -> vector2
Clamps each component to the range [0, 1].
auto vec2_fract(const vector2 v) -> vector2
Returns the fractional part of each component.
auto vec2_refract(const vector2 incident, const vector2 normal, const vm_float_t eta) -> vector2
Computes the refraction of incident across normal with ratio eta.
auto vec2_reject(const vector2 a, const vector2 b) -> vector2
Returns the component of a orthogonal to b.
auto vec2_splat(const vm_float_t s) -> vector2
Returns a vector with every component set to s.
auto vec2_from_angle(const vm_float_t radians) -> vector2
Returns the unit vector at the given angle in radians.
auto vec2_from_angle_deg(const vm_float_t degrees) -> vector2
Creates a vector2 from an angle given in degrees.
auto vec2_rotate_around(const vector2 v, const vector2 pivot, const vm_float_t radians) -> vector2
Rotates v around pivot by angle radians.
auto vec2_rotate_around_deg(const vector2 v, const vector2 pivot, const vm_float_t degrees) -> vector2
Rotates a vector2 around a pivot point by the given angle in degrees.
auto vec2_move_toward(const vector2 current, const vector2 target, const vm_float_t max_delta) -> vector2
Moves current toward target by at most max_delta.
auto vec2_limit_length(const vector2 v, const vm_float_t max_len) -> vector2
Clamps the vector length to max_len.
auto vec2_to_vec3(const vector2 v, const vm_float_t z) -> vector3
Converts a vector2 to a vector3 using z.
auto vec2_length_squared(const vector2 v) -> vm_float_t
Returns the squared Euclidean length.
auto vec2_length_manhattan(const vector2 v) -> vm_float_t
Returns the Manhattan (L1) length.
auto vec2_length_chebyshev(const vector2 v) -> vm_float_t
Returns the Chebyshev (L-inf) length.
auto vec2_distance_squared(const vector2 a, const vector2 b) -> vm_float_t
Returns the squared Euclidean distance between a and b.
auto vec2_cross_scalar(const vector2 a, const vector2 b) -> vm_float_t
Returns the 2D cross product as a scalar (a.x*b.y - a.y*b.x).
auto vec2_heading(const vector2 v) -> vm_float_t
Returns the heading angle of the vector in radians.
auto vec2_heading_deg(const vector2 v) -> vm_float_t
Returns the heading angle of the vector in degrees.
auto vec2_min_component(const vector2 v) -> vm_float_t
Returns the smallest component.
auto vec2_max_component(const vector2 v) -> vm_float_t
Returns the largest component.
auto vec2_sum(const vector2 v) -> vm_float_t
Returns the sum of all components.
auto vec2_is_zero(const vector2 v) -> bool
Returns true if every component is zero.
auto vec2_is_normalized(const vector2 v) -> bool
Returns true if the vector has unit length.
auto vec2_near(const vector2 a, const vector2 b, const vm_float_t eps) -> bool
Returns true if a and b are within eps of each other.

Function documentation

vector2 vec2_zero(void)

Returns a vector2 with both components set to 0.0f.

Returns The zero vector2.

vector2 vec2_one(void)

Returns a vector2 with both components set to 1.0f.

Returns The one vector2.

vector2 vec2_x_axis(const vm_float_t x)

Returns a vector2 along the x-axis (y = 0.0f).

Parameters
x The x component value.
Returns The x-axis vector2.

vector2 vec2_y_axis(const vm_float_t y)

Returns a vector2 along the y-axis (x = 0.0f).

Parameters
y The y component value.
Returns The y-axis vector2.

vector2 vec2_x_scale(const vm_float_t x)

Returns a vector2 representing x-axis scaling (y = 1.0f).

Parameters
x The x scale factor.
Returns The x-scale vector2.

vector2 vec2_y_scale(const vm_float_t y)

Returns a vector2 representing y-axis scaling (x = 1.0f).

Parameters
y The y scale factor.
Returns The y-scale vector2.

vector2 vec2_scale(const vector2 v, const vm_float_t s)

Scales a vector by a scalar component-wise.

Parameters
v The input vector.
s The scalar multiplier.
Returns The scaled vector2.

vector2 vec2_add(const vector2 a, const vector2 b)

Adds two vectors component-wise.

Parameters
a The first vector.
b The second vector.
Returns The sum vector2.

vector2 vec2_sub(const vector2 a, const vector2 b)

Subtracts the second vector from the first component-wise.

Parameters
a The first vector.
b The second vector.
Returns The difference vector2.

vector2 vec2_mul_scalar(const vector2 v, const vm_float_t s)

Multiplies a vector by a scalar component-wise.

Parameters
v The input vector.
s The scalar multiplier.
Returns The scaled vector2.

vector2 vec2_div_scalar(const vector2 v, const vm_float_t s)

Divides a vector by a scalar component-wise.

Parameters
v The input vector.
s The scalar divisor (non-zero).
Returns The divided vector2.

vector2 vec2_mul(const vector2 a, const vector2 b)

Multiplies two vectors component-wise (Hadamard product).

Parameters
a The first vector.
b The second vector.
Returns The product vector2.

vector2 vec2_neg(const vector2 v)

Negates the vector (multiplies each component by -1.0f).

Parameters
v The input vector.
Returns The negated vector2.

vector2 vec2_abs(const vector2 v)

Returns the absolute values of each component.

Parameters
v The input vector.
Returns The absolute value vector2.

vector2 vec2_cross(const vector2 a, const vector2 b)

Computes the 2D cross-product as a vector.

Parameters
a The first vector.
b The second vector.
Returns The cross-product vector2.

vector2 vec2_normalize(const vector2 v)

Normalizes the vector to unit length.

Parameters
v The input vector.
Returns The normalized vector2 (unchanged if zero length).

vector2 vec2_min(const vector2 a, const vector2 b)

Returns the component-wise minimum of two vectors.

Parameters
a The first vector.
b The second vector.
Returns The minimum vector2.

vector2 vec2_max(const vector2 a, const vector2 b)

Returns the component-wise maximum of two vectors.

Parameters
a The first vector.
b The second vector.
Returns The maximum vector2.

vector2 vec2_sign(const vector2 v)

Returns the sign of each component (+1.0f, -1.0f, or 0.0f).

Parameters
v The input vector.
Returns The sign vector2.

vector2 vec2_floor(const vector2 v)

Applies floor to each component.

Parameters
v The input vector.
Returns The floored vector2.

vector2 vec2_ceil(const vector2 v)

Applies ceil to each component.

Parameters
v The input vector.
Returns The ceiled vector2.

vector2 vec2_round(const vector2 v)

Applies round to each component.

Parameters
v The input vector.
Returns The rounded vector2.

vector2 vec2_perpendicular(const vector2 v)

Returns the perpendicular vector (90 degrees counterclockwise).

Parameters
v The input vector.
Returns The perpendicular vector2.

vector2 vec2_reflect(const vector2 v, const vector2 normal)

Reflects vector v across the normal, storing the result in res.

Parameters
v The incident vector.
normal The unit normal vector.
Returns The reflected vector2.

vector2 vec2_project(const vector2 a, const vector2 b)

Projects the first vector onto the second.

Parameters
a The vector to project.
b The projection direction vector.
Returns The projected vector2.

vector2 vec2_tangent(const vector2 v)

Returns the tangent vector perpendicular to the input (90 degrees clockwise).

Parameters
v The input vector.
Returns The tangent vector2.

vector2 vec2_rotate(const vector2 v, const vm_float_t radians)

Rotates the input vector counterclockwise by the given angle (radians).

Parameters
v The input vector.
radians The rotation angle in radians.
Returns The rotated vector2.

vector2 vec2_rotate_deg(const vector2 v, const vm_float_t degrees)

Rotates a vector2 by the given angle in degrees.

Parameters
v The vector2 to rotate.
degrees The rotation angle in degrees.
Returns The rotated vector2.

vector2 vec2_slide(const vector2 v, const vector2 normal)

Slides the input vector tangent to the normal (removes normal component).

Parameters
v The input vector.
normal The unit normal vector.
Returns The slid vector2.

vector2 vec2_clamp(const vector2 v, const vector2 min, const vector2 max)

Clamps vector v component-wise between min and max.

Parameters
v The input vector.
min The minimum bounds vector.
max The maximum bounds vector.
Returns The clamped vector2.

vm_float_t vec2_dot(const vector2 a, const vector2 b)

Computes the dot product of two vectors.

Parameters
a The first vector.
b The second vector.
Returns The dot product scalar.

vm_float_t vec2_length(const vector2 v)

Computes the length (magnitude) of the vector.

Parameters
v The input vector.
Returns The length scalar.

vm_float_t vec2_aspect_ratio(const vector2 v)

Computes the aspect ratio of the vector (x / y).

Parameters
v The input vector.
Returns The aspect ratio scalar.

Returns 0.0f if y == 0.0f.

vm_float_t vec2_distance(const vector2 a, const vector2 b)

Computes the Euclidean distance between two vectors (treated as points).

Parameters
a The first point.
b The second point.
Returns The distance scalar.

vm_float_t vec2_angle(const vector2 a, const vector2 b)

Computes the angle between two vectors (in radians, range [0, PI]).

Parameters
a The first vector.
b The second vector.
Returns The angle scalar.

Returns 0.0f if either vector has zero length.

vector2 vec2_lerp(const vector2 a, const vector2 b, const vm_float_t t)

Linearly interpolates from a to b by t.

Parameters
a First input vector.
b Second input vector.
t Interpolation factor.
Returns The resulting vector2.

vector2 vec2_div(const vector2 a, const vector2 b)

Divides two vectors component-wise.

Parameters
a First input vector.
b Second input vector.
Returns The resulting vector2.

vector2 vec2_add_scalar(const vector2 v, const vm_float_t s)

Adds a scalar to each component.

Parameters
v Input vector.
s Scalar value.
Returns The resulting vector2.

vector2 vec2_sub_scalar(const vector2 v, const vm_float_t s)

Subtracts a scalar from each component.

Parameters
v Input vector.
s Scalar value.
Returns The resulting vector2.

vector2 vec2_clamp_scalar(const vector2 v, const vm_float_t min, const vm_float_t max)

Clamps each component to the scalar range [min, max].

Parameters
v Input vector.
min Lower bound.
max Upper bound.
Returns The resulting vector2.

vector2 vec2_saturate(const vector2 v)

Clamps each component to the range [0, 1].

Parameters
v Input vector.
Returns The resulting vector2.

vector2 vec2_fract(const vector2 v)

Returns the fractional part of each component.

Parameters
v Input vector.
Returns The resulting vector2.

vector2 vec2_refract(const vector2 incident, const vector2 normal, const vm_float_t eta)

Computes the refraction of incident across normal with ratio eta.

Parameters
incident Incident vector.
normal Surface normal.
eta Ratio of indices of refraction.
Returns The resulting vector2.

vector2 vec2_reject(const vector2 a, const vector2 b)

Returns the component of a orthogonal to b.

Parameters
a First input vector.
b Second input vector.
Returns The resulting vector2.

vector2 vec2_splat(const vm_float_t s)

Returns a vector with every component set to s.

Parameters
s Scalar value.
Returns The resulting vector2.

vector2 vec2_from_angle(const vm_float_t radians)

Returns the unit vector at the given angle in radians.

Parameters
radians Angle in radians.
Returns The resulting vector2.

vector2 vec2_from_angle_deg(const vm_float_t degrees)

Creates a vector2 from an angle given in degrees.

Parameters
degrees The angle in degrees.
Returns A unit vector2 representing the given angle.

The resulting vector has unit length and points in the direction specified by the angle. The angle is converted to radians internally before computing the cosine and sine.

vector2 vec2_rotate_around(const vector2 v, const vector2 pivot, const vm_float_t radians)

Rotates v around pivot by angle radians.

Parameters
v Input vector.
pivot Rotation pivot.
radians Angle in radians.
Returns The resulting vector2.

vector2 vec2_rotate_around_deg(const vector2 v, const vector2 pivot, const vm_float_t degrees)

Rotates a vector2 around a pivot point by the given angle in degrees.

Parameters
v The vector2 to rotate.
pivot The vector2 to rotate around.
degrees The rotation angle in degrees.
Returns The rotated vector2.

vector2 vec2_move_toward(const vector2 current, const vector2 target, const vm_float_t max_delta)

Moves current toward target by at most max_delta.

Parameters
current Current position.
target Target position.
max_delta Maximum distance to move.
Returns The resulting vector2.

vector2 vec2_limit_length(const vector2 v, const vm_float_t max_len)

Clamps the vector length to max_len.

Parameters
v Input vector.
max_len Maximum length.
Returns The resulting vector2.

vector3 vec2_to_vec3(const vector2 v, const vm_float_t z)

Converts a vector2 to a vector3 using z.

Parameters
v Input vector.
z Z component.
Returns The resulting vector3.

vm_float_t vec2_length_squared(const vector2 v)

Returns the squared Euclidean length.

Parameters
v Input vector.
Returns The resulting scalar.

vm_float_t vec2_length_manhattan(const vector2 v)

Returns the Manhattan (L1) length.

Parameters
v Input vector.
Returns The resulting scalar.

vm_float_t vec2_length_chebyshev(const vector2 v)

Returns the Chebyshev (L-inf) length.

Parameters
v Input vector.
Returns The resulting scalar.

vm_float_t vec2_distance_squared(const vector2 a, const vector2 b)

Returns the squared Euclidean distance between a and b.

Parameters
a First input vector.
b Second input vector.
Returns The resulting scalar.

vm_float_t vec2_cross_scalar(const vector2 a, const vector2 b)

Returns the 2D cross product as a scalar (a.x*b.y - a.y*b.x).

Parameters
a First input vector.
b Second input vector.
Returns The resulting scalar.

vm_float_t vec2_heading(const vector2 v)

Returns the heading angle of the vector in radians.

Parameters
v Input vector.
Returns The resulting scalar.

vm_float_t vec2_heading_deg(const vector2 v)

Returns the heading angle of the vector in degrees.

Parameters
v The vector2 whose heading is to be calculated.
Returns The heading angle of the vector in degrees.

Computes the angle between the positive x-axis and the vector using atan2, then converts the result from radians to degrees.

vm_float_t vec2_min_component(const vector2 v)

Returns the smallest component.

Parameters
v Input vector.
Returns The resulting scalar.

vm_float_t vec2_max_component(const vector2 v)

Returns the largest component.

Parameters
v Input vector.
Returns The resulting scalar.

vm_float_t vec2_sum(const vector2 v)

Returns the sum of all components.

Parameters
v Input vector.
Returns The resulting scalar.

bool vec2_is_zero(const vector2 v)

Returns true if every component is zero.

Parameters
v Input vector.
Returns True if every component is zero.

bool vec2_is_normalized(const vector2 v)

Returns true if the vector has unit length.

Parameters
v Input vector.
Returns True if the vector has unit length.

bool vec2_near(const vector2 a, const vector2 b, const vm_float_t eps)

Returns true if a and b are within eps of each other.

Parameters
a First input vector.
b Second input vector.
eps Distance tolerance.
Returns True if a and b are within eps.