Skip to content

Commit

Permalink
Feature: added 10 basic vector calculus functions
Browse files Browse the repository at this point in the history
  • Loading branch information
Tsunghao-C committed Aug 27, 2024
1 parent 92b782c commit 37da5d9
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 7 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
# By: tsuchen <[email protected]> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2024/08/15 12:02:08 by tsuchen #+# #+# #
# Updated: 2024/08/27 11:36:32 by tsuchen ### ########.fr #
# Updated: 2024/08/27 13:58:10 by tsuchen ### ########.fr #
# #
# **************************************************************************** #

Expand All @@ -22,7 +22,7 @@ SRCS_MAP = map.c

SRCS_RC = raycasting.c

SRCS_VEC = vector.c
SRCS_VEC = vector.c vector_2.c

PATH_M = srcs/
PATH_PS = srcs/parser/
Expand Down
5 changes: 3 additions & 2 deletions includes/parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@
/* ::: :::::::: */
/* parser.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jteissie <jteissie@student.42.fr> +#+ +:+ +#+ */
/* By: tsuchen <tsuchen@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/08/15 15:00:57 by jteissie #+# #+# */
/* Updated: 2024/08/16 18:58:41 by jteissie ### ########.fr */
/* Updated: 2024/08/27 14:00:34 by tsuchen ### ########.fr */
/* */
/* ************************************************************************** */

#ifndef PARSER_H
# define PARSER_H

# include "cub3d.h"

typedef enum e_bool t_bool;
Expand Down
15 changes: 13 additions & 2 deletions includes/vector.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,32 @@
/* By: tsuchen <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/08/27 10:56:32 by tsuchen #+# #+# */
/* Updated: 2024/08/27 11:41:19 by tsuchen ### ########.fr */
/* Updated: 2024/08/27 13:55:58 by tsuchen ### ########.fr */
/* */
/* ************************************************************************** */

#ifndef VECTOR_H
# define VECTOR_H

# include <math.h>
# include <limits.h>
# include <stdint.h>

typedef struct s_vec
{
double x;
double y;
} t_vec;

void vec_init(t_vec *vec, double x, double y);
void vec_add(t_vec *vec_a, const t_vec *vec_b);
void vec_sub(t_vec *vec_a, const t_vec *vec_b);
void vec_muls(t_vec *vec, double scalar);
double vec_dot(const t_vec *vec_a, const t_vec *vec_b);
double vec_absv(const t_vec *vec);
double vec_dist(const t_vec *vec_a, const t_vec *vec_b);
void vec_mirror(t_vec *vec);
void vec_transp(t_vec *vec);
void vec_rotate(t_vec *vec, double angle);
t_vec vec_dot(t_vec *vec_a, t_vec *vec_b);

#endif
28 changes: 27 additions & 1 deletion srcs/vector/vector.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: tsuchen <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/08/27 11:11:34 by tsuchen #+# #+# */
/* Updated: 2024/08/27 11:14:06 by tsuchen ### ########.fr */
/* Updated: 2024/08/27 13:32:54 by tsuchen ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -17,3 +17,29 @@ void vec_init(t_vec *vec, double x, double y)
vec->x = x;
vec->y = y;
}

void vec_add(t_vec *vec_a, const t_vec *vec_b)
{
vec_a->x = vec_a->x + vec_b->x;
vec_a->y = vec_a->y + vec_b->y;
}

void vec_sub(t_vec *vec_a, const t_vec *vec_b)
{
vec_a->x = vec_a->x - vec_b->x;
vec_a->y = vec_b->y - vec_b->y;
}

void vec_muls(t_vec *vec, double scalar)
{
vec->x = vec->x * scalar;
vec->y = vec->y * scalar;
}

double vec_dot(const t_vec *vec_a, const t_vec *vec_b)
{
double dot;

dot = (vec_a->x * vec_b->x) + (vec_a->y * vec_b->y);
return (dot);
}
59 changes: 59 additions & 0 deletions srcs/vector/vector_2.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* vector_2.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tsuchen <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/08/27 13:33:30 by tsuchen #+# #+# */
/* Updated: 2024/08/27 13:55:09 by tsuchen ### ########.fr */
/* */
/* ************************************************************************** */

#include "vector.h"

double vec_absv(const t_vec *vec)
{
double absv;

absv = sqrt((vec->x * vec->x) + (vec->y * vec->y));
return (absv);
}

double vec_dist(const t_vec *vec_a, const t_vec *vec_b)
{
double dist;
double del_x;
double del_y;

del_x = vec_b->x - vec_a->x;
del_y = vec_b->y - vec_a->y;
dist = sqrt((del_x * del_x) + (del_y * del_y));
return (dist);
}

void vec_mirror(t_vec *vec)
{
vec->x = vec->x * -1;
vec->y = vec->y * -1;
}

void vec_transp(t_vec *vec)
{
double tmp;

tmp = vec->x;
vec->x = vec->y;
vec->y = tmp;
}

void vec_rotate(t_vec *vec, double angle)
{
double new_x;
double new_y;

new_x = vec->x * cos(angle) - vec->y * sin(angle);
new_y = vec->x * sin(angle) + vec->y * cos(angle);
vec->x = new_x;
vec->y = new_y;
}

0 comments on commit 37da5d9

Please sign in to comment.