Skip to content

Commit

Permalink
Updated with FDF used functions, particularly ft_math_utils.
Browse files Browse the repository at this point in the history
  • Loading branch information
Matthew Yeow committed Aug 23, 2024
1 parent cb8b85d commit d016d3c
Show file tree
Hide file tree
Showing 56 changed files with 1,602 additions and 279 deletions.
47 changes: 47 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ CHARSRC = \
isdigit \
isprint \
isspace \
ishex \

CHARSRCS = $(addprefix $(CHARDIR)$(FT_PRE), $(CHARSRC))

Expand All @@ -34,6 +35,7 @@ MEMSRCS = $(addprefix $(MEMDIR)$(FT_PRE), $(MEMSRC))
STRDIR = $(SRCDIR)$(FT_PRE)string_utils/
STRSRC = \
atoi \
atoi_base \
itoa \
free_ft_split \
split \
Expand Down Expand Up @@ -70,6 +72,8 @@ PRNTSRC = \
putstr_fd \
putendl_fd \
putnbr_fd \
putnbr_base_fd \
putdbl_fd \

PRNTSRCS = $(addprefix $(PRNTDIR)$(FT_PRE), $(PRNTSRC))

Expand Down Expand Up @@ -128,6 +132,48 @@ PRNTFSRC = \

PRNTFSRCS = $(addprefix $(PRNTFDIR)$(FT_PRE), $(PRNTFSRC))

MATHDIR = $(SRCDIR)$(FT_PRE)math_utils/
MATHSRC = \
abs \
copysign \
swap \
modf \
floor \
ceil \
round \
fractional \
rev_fractional \
fmod \
power \
sqrt \
sin \
cos \
tan \
asin \
acos \
atan \
atan2 \
quatset \
quatset_id \
quatcpy \
quat_isequal \
quat_print \
quat_from_axis_angle \
quat_from_xrotation \
quat_from_yrotation \
quat_from_zrotation \
quatget_magnitude \
quat_normalise \
quat_to_axis_angle \
quat_from_euler_zyx \
quat_to_euler_zyx \
quat_conjugate \
quat_mult \
quat_rotate \
quat_slerp \

MATHSRCS = $(addprefix $(MATHDIR)$(FT_PRE), $(MATHSRC))


SRC_AGG = \
$(CHARSRCS) \
Expand All @@ -138,6 +184,7 @@ SRC_AGG = \
$(ERRSRCS) \
$(GNLSRCS) \
$(PRNTFSRCS) \
$(MATHSRCS) \

SRCS = $(addsuffix .c, $(SRC_AGG))

Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ In 42, it is forbidden to use any pre-defined funtions unless stated in the requ
TODOs:
1. Error management for the functions.
2. Makefile for each subfolder.
3. Consider separating quaternion functions from math lib.
5 changes: 3 additions & 2 deletions includes/ft_char_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: myeow <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/12 16:48:49 by myeow #+# #+# */
/* Updated: 2024/05/12 16:50:38 by myeow ### ########.fr */
/* Updated: 2024/08/05 22:04:41 by myeow ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -19,5 +19,6 @@ int ft_isascii(int c);
int ft_isdigit(int c);
int ft_isprint(int c);
int ft_isspace(int c);
int ft_ishex(int c);

#endif
#endif
113 changes: 113 additions & 0 deletions includes/ft_math_utils.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_math_utils.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: myeow <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/30 20:02:38 by myeow #+# #+# */
/* Updated: 2024/08/12 19:42:05 by myeow ### ########.fr */
/* */
/* ************************************************************************** */

#ifndef FT_MATH_UTILS_H
# define FT_MATH_UTILS_H

# include <stdint.h>

# ifndef PI
# define PI 3.14159265358979323846
# endif

# ifndef PI_2
# define PI_2 1.57079632679489661923
# endif

# ifndef PI_4
# define PI_4 0.78539816339744830962
# endif

# ifndef ATAN_TABLE_ITERS
# define ATAN_TABLE_ITERS 16
# endif

# ifndef K_FACTOR
# define K_FACTOR 0.6072529350088812561694
# endif

# ifndef QUAT_EPS
# define QUAT_EPS 1e-4
# endif

typedef struct s_2d_vector
{
double x;
double y;
} t_vec2;

typedef struct s_3d_vector
{
double x;
double y;
double z;
} t_vec3;

typedef struct s_quaternion
{
double w;
t_vec3 v;
} t_quat;

typedef union s_conv
{
double d;
uint64_t i;
} t_conv;

double ft_abs(double d);
double ft_copysign(double x, double y);
void ft_swap(double *a, double *b);
double ft_modf(double d, double *i);
double ft_floor(double d);
double ft_ceil(double d);
double ft_round(double d);
double ft_fractional(double d);
double ft_rev_fractional(double d);
double ft_fmod(double x, double y);
double ft_sqrt_inverse_fast(double x);
double ft_sqrt_fast(double x);
double ft_sqrt(double x);
unsigned long long ft_factorial(int x);
double ft_power(double x, int p);
double ft_atan_table(int i);
double ft_sin(double r);
double ft_cos(double r);
double ft_tan(double r);
double ft_asin(double x);
double ft_acos(double x);
double ft_atan(double x);
double ft_atan2(double y, double x);

//QUATERNION_UTILS
void ft_quatset(double w, t_vec3 v, t_quat *out);
void ft_quatset_id(t_quat *out);
void ft_quatcpy(t_quat *q, t_quat *out);
int ft_quat_isequal(t_quat *q1, t_quat *q2);
void ft_quat_print(t_quat *q);
void ft_quat_from_axis_angle(t_vec3 *axis, double r, \
t_quat *out);
void ft_quat_from_xrotation(double r, t_quat *out);
void ft_quat_from_yrotation(double r, t_quat *out);
void ft_quat_from_zrotation(double r, t_quat *out);
double ft_quatget_magnitude(t_quat *q);
void ft_quat_normalise(t_quat *out);
double ft_quat_to_axis_angle(t_quat *q, t_vec3 *out);
void ft_quat_from_euler_zyx(t_vec3 *v, t_quat *out);
void ft_quat_to_euler_zyx(t_quat *q, t_vec3 *out);
void ft_quat_conjugate(t_quat *q, t_quat *out);
void ft_quat_mult(t_quat *q1, t_quat *q2, t_quat *out);
void ft_quat_rotate(t_vec3 *v, t_quat *q, t_vec3 *out);
void ft_quat_slerp(t_quat *q1, t_quat *q2, double t, \
t_quat *out);

#endif
6 changes: 4 additions & 2 deletions includes/ft_print_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: myeow <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/12 16:50:08 by myeow #+# #+# */
/* Updated: 2024/05/12 16:50:34 by myeow ### ########.fr */
/* Updated: 2024/08/11 18:06:22 by myeow ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -17,5 +17,7 @@ void ft_putchar_fd(char c, int fd);
void ft_putstr_fd(char *s, int fd);
void ft_putendl_fd(char *s, int fd);
void ft_putnbr_fd(int n, int fd);
void ft_putnbr_base_fd(int nbr, char *base, int fd);
void ft_putdbl_fd(double d, int fd);

#endif
#endif
1 change: 1 addition & 0 deletions includes/ft_printf.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
# include <unistd.h>
# include <stdarg.h>
# include <stdlib.h>
# include <stdint.h>

# define FLAGS "-0# +"
# define SPECIFIERS "cspdiuxX"
Expand Down
3 changes: 2 additions & 1 deletion includes/ft_string_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: myeow <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/12 16:50:19 by myeow #+# #+# */
/* Updated: 2024/05/17 17:50:15 by myeow ### ########.fr */
/* Updated: 2024/08/07 20:05:29 by myeow ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -16,6 +16,7 @@
# include <stddef.h>

int ft_atoi(const char *str);
int ft_atoi_base(char *str, char *base);
void ft_free_ft_split(char **str_array);
char *ft_itoa(int n);
char **ft_split(char const *s, char c);
Expand Down
4 changes: 4 additions & 0 deletions includes/get_next_line.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@
# define BUFFER_SIZE 42
# endif

# ifndef OPEN_MAX
# define OPEN_MAX 256
# endif

# include <unistd.h>
# include <fcntl.h>
# include <stdlib.h>
Expand Down
2 changes: 1 addition & 1 deletion includes/libft.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: myeow <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/15 16:11:56 by myeow #+# #+# */
/* Updated: 2024/05/12 16:50:31 by myeow ### ########.fr */
/* Updated: 2024/07/30 20:05:47 by myeow ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down
24 changes: 24 additions & 0 deletions srcs/ft_char_utils/ft_ishex.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_ishex.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: myeow <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/08/05 22:01:09 by myeow #+# #+# */
/* Updated: 2024/08/05 22:05:32 by myeow ### ########.fr */
/* */
/* ************************************************************************** */

int ft_isdigit(int c);

int ft_ishex(int c)
{
if (ft_isdigit(c))
return (1);
if (c >= 'A' && c <= 'F')
return (1);
if (c >= 'a' && c <= 'f')
return (1);
return (0);
}
18 changes: 18 additions & 0 deletions srcs/ft_math_utils/ft_abs.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_abs.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: myeow <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/30 19:56:45 by myeow #+# #+# */
/* Updated: 2024/07/30 19:58:38 by myeow ### ########.fr */
/* */
/* ************************************************************************** */

double ft_abs(double d)
{
if (d < 0)
return (-d);
return (d);
}
27 changes: 27 additions & 0 deletions srcs/ft_math_utils/ft_acos.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_acos.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: myeow <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/08/10 00:47:32 by myeow #+# #+# */
/* Updated: 2024/08/11 01:50:32 by myeow ### ########.fr */
/* */
/* ************************************************************************** */

#include "ft_math_utils.h"

void ft_putendl_fd(char *s, int fd);

double ft_asin(double x);

double ft_acos(double x)
{
if (x < -1.0 || x > 1.0)
{
ft_putendl_fd("Undefined.", 2);
return (0);
}
return (PI / 2 - ft_asin(x));
}
43 changes: 43 additions & 0 deletions srcs/ft_math_utils/ft_asin.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_asin.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: myeow <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/08/10 00:48:44 by myeow #+# #+# */
/* Updated: 2024/08/11 01:50:07 by myeow ### ########.fr */
/* */
/* ************************************************************************** */

void ft_putendl_fd(char *s, int fd);

double ft_abs(double d);

double ft_sin(double r);

double ft_cos(double r);

double ft_asin(double x)
{
double y;
int i;
double y_prime;
const double tolerance = 1e-10;

if (x < -1.0 || x > 1.0)
{
ft_putendl_fd("Undefined.", 2);
return (0);
}
y = x;
i = -1;
while (++i < 100)
{
y_prime = y - ((ft_sin(y) - x) / ft_cos(y));
if (ft_abs(y_prime - y) < tolerance)
return (y_prime);
y = y_prime;
}
return (y);
}
Loading

0 comments on commit d016d3c

Please sign in to comment.