libft
Loading...
Searching...
No Matches
Files | Data Structures | Macros | Typedefs | Functions
Math and Vector Utilities

Utility functions for angles, distances, and vector operations. More...

Files

file  angle_utils.c
 Angle conversion and normalization utilities.
 
file  distance_utils.c
 Distance computation utilities for 2D points.
 
file  ft_math.h
 Math and geometry utilities for libft.
 
file  math_utils.c
 Utility math functions for floating-point operations.
 

Data Structures

struct  s_dpoint
 
struct  s_point
 

Macros

#define M_PI   3.14159265358979323846
 

Typedefs

typedef struct s_dpoint t_dpoint
 A simple struct for storing double float coordinates.
 
typedef struct s_point t_point
 A simple struct for storing integer coordinates.
 

Functions

double clamp (double value, double min, double max)
 Clamps a value between a minimum and maximum.
 
double deg_to_rad (double degrees)
 Converts an angle from degrees to radians.
 
t_dpoint divide_vector_by_scalar (t_dpoint vec, double scalar)
 Divides a 2D vector by a scalar.
 
double ft_euclidean_dist_dpoint (t_dpoint a, t_dpoint b)
 Computes Euclidean distance between two floating-point points.
 
int ft_euclidean_dist_point (t_point a, t_point b)
 Computes Euclidean distance between two integer points.
 
double ft_manhattan_dist_dpoint (t_dpoint a, t_dpoint b)
 Computes Manhattan distance between two floating-point points.
 
int ft_manhattan_dist_point (t_point a, t_point b)
 Computes Manhattan distance between two integer points.
 
t_dpoint get_direction_vector (t_dpoint from, t_dpoint to)
 Computes the vector pointing from one point to another.
 
double get_dot_product (t_dpoint vec1, t_dpoint vec2)
 Computes the dot product of two 2D vectors.
 
double get_fractional_part (double value)
 Gets the fractional part of a floating-point number.
 
t_dpoint get_normalized_vector (t_dpoint vector)
 Returns the normalized (unit length) vector.
 
t_dpoint get_unit_direction_vector (t_dpoint from, t_dpoint to)
 Computes the normalized direction vector between two points.
 
double get_vector_angle (t_dpoint vector)
 Computes the angle of a 2D vector in radians.
 
double get_vector_angle_between (t_dpoint from, t_dpoint to)
 Computes the angle between two points as a direction vector.
 
double get_vector_length (t_dpoint vec)
 Computes the length (magnitude) of a 2D vector.
 
double rad_to_deg (double radians)
 Converts an angle from radians to degrees.
 
double wrap_angle (double angle)
 Wraps an angle in radians to the range [0, 2π).
 
double wrap_in_range (double value, double range)
 Wraps a value within the range [0, range).
 

Detailed Description

Utility functions for angles, distances, and vector operations.

This group contains helper functions for geometric calculations such as angle conversion, vector normalization, direction finding, and distance measurement between points.

Includes:

Note
All functions are pure and do not perform memory allocation.

Data Structure Documentation

◆ s_dpoint

struct s_dpoint

Data Fields

double x
 
double y
 

Field Documentation

◆ x

double s_dpoint::x

◆ y

double s_dpoint::y

◆ s_point

struct s_point

Data Fields

int x
 
int y
 

Field Documentation

◆ x

int s_point::x

◆ y

int s_point::y

Macro Definition Documentation

◆ M_PI

#define M_PI   3.14159265358979323846

#include <include/ft_math.h>

Mathematical constant π

Typedef Documentation

◆ t_dpoint

#include <include/ft_math.h>

A simple struct for storing double float coordinates.

Represents a 2D point or vector with double-precision.

  • x: the horizontal component
  • y: the vertical component

◆ t_point

#include <include/ft_math.h>

A simple struct for storing integer coordinates.

Represents a 2D point with integer precision.

  • x: the horizontal component
  • y: the vertical component

Function Documentation

◆ clamp()

double clamp ( double  value,
double  min,
double  max 
)

#include <include/ft_math.h>

Clamps a value between a minimum and maximum.

Ensures that the returned value lies within the interval [min, max]. If the input is below min, returns min. If above max, returns max. Otherwise, returns value.

Parameters
valueThe input value to clamp.
minThe minimum allowed value.
maxThe maximum allowed value.
Returns
The clamped value.

◆ deg_to_rad()

double deg_to_rad ( double  degrees)

#include <include/ft_math.h>

Converts an angle from degrees to radians.

Multiplies the given angle in degrees by π/180 to convert it into radians. This is useful when dealing with trigonometric functions that expect radians.

Parameters
degreesThe angle in degrees.
Returns
The angle in radians.

◆ divide_vector_by_scalar()

t_dpoint divide_vector_by_scalar ( t_dpoint  vec,
double  scalar 
)

#include <include/ft_math.h>

Divides a 2D vector by a scalar.

Returns a new vector with each component divided by the given scalar. This is often used for vector normalization or scaling down.

Parameters
vecThe original vector.
scalarThe scalar to divide by.
Returns
A new vector with divided components.
Warning
Does not handle division by zero.

◆ ft_euclidean_dist_dpoint()

double ft_euclidean_dist_dpoint ( t_dpoint  a,
t_dpoint  b 
)

#include <include/ft_math.h>

Computes Euclidean distance between two floating-point points.

Uses the Pythagorean theorem to compute the straight-line distance between two points represented with t_dpoint. This is useful in precise geometric contexts such as physics or rendering systems.

Parameters
aThe first point (floating-point).
bThe second point (floating-point).
Returns
The Euclidean distance as a double.

◆ ft_euclidean_dist_point()

int ft_euclidean_dist_point ( t_point  a,
t_point  b 
)

#include <include/ft_math.h>

Computes Euclidean distance between two integer points.

Applies the Pythagorean theorem to compute the straight-line distance between two 2D integer points. Internally casts to double to perform accurate calculations. The final result is truncated to an integer.

Parameters
aThe first point.
bThe second point.
Returns
The Euclidean distance as an integer (truncated).

◆ ft_manhattan_dist_dpoint()

double ft_manhattan_dist_dpoint ( t_dpoint  a,
t_dpoint  b 
)

#include <include/ft_math.h>

Computes Manhattan distance between two floating-point points.

Calculates the Manhattan distance using floating-point coordinates. This version is useful for precise 2D simulations and smooth position interpolation where integer coordinates are insufficient.

Parameters
aThe first point (floating-point).
bThe second point (floating-point).
Returns
The Manhattan distance as a double.

◆ ft_manhattan_dist_point()

int ft_manhattan_dist_point ( t_point  a,
t_point  b 
)

#include <include/ft_math.h>

Computes Manhattan distance between two integer points.

Calculates the sum of the absolute differences in the x and y coordinates. This is often used in grid-based pathfinding like A* or taxicab geometry.

Parameters
aThe first point.
bThe second point.
Returns
The Manhattan distance as an integer.

◆ get_direction_vector()

t_dpoint get_direction_vector ( t_dpoint  from,
t_dpoint  to 
)

#include <include/ft_math.h>

Computes the vector pointing from one point to another.

Returns the vector representing the displacement from from to to. Useful for direction-based operations.

Parameters
fromStarting point.
toTarget point.
Returns
A 2D vector representing the direction.

◆ get_dot_product()

double get_dot_product ( t_dpoint  vec1,
t_dpoint  vec2 
)

#include <include/ft_math.h>

Computes the dot product of two 2D vectors.

Calculates the scalar dot product as (x1 * x2 + y1 * y2). This is useful for projections, angle comparisons, and detecting perpendicular vectors.

Parameters
vec1First 2D vector.
vec2Second 2D vector.
Returns
The scalar dot product.

◆ get_fractional_part()

double get_fractional_part ( double  value)

#include <include/ft_math.h>

Gets the fractional part of a floating-point number.

Returns the part of the input after the decimal point by subtracting the floored value from the original. This is useful in periodic functions or graphical effects.

Parameters
valueThe input value.
Returns
The fractional part of the input (value - floor(value)).

◆ get_normalized_vector()

t_dpoint get_normalized_vector ( t_dpoint  vector)

#include <include/ft_math.h>

Returns the normalized (unit length) vector.

Produces a vector with length 1 pointing in the same direction as the input. If the input is zero-length, returns (0, 0).

Parameters
vectorThe input vector to normalize.
Returns
A unit vector in the same direction.
See also
get_vector_length
divide_vector_by_scalar

◆ get_unit_direction_vector()

t_dpoint get_unit_direction_vector ( t_dpoint  from,
t_dpoint  to 
)

#include <include/ft_math.h>

Computes the normalized direction vector between two points.

Returns a unit-length vector pointing from from to to. This is especially useful for movement or orientation systems.

Parameters
fromStarting point.
toTarget point.
Returns
The unit direction vector.
See also
get_direction_vector
get_normalized_vector

◆ get_vector_angle()

double get_vector_angle ( t_dpoint  vector)

#include <include/ft_math.h>

Computes the angle of a 2D vector in radians.

Uses atan2(y, x) to return the signed angle from the positive x-axis to the vector direction.

Parameters
vectorThe 2D vector.
Returns
The angle in radians.

◆ get_vector_angle_between()

double get_vector_angle_between ( t_dpoint  from,
t_dpoint  to 
)

#include <include/ft_math.h>

Computes the angle between two points as a direction vector.

Forms a vector from from to to, then calculates its angle from the x-axis using atan2. Useful for aiming or facing direction.

Parameters
fromStarting point.
toTarget point.
Returns
Angle in radians of the direction vector.
See also
get_direction_vector
get_vector_angle

◆ get_vector_length()

double get_vector_length ( t_dpoint  vec)

#include <include/ft_math.h>

Computes the length (magnitude) of a 2D vector.

Uses the Euclidean norm formula: sqrt(x² + y²) to compute the vector's magnitude. This is commonly used for normalization, distance evaluation, and vector-based physics.

Parameters
vecA 2D vector.
Returns
The magnitude (length) of the vector.

◆ rad_to_deg()

double rad_to_deg ( double  radians)

#include <include/ft_math.h>

Converts an angle from radians to degrees.

Multiplies the given angle in radians by 180/π to obtain its value in degrees. This is commonly used when interfacing with systems that expect degrees instead of radians.

Parameters
radiansThe angle in radians.
Returns
The angle in degrees.

◆ wrap_angle()

double wrap_angle ( double  angle)

#include <include/ft_math.h>

Wraps an angle in radians to the range [0, 2π).

Normalizes the angle by continuously adding or subtracting 2π until it fits within the standard interval [0, 2π). This is essential when working with periodic functions or for ensuring angular continuity in rendering and physics.

Parameters
angleThe angle in radians to normalize.
Returns
The normalized angle in the range [0, 2π).

◆ wrap_in_range()

double wrap_in_range ( double  value,
double  range 
)

#include <include/ft_math.h>

Wraps a value within the range [0, range).

Normalizes a value to be within the bounds of [0, range). If the value is below 0, adds the range. If it's greater than or equal to the range, subtracts the range.

This is commonly used in cyclic systems like angles or timers.

Parameters
valueThe value to wrap.
rangeThe range in which to wrap the value.
Returns
A wrapped value within [0, range).