Skip to content

Commit

Permalink
libft: add lerp functions
Browse files Browse the repository at this point in the history
  • Loading branch information
pbondoer committed May 11, 2016
1 parent b85d076 commit 7c56ab0
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 1 deletion.
20 changes: 20 additions & 0 deletions libft/ft_ilerp.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_ilerp.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: pbondoer <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/05/11 01:06:57 by pbondoer #+# #+# */
/* Updated: 2016/05/11 01:42:28 by pbondoer ### ########.fr */
/* */
/* ************************************************************************** */

double ft_ilerp(double val, double first, double second)
{
if (val == first)
return (0.0);
if (val == second)
return (1.0);
return ((val - first) / (second - first));
}
18 changes: 18 additions & 0 deletions libft/ft_lerp.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lerp.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: pbondoer <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/05/11 00:34:09 by pbondoer #+# #+# */
/* Updated: 2016/05/11 01:42:49 by pbondoer ### ########.fr */
/* */
/* ************************************************************************** */

double ft_lerp(double first, double second, double p)
{
if (first == second)
return (first);
return (first + (second - first) * p);
}
18 changes: 18 additions & 0 deletions libft/ft_lerpi.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lerpi.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: pbondoer <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/05/11 00:34:09 by pbondoer #+# #+# */
/* Updated: 2016/05/11 01:43:03 by pbondoer ### ########.fr */
/* */
/* ************************************************************************** */

int ft_lerpi(int first, int second, double p)
{
if (first == second)
return (first);
return ((int)((double)first + (second - first) * p));
}
5 changes: 4 additions & 1 deletion libft/libft.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: pbondoer <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2015/11/24 19:59:25 by pbondoer #+# #+# */
/* Updated: 2016/02/26 04:21:16 by pbondoer ### ########.fr */
/* Updated: 2016/05/11 01:21:15 by pbondoer ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -88,4 +88,7 @@ int ft_max(int a, int b);
int ft_abs(int i);
int ft_iswhitespace(char c);
size_t ft_countwords(char *str, char c);
double ft_lerp(double first, double second, double p);
double ft_ilerp(double val, double first, double second);
int ft_lerpi(int first, int second, double p);
#endif

0 comments on commit 7c56ab0

Please sign in to comment.