Operation Null Mind 0.4.1
A 2D roguelike top down shooter game made with SDL2
 
Loading...
Searching...
No Matches
vec2.c File Reference

2D vector operations library More...

#include <vec2.h>
#include <math.h>

Macros

#define M_PI   3.14159265358979323846f
 

Functions

float Vec2_Magnitude (Vec2 v)
 [Utility] Returns the magnitude of a vector.
 
float Vec2_Distance (Vec2 a, Vec2 b)
 [Utility] Returns the magnitude of a vector.
 
Vec2 Vec2_Normalize (Vec2 v)
 [Utility] Returns the normalized vector of the input vector.
 
Vec2 Vec2_Add (Vec2 a, Vec2 b)
 [Utility] Adds two vectors.
 
Vec2 Vec2_Increment (Vec2 *a, Vec2 b)
 [Utility] Increments the vector pointed to by a with vector b.
 
Vec2 Vec2_Subtract (Vec2 a, Vec2 b)
 [Utility] Subtracts vector b from vector a.
 
Vec2 Vec2_Decrement (Vec2 *a, Vec2 b)
 [Utility] Decrements the vector pointed to by a by vector b.
 
Vec2 Vec2_Multiply (Vec2 v, float scalar)
 [Utility] Multiplies a vector by a scalar.
 
Vec2 Vec2_Divide (Vec2 v, float scalar)
 [Utility] Divides a vector by a scalar.
 
float Vec2_Dot (Vec2 a, Vec2 b)
 [Utility] Returns the dot product of two vectors.
 
Vec2 Vec2_Lerp (Vec2 a, Vec2 b, float t)
 [Utility] Linearly interpolates between vectors a and b.
 
Vec2 Vec2_RotateRadians (Vec2 v, float angle)
 [Utility] Rotates a vector around the origin by a given angle (in radians).
 
Vec2 Vec2_RotateDegrees (Vec2 v, float angle)
 [Utility] Rotates a vector around the origin by a given angle (in degrees).
 
Vec2 Vec2_RotateAroundRadians (Vec2 point, Vec2 center, float angle)
 [Utility] Rotates a point around a center by a given angle (in radians).
 
Vec2 Vec2_RotateAroundDegrees (Vec2 point, Vec2 center, float angle)
 [Utility] Rotates a point around a center by a given angle (in degrees).
 
SDL_Rect Vec2_ToRect (Vec2 position, Vec2 size)
 [Utility] Converts a position and size into an SDL_Rect.
 
SDL_Rect Vec2_ToCenteredRect (Vec2 position, Vec2 size)
 [Utility] Converts a position and size into a centered SDL_Rect.
 
Vec2 Vec2_ToCenteredPosition (Vec2 position, Vec2 size)
 [Utility] Converts a position and size to a centered position.
 
void Vec2_FromRect (SDL_Rect rect, Vec2 *position, Vec2 *size)
 [Utility] Extracts a position and size from an SDL_Rect.
 
Vec2 Vec2_Clamp (Vec2 v, Vec2 min, Vec2 max)
 [Utility] Clamps a vector between min and max vectors.
 
Vec2 Vec2_Max (Vec2 a, Vec2 b)
 [Utility] Returns a vector that is the component-wise maximum of a and b.
 
Vec2 Vec2_Min (Vec2 a, Vec2 b)
 [Utility] Returns a vector that is the component-wise minimum of a and b.
 
Vec2 Vec2_Abs (Vec2 v)
 [Utility] Returns the absolute values of a vector's components.
 
Vec2 Vec2_FromPoint (SDL_Point point)
 [Utility] Converts an SDL_Point to a Vec2.
 
SDL_Point Vec2_ToPoint (Vec2 v)
 [Utility] Converts a Vec2 to an SDL_Point.
 
bool Vec2_AreEqual (Vec2 a, Vec2 b)
 
Vec2 Vec2_Square (float side)
 
SDL_Rect Vec2_ToSquareRect (Vec2 position, float side)
 
SDL_Rect Vec2_ToCenteredSquareRect (Vec2 position, float side)
 
float Vec2_AngleBetween (Vec2 a, Vec2 b)
 Calculates the angle between two vectors in degrees.
 

Detailed Description

2D vector operations library

Implements a comprehensive set of vector operations for 2D space, supporting game physics, transformations, and coordinate handling.

Author
Mango
Date
2025-02-28

Macro Definition Documentation

◆ M_PI

#define M_PI   3.14159265358979323846f

Function Documentation

◆ Vec2_Abs()

Vec2 Vec2_Abs ( Vec2  v)

[Utility] Returns the absolute values of a vector's components.

Get the absolute value of a vector.

Parameters
vThe vector to compute absolute values for.
Returns
A vector where each component is the absolute value of v.

◆ Vec2_Add()

Vec2 Vec2_Add ( Vec2  a,
Vec2  b 
)

[Utility] Adds two vectors.

Add two vectors.

Parameters
aThe first vector.
bThe second vector.
Returns
The resulting vector from a + b.

◆ Vec2_AngleBetween()

float Vec2_AngleBetween ( Vec2  a,
Vec2  b 
)

Calculates the angle between two vectors in degrees.

Parameters
aThe first vector.
bThe second vector.
Returns
The angle in degrees between the two vectors. This means that Vec2_RotateDegrees(a, angle) will be equal to b.

◆ Vec2_AreEqual()

bool Vec2_AreEqual ( Vec2  a,
Vec2  b 
)

◆ Vec2_Clamp()

Vec2 Vec2_Clamp ( Vec2  v,
Vec2  min,
Vec2  max 
)

[Utility] Clamps a vector between min and max vectors.

Clamp a vector between two vectors.

Ensures the vector's components stay within the provided bounds.

Parameters
vThe vector to clamp.
minThe minimum values.
maxThe maximum values.
Returns
The clamped vector.

◆ Vec2_Decrement()

Vec2 Vec2_Decrement ( Vec2 *  a,
Vec2  b 
)

[Utility] Decrements the vector pointed to by a by vector b.

Decrement vector a by vector b.

Parameters
aPointer to the vector to decrement.
bThe vector by which a will be decremented.
Returns
The updated vector a after subtraction.

◆ Vec2_Distance()

float Vec2_Distance ( Vec2  a,
Vec2  b 
)

[Utility] Returns the magnitude of a vector.

Calculate the distance between two vectors.

Magnitude is the length of a vector.

Parameters
vThe vector to get the magnitude of.
Returns
The magnitude (length) of vector v.

◆ Vec2_Divide()

Vec2 Vec2_Divide ( Vec2  v,
float  scalar 
)

[Utility] Divides a vector by a scalar.

Divide a vector by a scalar.

Parameters
vThe vector to divide.
scalarThe scalar divisor.
Returns
The resulting vector v / scalar.

◆ Vec2_Dot()

float Vec2_Dot ( Vec2  a,
Vec2  b 
)

[Utility] Returns the dot product of two vectors.

Calculate the dot product of two vectors.

Parameters
aThe first vector.
bThe second vector.
Returns
The dot product (a.x * b.x + a.y * b.y).

◆ Vec2_FromPoint()

Vec2 Vec2_FromPoint ( SDL_Point  point)

[Utility] Converts an SDL_Point to a Vec2.

Convert an SDL_Point to a Vec2.

Parameters
pointThe SDL_Point to convert.
Returns
The corresponding Vec2.

◆ Vec2_FromRect()

void Vec2_FromRect ( SDL_Rect  rect,
Vec2 *  position,
Vec2 *  size 
)

[Utility] Extracts a position and size from an SDL_Rect.

Extract position and size from an SDL_Rect.

Parameters
rectThe SDL_Rect to decompose.
positionPointer to store the position.
sizePointer to store the size.

◆ Vec2_Increment()

Vec2 Vec2_Increment ( Vec2 *  a,
Vec2  b 
)

[Utility] Increments the vector pointed to by a with vector b.

Increment vector a by vector b.

Parameters
aPointer to the vector to increment.
bThe vector to add.
Returns
The updated vector a (a + b).

◆ Vec2_Lerp()

Vec2 Vec2_Lerp ( Vec2  a,
Vec2  b,
float  t 
)

[Utility] Linearly interpolates between vectors a and b.

Linearly interpolate between two vectors.

Parameters
aThe start vector.
bThe destination vector.
tThe interpolation factor (0.0 to 1.0).
Returns
The interpolated vector between a and b.

◆ Vec2_Magnitude()

float Vec2_Magnitude ( Vec2  v)

[Utility] Returns the magnitude of a vector.

Calculate the magnitude (length) of a vector.

Magnitude is the length of a vector.

Parameters
vThe vector to get the magnitude of.
Returns
The magnitude (length) of vector v.

◆ Vec2_Max()

Vec2 Vec2_Max ( Vec2  a,
Vec2  b 
)

[Utility] Returns a vector that is the component-wise maximum of a and b.

Get the maximum of two vectors.

Parameters
aThe first vector.
bThe second vector.
Returns
A vector of max components.

◆ Vec2_Min()

Vec2 Vec2_Min ( Vec2  a,
Vec2  b 
)

[Utility] Returns a vector that is the component-wise minimum of a and b.

Get the minimum of two vectors.

Parameters
aThe first vector.
bThe second vector.
Returns
A vector of min components.

◆ Vec2_Multiply()

Vec2 Vec2_Multiply ( Vec2  v,
float  scalar 
)

[Utility] Multiplies a vector by a scalar.

Multiply a vector by a scalar.

Parameters
vThe vector to multiply.
scalarThe scalar multiplier.
Returns
The resulting vector v * scalar.

◆ Vec2_Normalize()

Vec2 Vec2_Normalize ( Vec2  v)

[Utility] Returns the normalized vector of the input vector.

Normalize a vector (make it unit length)

A normalized vector has a magnitude of 1 and the same direction as v.

Parameters
vThe vector to normalize.
Returns
The normalized vector of v.

◆ Vec2_RotateAroundDegrees()

Vec2 Vec2_RotateAroundDegrees ( Vec2  point,
Vec2  center,
float  angle 
)

[Utility] Rotates a point around a center by a given angle (in degrees).

Rotate a point around a center by an angle in degrees.

Parameters
pointThe point to rotate.
centerThe pivot for rotation.
angleThe rotation angle in degrees.
Returns
The rotated point.

◆ Vec2_RotateAroundRadians()

Vec2 Vec2_RotateAroundRadians ( Vec2  point,
Vec2  center,
float  angle 
)

[Utility] Rotates a point around a center by a given angle (in radians).

Rotate a point around a center by an angle in radians.

Translates the point to origin-relative, rotates it, then translates back.

Parameters
pointThe point to rotate.
centerThe pivot for rotation.
angleThe rotation angle in radians.
Returns
The rotated point.

◆ Vec2_RotateDegrees()

Vec2 Vec2_RotateDegrees ( Vec2  v,
float  angle 
)

[Utility] Rotates a vector around the origin by a given angle (in degrees).

Rotate a vector by an angle in degrees.

Parameters
vThe vector to rotate.
angleThe angle in degrees.
Returns
The rotated vector.

◆ Vec2_RotateRadians()

Vec2 Vec2_RotateRadians ( Vec2  v,
float  angle 
)

[Utility] Rotates a vector around the origin by a given angle (in radians).

Rotate a vector by an angle in radians.

Parameters
vThe vector to rotate.
angleThe angle to rotate by in radians.
Returns
The rotated vector.

◆ Vec2_Square()

Vec2 Vec2_Square ( float  side)

◆ Vec2_Subtract()

Vec2 Vec2_Subtract ( Vec2  a,
Vec2  b 
)

[Utility] Subtracts vector b from vector a.

Subtract vector b from vector a.

Parameters
aThe vector to subtract from.
bThe vector to subtract.
Returns
The result of a - b.

◆ Vec2_ToCenteredPosition()

Vec2 Vec2_ToCenteredPosition ( Vec2  position,
Vec2  size 
)

[Utility] Converts a position and size to a centered position.

Convert a position and size to a centered position.

Useful for positioning sprites centered on a coordinate.

Parameters
positionThe top-left position.
sizeThe size of the object.
Returns
The centered position.

◆ Vec2_ToCenteredRect()

SDL_Rect Vec2_ToCenteredRect ( Vec2  position,
Vec2  size 
)

[Utility] Converts a position and size into a centered SDL_Rect.

Convert a position and size to a centered SDL_Rect.

Centers the rect at the specified position.

Parameters
positionThe center position.
sizeThe width and height.
Returns
A centered SDL_Rect.

◆ Vec2_ToCenteredSquareRect()

SDL_Rect Vec2_ToCenteredSquareRect ( Vec2  position,
float  side 
)

◆ Vec2_ToPoint()

SDL_Point Vec2_ToPoint ( Vec2  v)

[Utility] Converts a Vec2 to an SDL_Point.

Convert a Vec2 to an SDL_Point.

Parameters
vThe Vec2 to convert.
Returns
The corresponding SDL_Point.

◆ Vec2_ToRect()

SDL_Rect Vec2_ToRect ( Vec2  position,
Vec2  size 
)

[Utility] Converts a position and size into an SDL_Rect.

Convert a position and size to an SDL_Rect.

Parameters
positionThe top-left position.
sizeThe width and height.
Returns
The SDL_Rect corresponding to position and size.

◆ Vec2_ToSquareRect()

SDL_Rect Vec2_ToSquareRect ( Vec2  position,
float  side 
)