Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Vector cal library #10

Merged
merged 11 commits into from
Aug 27, 2024
12 changes: 8 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
# ::: :::::::: #
# Makefile :+: :+: :+: #
# +:+ +:+ +:+ #
# By: jteissie <jteissie@student.42.fr> +#+ +:+ +#+ #
# By: tsuchen <tsuchen@student.42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2024/08/15 12:02:08 by tsuchen #+# #+# #
# Updated: 2024/08/16 19:28:50 by jteissie ### ########.fr #
# Updated: 2024/08/27 13:58:10 by tsuchen ### ########.fr #
# #
# **************************************************************************** #

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

SRCS_RC = raycasting.c

SRCS_VEC = vector.c vector_2.c

PATH_M = srcs/
PATH_PS = srcs/parser/
PATH_MAP = srcs/map/
PATH_RC = srcs/raycasting/
PATH_VEC = srcs/vector/

SRCS = $(addprefix $(PATH_M), $(SRCS_M)) \
$(addprefix $(PATH_PS), $(SRCS_PS)) \
$(addprefix $(PATH_MAP), $(SRCS_MAP)) \
$(addprefix $(PATH_RC), $(SRCS_RC))
$(addprefix $(PATH_RC), $(SRCS_RC)) \
$(addprefix $(PATH_VEC), $(SRCS_VEC))

HEADERS = cub3d.h
HEADERS = cub3d.h parser.h vector.h

OBJS = $(SRCS:.c=.o)

Expand Down
11 changes: 8 additions & 3 deletions includes/cub3d.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
/* ::: :::::::: */
/* cub3d.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jteissie <jteissie@student.42.fr> +#+ +:+ +#+ */
/* By: tsuchen <tsuchen@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/08/15 00:19:44 by tsuchen #+# #+# */
/* Updated: 2024/08/16 19:18:23 by jteissie ### ########.fr */
/* Updated: 2024/08/27 11:37:10 by tsuchen ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -32,6 +32,7 @@
# include "get_next_line_bonus.h"

# include "parser.h"
# include "vector.h"

# define SUCCESS 0
# define PANIC 1
Expand Down Expand Up @@ -60,13 +61,17 @@ typedef enum e_p_dir
WEST = 'W',
EAST = 'E',
} t_p_dir;

typedef struct s_data
{
char *map_path;
int map_fd;
size_t map_bound;
char **map;
t_p_dir p_dir;
t_p_dir p_dir_default;
t_vec p_pos;
t_vec p_dir;
t_vec p_cam;
} t_data;

#endif
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
41 changes: 41 additions & 0 deletions includes/vector.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* vector.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tsuchen <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/08/27 10:56:32 by tsuchen #+# #+# */
/* Updated: 2024/08/27 14:42:47 by tsuchen ### ########.fr */
/* */
/* ************************************************************************** */

#ifndef VECTOR_H
# define VECTOR_H

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

# define PI 3.14159265359f

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);

#endif
23 changes: 21 additions & 2 deletions srcs/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,33 @@
/* ::: :::::::: */
/* main.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jteissie <jteissie@student.42.fr> +#+ +:+ +#+ */
/* By: tsuchen <tsuchen@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/08/15 12:03:33 by tsuchen #+# #+# */
/* Updated: 2024/08/16 19:40:02 by jteissie ### ########.fr */
/* Updated: 2024/08/27 11:51:43 by tsuchen ### ########.fr */
/* */
/* ************************************************************************** */

#include "cub3d.h"

void print_data(t_data *data)
{
int i;

i = 0;
printf("Player position x: %.2f, y: %.2f\n", data->p_pos.x, data->p_pos.y);
printf("Player direction x: %.2f, y: %.2f\n", data->p_dir.x, data->p_dir.y);
printf("Player default dir: %d\n", data->p_dir_default);
printf("Map path is: %s\n", data->map_path);
printf("Map output:\n");
while (data->map[i])
{
printf("%s", data->map[i]);
i++;
}
printf("Map bound: %zu\n", data->map_bound);
}

static t_bool is_cub_file(char *file)
{
uint32_t index;
Expand Down Expand Up @@ -42,6 +60,7 @@ int main(int ac, char *av[])
return (EXIT_FAILURE);
if (parse_map(&data) == PANIC)
return (EXIT_FAILURE);
print_data(&data);
ft_free_all(data.map);
close(data.map_fd);
return (0);
Expand Down
23 changes: 18 additions & 5 deletions srcs/parser/parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/* ::: :::::::: */
/* parser.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jteissie <jteissie@student.42.fr> +#+ +:+ +#+ */
/* By: tsuchen <tsuchen@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/08/15 12:12:00 by tsuchen #+# #+# */
/* Updated: 2024/08/27 13:17:50 by jteissie ### ########.fr */
Expand Down Expand Up @@ -39,13 +39,26 @@ t_parse_status check_walls(char **map, uint32_t x, uint32_t y, size_t bound)
static void get_player_dir(t_data *data, u_int32_t x, u_int32_t y)
{
if (data->map[x][y] == 'N')
data->p_dir = NORTH;
{
data->p_dir_default = NORTH;
vec_init(&data->p_dir, -1.0f, 0.0f);
}
else if (data->map[x][y] == 'S')
data->p_dir = SOUTH;
{
data->p_dir_default = SOUTH;
vec_init(&data->p_dir, 1.0f, 0.0f);
}
else if (data->map[x][y] == 'W')
data->p_dir = WEST;
{
data->p_dir_default = WEST;
vec_init(&data->p_dir, 0.0f, -1.0f);
}
else if (data->map[x][y] == 'E')
data->p_dir = EAST;
{
data->p_dir_default = EAST;
vec_init(&data->p_dir, 0.0f, 1.0f);
}
vec_init(&data->p_pos, (double)x, (double)y);
}

t_parse_status verify_map(char **map, t_data *data)
Expand Down
2 changes: 1 addition & 1 deletion srcs/parser/parser_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/* ::: :::::::: */
/* parser_utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jteissie <jteissie@student.42.fr> +#+ +:+ +#+ */
/* By: tsuchen <tsuchen@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/08/16 18:12:37 by jteissie #+# #+# */
/* Updated: 2024/08/27 13:23:59 by jteissie ### ########.fr */
Expand Down
72 changes: 72 additions & 0 deletions srcs/vector/vector.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* vector.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tsuchen <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/08/27 11:11:34 by tsuchen #+# #+# */
/* Updated: 2024/08/27 15:46:44 by tsuchen ### ########.fr */
/* */
/* ************************************************************************** */

#include "vector.h"

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_a->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);
}

// int main(void)
// {
// t_vec a;
// t_vec b;

// vec_init(&a, 3, 4);
// vec_init(&b, 1, 2);
// printf("vec_a init ax: %.2f, ay: %.2f\n", a.x, a.y);
// printf("vec_b init bx: %.2f, by: %.2f\n", b.x, b.y);
// vec_add(&a, &b);
// printf("vec_a after adding b ax: %.2f, ay: %.2f\n", a.x, a.y);
// vec_sub(&a, &b);
// printf("vec_a after minus b ax: %.2f, ay: %.2f\n", a.x, a.y);
// vec_muls(&a, 5);
// printf("vec_a after multiply by 5 ax: %.2f, ay: %.2f\n", a.x, a.y);
// printf("vec_a dot vec_b is %.2f\n", vec_dot(&a, &b));
// printf("abs of a is %.2f\n", vec_absv(&a));
// printf("dist between a and b: %.2f\n", vec_dist(&a, &b));
// vec_mirror(&a);
// printf("vec_a after mirror ax: %.2f, ay: %.2f\n", a.x, a.y);
// vec_transp(&a);
// printf("vec_a after transp ax: %.2f, ay: %.2f\n", a.x, a.y);
// vec_rotate(&b, 30);
// printf("vec_b after rotate 30 degree bx: %.2f, by: %.2f\n", b.x, b.y);
// return (0);
// }
63 changes: 63 additions & 0 deletions srcs/vector/vector_2.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* vector_2.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tsuchen <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/08/27 13:33:30 by tsuchen #+# #+# */
/* Updated: 2024/08/27 15:42:12 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;
double pi;
double angle_r;

pi = acos(-1);
angle_r = angle * pi / 180;
new_x = vec->x * cos(angle_r) - vec->y * sin(angle_r);
new_y = vec->x * sin(angle_r) + vec->y * cos(angle_r);
vec->x = new_x;
vec->y = new_y;
}
Loading