vec_math
This commit is contained in:
parent
e866d3f702
commit
2974f7a9b2
@ -6,7 +6,7 @@
|
|||||||
/* By: qmennen <qmennen@student.codam.nl> +#+ */
|
/* By: qmennen <qmennen@student.codam.nl> +#+ */
|
||||||
/* +#+ */
|
/* +#+ */
|
||||||
/* Created: 2025/04/15 15:52:44 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;
|
float y;
|
||||||
} t_vec2;
|
} t_vec2;
|
||||||
|
|
||||||
|
typedef struct s_vec2_line
|
||||||
|
{
|
||||||
|
t_vec2 support;
|
||||||
|
t_vec2 dir;
|
||||||
|
} t_vec2_line;
|
||||||
|
|
||||||
typedef struct s_player
|
typedef struct s_player
|
||||||
{
|
{
|
||||||
t_vec2 pos;
|
t_vec2 pos;
|
||||||
|
|||||||
97
inc/vec_math.h
Normal file
97
inc/vec_math.h
Normal file
@ -0,0 +1,97 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* :::::::: */
|
||||||
|
/* vec_math.h :+: :+: */
|
||||||
|
/* +:+ */
|
||||||
|
/* By: whaffman <whaffman@student.codam.nl> +#+ */
|
||||||
|
/* +#+ */
|
||||||
|
/* 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
|
||||||
24
src/math/add.c
Normal file
24
src/math/add.c
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* :::::::: */
|
||||||
|
/* add.c :+: :+: */
|
||||||
|
/* +:+ */
|
||||||
|
/* By: whaffman <whaffman@student.codam.nl> +#+ */
|
||||||
|
/* +#+ */
|
||||||
|
/* 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);
|
||||||
|
}
|
||||||
20
src/math/dist.c
Normal file
20
src/math/dist.c
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* :::::::: */
|
||||||
|
/* dist.c :+: :+: */
|
||||||
|
/* +:+ */
|
||||||
|
/* By: whaffman <whaffman@student.codam.nl> +#+ */
|
||||||
|
/* +#+ */
|
||||||
|
/* 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)));
|
||||||
|
}
|
||||||
25
src/math/dist_point_line.c
Normal file
25
src/math/dist_point_line.c
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* :::::::: */
|
||||||
|
/* dist_point_line.c :+: :+: */
|
||||||
|
/* +:+ */
|
||||||
|
/* By: whaffman <whaffman@student.codam.nl> +#+ */
|
||||||
|
/* +#+ */
|
||||||
|
/* 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);
|
||||||
|
}
|
||||||
20
src/math/dot.c
Normal file
20
src/math/dot.c
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* :::::::: */
|
||||||
|
/* dot.c :+: :+: */
|
||||||
|
/* +:+ */
|
||||||
|
/* By: whaffman <whaffman@student.codam.nl> +#+ */
|
||||||
|
/* +#+ */
|
||||||
|
/* 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);
|
||||||
|
}
|
||||||
24
src/math/mul.c
Normal file
24
src/math/mul.c
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* :::::::: */
|
||||||
|
/* mul.c :+: :+: */
|
||||||
|
/* +:+ */
|
||||||
|
/* By: whaffman <whaffman@student.codam.nl> +#+ */
|
||||||
|
/* +#+ */
|
||||||
|
/* 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);
|
||||||
|
}
|
||||||
20
src/math/norm.c
Normal file
20
src/math/norm.c
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* :::::::: */
|
||||||
|
/* norm.c :+: :+: */
|
||||||
|
/* +:+ */
|
||||||
|
/* By: whaffman <whaffman@student.codam.nl> +#+ */
|
||||||
|
/* +#+ */
|
||||||
|
/* 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));
|
||||||
|
}
|
||||||
24
src/math/perp.c
Normal file
24
src/math/perp.c
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* :::::::: */
|
||||||
|
/* perp.c :+: :+: */
|
||||||
|
/* +:+ */
|
||||||
|
/* By: whaffman <whaffman@student.codam.nl> +#+ */
|
||||||
|
/* +#+ */
|
||||||
|
/* 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);
|
||||||
|
}
|
||||||
28
src/math/rot.c
Normal file
28
src/math/rot.c
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* :::::::: */
|
||||||
|
/* rot.c :+: :+: */
|
||||||
|
/* +:+ */
|
||||||
|
/* By: whaffman <whaffman@student.codam.nl> +#+ */
|
||||||
|
/* +#+ */
|
||||||
|
/* 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);
|
||||||
|
}
|
||||||
24
src/math/sub.c
Normal file
24
src/math/sub.c
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* :::::::: */
|
||||||
|
/* sub.c :+: :+: */
|
||||||
|
/* +:+ */
|
||||||
|
/* By: whaffman <whaffman@student.codam.nl> +#+ */
|
||||||
|
/* +#+ */
|
||||||
|
/* 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);
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue
Block a user