|
libft
|
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). | |
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:
| struct s_dpoint |
| #define M_PI 3.14159265358979323846 |
#include <include/ft_math.h>
Mathematical constant π
#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 componenty: the vertical component #include <include/ft_math.h>
A simple struct for storing integer coordinates.
Represents a 2D point with integer precision.
x: the horizontal componenty: the vertical component | 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.
| value | The input value to clamp. |
| min | The minimum allowed value. |
| max | The maximum allowed value. |
| 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.
| degrees | The angle in degrees. |
#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.
| vec | The original vector. |
| scalar | The scalar to divide by. |
#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.
| a | The first point (floating-point). |
| b | The second point (floating-point). |
#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.
| a | The first point. |
| b | The second point. |
#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.
| a | The first point (floating-point). |
| b | The second point (floating-point). |
#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.
| a | The first point. |
| b | The second point. |
#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.
| from | Starting point. |
| to | Target point. |
#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.
| vec1 | First 2D vector. |
| vec2 | Second 2D vector. |
| 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.
| value | The input value. |
#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).
| vector | The input vector to normalize. |
#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.
| from | Starting point. |
| to | Target point. |
| 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.
| vector | The 2D vector. |
#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.
| from | Starting point. |
| to | Target point. |
| 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.
| vec | A 2D vector. |
| 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.
| radians | The angle in radians. |
| 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.
| angle | The angle in radians to normalize. |
| 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.
| value | The value to wrap. |
| range | The range in which to wrap the value. |