Compare commits

..

2 commits

Author SHA1 Message Date
24002b2de3 finish 000 2025-05-21 16:47:02 -06:00
c74d35709a add 'ease' procedure 2025-05-21 16:46:49 -06:00
3 changed files with 94 additions and 1 deletions

2
TODO
View file

@ -1,7 +1,6 @@
*** IN PROGRESS ***
[Judah]
000 [math] add easing procedures
[Jesse]
001 [arch] create module to determine simd support
@ -52,3 +51,4 @@
*** DONE ***
010 [x] make base jai files standalone (ie. [array], [memory], etc... big maybe)
042 [x] use absolute imports internally (ie. #import "jc/math"), update symlink/install process in README
000 [math] add easing procedures

92
math/ease.jai Normal file
View file

@ -0,0 +1,92 @@
Ease :: enum {
linear;
quad;
cubic;
quart;
quint;
expo;
sine;
circ;
back;
elastic;
}
Transition :: enum {
in;
out;
in_out;
}
/*
Calculate a tween value for the given process, easing function, and transition.
The value returned can be used like so:
progress := 1.0 / time_in_seconds;
current = original + ease(progress, .cubic, .in_out);
@Note: Progress must be between 0.0 and 1.0
*/
ease :: (progress: $T, $$ease: Ease = .linear, $$transition: Transition = .in) -> T
#modify { return meta.type_is_float(T); }
{
p := progress;
#insert -> string {
expressions :: string.[
"p", // linear
"p * p", // quad
"p * p * p", // cubic
"p * p * p * p", // quart
"p * p * p * p * p", // quint
"math.pow(2, (10 * (p - 1)))", // expo
"-math.cos(p * (math.PI * 0.5)) + 1", // sine
"-(math.sqrt(1 - (p * p)) - 1)", // circ
"p * p * (2.7 * p - 1.7)", // back
"-math.pow(2, 10 * (p - 1)) * math.sin((p - 1.075) * (math.PI * 2) / 0.3)" // elastic
];
b: basic.String_Builder;
if is_constant(ease) basic.append(*b, "#");
basic.append(*b, "if ease == {\n");
info := type_info(Ease);
for info.names {
basic.print_to_builder(*b, #string END
case .%1; %2if transition == {
case .in;
return %3;
case .out;
p = 1 - p;
return p - (%3);
case .in_out;
p *= 2;
if p < 1 {
return 0.5 * (%3);
}
p = 2 - p;
return 0.5 * (1 - (%3)) + 0.5;
}
END, it, ifx is_constant(transition) "#" else "", expressions[it_index]);
}
basic.append(*b, "}\n");
return basic.builder_to_string(*b);
}
return p;
}
#scope_file;
meta :: #import "jc/meta";
math :: #import "Math"; // @future
basic :: #import "Basic"; // @future

View file

@ -5,6 +5,7 @@
#load "vec.jai";
#load "mat.jai";
#load "ease.jai";
#scope_module;