From 2974f7a9b2731e96fd9988cccc994a06fdc5abd8 Mon Sep 17 00:00:00 2001 From: whaffman Date: Fri, 25 Apr 2025 10:46:34 +0200 Subject: [PATCH] vec_math --- inc/types.h | 8 +++- inc/vec_math.h | 97 ++++++++++++++++++++++++++++++++++++++ src/math/add.c | 24 ++++++++++ src/math/dist.c | 20 ++++++++ src/math/dist_point_line.c | 25 ++++++++++ src/math/dot.c | 20 ++++++++ src/math/mul.c | 24 ++++++++++ src/math/norm.c | 20 ++++++++ src/math/perp.c | 24 ++++++++++ src/math/rot.c | 28 +++++++++++ src/math/sub.c | 24 ++++++++++ 11 files changed, 313 insertions(+), 1 deletion(-) create mode 100644 inc/vec_math.h create mode 100644 src/math/add.c create mode 100644 src/math/dist.c create mode 100644 src/math/dist_point_line.c create mode 100644 src/math/dot.c create mode 100644 src/math/mul.c create mode 100644 src/math/norm.c create mode 100644 src/math/perp.c create mode 100644 src/math/rot.c create mode 100644 src/math/sub.c diff --git a/inc/types.h b/inc/types.h index 44829d0..049ac52 100644 --- a/inc/types.h +++ b/inc/types.h @@ -6,7 +6,7 @@ /* By: qmennen +#+ */ /* +#+ */ /* Created: 2025/04/15 15:52:44 by qmennen #+# #+# */ -/* Updated: 2025/04/22 11:28:34 by whaffman ######## odam.nl */ +/* Updated: 2025/04/25 10:01:28 by whaffman ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -30,6 +30,12 @@ typedef struct s_vec2 float y; } t_vec2; +typedef struct s_vec2_line +{ + t_vec2 support; + t_vec2 dir; +} t_vec2_line; + typedef struct s_player { t_vec2 pos; diff --git a/inc/vec_math.h b/inc/vec_math.h new file mode 100644 index 0000000..4452594 --- /dev/null +++ b/inc/vec_math.h @@ -0,0 +1,97 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* vec_math.h :+: :+: */ +/* +:+ */ +/* By: whaffman +#+ */ +/* +#+ */ +/* Created: 2025/04/25 10:11:44 by whaffman #+# #+# */ +/* Updated: 2025/04/25 10:44:33 by whaffman ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#ifndef VEC_MATH_H +# define VEC_MATH_H +# include "types.h" + + +/** + * @brief Calculates the distance between two 2D vectors. + * + * @param a The first 2D vector. + * @param b The second 2D vector. + * @return The distance between vector a and vector b as a float. + */ +float dist(t_vec2 a, t_vec2 b); + +/** + * @brief Calculates the norm (length) of a 2D vector. + * + * @param a The 2D vector. + * @return The norm of vector a as a float. + */ +float norm(t_vec2 a); + +/** + * @brief Calculates the dot product of two 2D vectors. + * + * @param a The first 2D vector. + * @param b The second 2D vector. + * @return The dot product of vector a and vector b as a float. + */ +float dot(t_vec2 a, t_vec2 b); + +/** + * @brief Calculates the distance from a point to a line. + * + * @param point The point to calculate the distance from. + * @param line The line defined by a (support)point and a direction. + * @return The distance from the point to the line as a float. + */ +float dist_point_line(t_vec2 point, t_vec2_line line); + +/** + * @brief Adds two 2D vectors. + * + * @param a The first 2D vector. + * @param b The second 2D vector. + * @return The sum of vector a and vector b as a new 2D vector. + */ +t_vec2 add(t_vec2 a, t_vec2 b); + +/** + * @brief Subtracts one 2D vector from another. + * + * @param a The first 2D vector. + * @param b The second 2D vector to subtract from the first. + * @return The result of subtracting vector b from vector a as a new 2D vector. + */ +t_vec2 sub(t_vec2 a, t_vec2 b); + +/** + * @brief Multiplies a 2D vector by a scalar. + * + * @param a The 2D vector to multiply. + * @param b The scalar to multiply the vector by. + * @return The result of multiplying vector a by scalar b as a new 2D vector. + */ +t_vec2 mul(t_vec2 a, float b); + +/** + * @brief Rotates a 2D vector by a given angle. + * + * @param a The 2D vector to rotate. + * @param angle The angle in radians to rotate the vector by. + * @return The rotated vector as a new 2D vector. + */ +t_vec2 rot(t_vec2 a, float angle); + +/** + * @brief Calculates the perpendicular vector of a 2D vector. + * + * @param a The 2D vector to calculate the perpendicular of. + * @return The perpendicular vector as a new 2D vector. + */ +t_vec2 perp(t_vec2 a); + +#endif // VEC_MATH_H \ No newline at end of file diff --git a/src/math/add.c b/src/math/add.c new file mode 100644 index 0000000..79b07d0 --- /dev/null +++ b/src/math/add.c @@ -0,0 +1,24 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* add.c :+: :+: */ +/* +:+ */ +/* By: whaffman +#+ */ +/* +#+ */ +/* Created: 2025/04/25 09:54:24 by whaffman #+# #+# */ +/* Updated: 2025/04/25 10:39:42 by whaffman ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#include "cub3d.h" +#include "math.h" + + +t_vec2 add(t_vec2 a, t_vec2 b) +{ + t_vec2 result; + + result.x = a.x + b.x; + result.y = a.y + b.y; + return (result); +} \ No newline at end of file diff --git a/src/math/dist.c b/src/math/dist.c new file mode 100644 index 0000000..34bab2b --- /dev/null +++ b/src/math/dist.c @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* dist.c :+: :+: */ +/* +:+ */ +/* By: whaffman +#+ */ +/* +#+ */ +/* Created: 2025/04/25 10:07:59 by whaffman #+# #+# */ +/* Updated: 2025/04/25 10:39:59 by whaffman ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#include "cub3d.h" +#include "math.h" +#include "vec_math.h" + +float dist(t_vec2 a, t_vec2 b) +{ + return (sqrtf((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y))); +} \ No newline at end of file diff --git a/src/math/dist_point_line.c b/src/math/dist_point_line.c new file mode 100644 index 0000000..9e44cdb --- /dev/null +++ b/src/math/dist_point_line.c @@ -0,0 +1,25 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* dist_point_line.c :+: :+: */ +/* +:+ */ +/* By: whaffman +#+ */ +/* +#+ */ +/* Created: 2025/04/25 10:09:45 by whaffman #+# #+# */ +/* Updated: 2025/04/25 10:46:06 by whaffman ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#include "cub3d.h" +#include "math.h" +#include "vec_math.h" + +float dist_point_line(t_vec2 point, t_vec2_line line) +{ + float d; + t_vec2 ps; + + ps = sub(line.support, point); + d = norm(sub(ps, mul(line.dir, dot(ps, line.dir)))); + return (d); +} \ No newline at end of file diff --git a/src/math/dot.c b/src/math/dot.c new file mode 100644 index 0000000..7beb83e --- /dev/null +++ b/src/math/dot.c @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* dot.c :+: :+: */ +/* +:+ */ +/* By: whaffman +#+ */ +/* +#+ */ +/* Created: 2025/04/25 09:58:29 by whaffman #+# #+# */ +/* Updated: 2025/04/25 10:40:02 by whaffman ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#include "cub3d.h" +#include "math.h" +#include "vec_math.h" + +float dot(t_vec2 a, t_vec2 b) +{ + return (a.x * b.x + a.y * b.y); +} \ No newline at end of file diff --git a/src/math/mul.c b/src/math/mul.c new file mode 100644 index 0000000..93257e9 --- /dev/null +++ b/src/math/mul.c @@ -0,0 +1,24 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* mul.c :+: :+: */ +/* +:+ */ +/* By: whaffman +#+ */ +/* +#+ */ +/* Created: 2025/04/25 09:56:39 by whaffman #+# #+# */ +/* Updated: 2025/04/25 10:40:05 by whaffman ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#include "cub3d.h" +#include "math.h" +#include "vec_math.h" + +t_vec2 mul(t_vec2 a, float b) +{ + t_vec2 result; + + result.x = a.x * b; + result.y = a.y * b; + return (result); +} \ No newline at end of file diff --git a/src/math/norm.c b/src/math/norm.c new file mode 100644 index 0000000..326b353 --- /dev/null +++ b/src/math/norm.c @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* norm.c :+: :+: */ +/* +:+ */ +/* By: whaffman +#+ */ +/* +#+ */ +/* Created: 2025/04/25 09:59:13 by whaffman #+# #+# */ +/* Updated: 2025/04/25 10:40:13 by whaffman ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#include "cub3d.h" +#include "math.h" +#include "vec_math.h" + +float norm(t_vec2 a) +{ + return (sqrtf(a.x * a.x + a.y * a.y)); +} \ No newline at end of file diff --git a/src/math/perp.c b/src/math/perp.c new file mode 100644 index 0000000..844634b --- /dev/null +++ b/src/math/perp.c @@ -0,0 +1,24 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* perp.c :+: :+: */ +/* +:+ */ +/* By: whaffman +#+ */ +/* +#+ */ +/* Created: 2025/04/25 10:34:39 by whaffman #+# #+# */ +/* Updated: 2025/04/25 10:40:17 by whaffman ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#include "cub3d.h" +#include "math.h" +#include "vec_math.h" + +t_vec2 perp(t_vec2 a) +{ + t_vec2 result; + + result.x = -a.y; + result.y = a.x; + return (result); +} \ No newline at end of file diff --git a/src/math/rot.c b/src/math/rot.c new file mode 100644 index 0000000..1608710 --- /dev/null +++ b/src/math/rot.c @@ -0,0 +1,28 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* rot.c :+: :+: */ +/* +:+ */ +/* By: whaffman +#+ */ +/* +#+ */ +/* Created: 2025/04/25 10:35:58 by whaffman #+# #+# */ +/* Updated: 2025/04/25 10:40:23 by whaffman ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#include "cub3d.h" +#include "math.h" +#include "vec_math.h" + +t_vec2 rot(t_vec2 a, float angle) +{ + t_vec2 result; + float cos_angle; + float sin_angle; + + cos_angle = cosf(angle); + sin_angle = sinf(angle); + result.x = a.x * cos_angle - a.y * sin_angle; + result.y = a.x * sin_angle + a.y * cos_angle; + return (result); +} \ No newline at end of file diff --git a/src/math/sub.c b/src/math/sub.c new file mode 100644 index 0000000..b136daa --- /dev/null +++ b/src/math/sub.c @@ -0,0 +1,24 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* sub.c :+: :+: */ +/* +:+ */ +/* By: whaffman +#+ */ +/* +#+ */ +/* Created: 2025/04/25 09:57:55 by whaffman #+# #+# */ +/* Updated: 2025/04/25 10:40:27 by whaffman ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#include "cub3d.h" +#include "math.h" +#include "vec_math.h" + +t_vec2 sub(t_vec2 a, t_vec2 b) +{ + t_vec2 result; + + result.x = a.x - b.x; + result.y = a.y - b.y; + return (result); +} \ No newline at end of file