| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429 |
- // EASINGS :: https://github.com/prideout/par
- // Robert Penner's easing functions.
- //
- // Distributed under the MIT License, see bottom of file.
- // -----------------------------------------------------------------------------
- // BEGIN PUBLIC API
- // -----------------------------------------------------------------------------
- #ifndef PAR_EASINGS_H
- #define PAR_EASINGS_H
- #ifdef __cplusplus
- extern "C" {
- #endif
- #ifndef PAR_EASINGS_FLOAT
- #define PAR_EASINGS_FLOAT float
- #endif
- PAR_EASINGS_FLOAT par_easings_linear(PAR_EASINGS_FLOAT t);
- PAR_EASINGS_FLOAT par_easings_in_cubic(PAR_EASINGS_FLOAT t);
- PAR_EASINGS_FLOAT par_easings_out_cubic(PAR_EASINGS_FLOAT t);
- PAR_EASINGS_FLOAT par_easings_in_out_cubic(PAR_EASINGS_FLOAT t);
- PAR_EASINGS_FLOAT par_easings_in_quad(PAR_EASINGS_FLOAT t);
- PAR_EASINGS_FLOAT par_easings_out_quad(PAR_EASINGS_FLOAT t);
- PAR_EASINGS_FLOAT par_easings_in_out_quad(PAR_EASINGS_FLOAT t);
- PAR_EASINGS_FLOAT par_easings_in_elastic(PAR_EASINGS_FLOAT t);
- PAR_EASINGS_FLOAT par_easings_out_elastic(PAR_EASINGS_FLOAT t);
- PAR_EASINGS_FLOAT par_easings_in_out_elastic(PAR_EASINGS_FLOAT t);
- PAR_EASINGS_FLOAT par_easings_in_bounce(PAR_EASINGS_FLOAT t);
- PAR_EASINGS_FLOAT par_easings_out_bounce(PAR_EASINGS_FLOAT t);
- PAR_EASINGS_FLOAT par_easings_in_out_bounce(PAR_EASINGS_FLOAT t);
- PAR_EASINGS_FLOAT par_easings_in_back(PAR_EASINGS_FLOAT t);
- PAR_EASINGS_FLOAT par_easings_out_back(PAR_EASINGS_FLOAT t);
- PAR_EASINGS_FLOAT par_easings_in_out_back(PAR_EASINGS_FLOAT t);
- #ifndef PAR_PI
- #define PAR_PI (3.14159265359)
- #define PAR_MIN(a, b) (a > b ? b : a)
- #define PAR_MAX(a, b) (a > b ? a : b)
- #define PAR_CLAMP(v, lo, hi) PAR_MAX(lo, PAR_MIN(hi, v))
- #define PAR_SWAP(T, A, B) { T tmp = B; B = A; A = tmp; }
- #define PAR_SQR(a) ((a) * (a))
- #endif
- #ifdef __cplusplus
- }
- #endif
- // -----------------------------------------------------------------------------
- // END PUBLIC API
- // -----------------------------------------------------------------------------
- #ifdef PAR_EASINGS_IMPLEMENTATION
- #define PARFLT PAR_EASINGS_FLOAT
- PARFLT par_easings__linear(PARFLT t, PARFLT b, PARFLT c, PARFLT d)
- {
- return c * t / d + b;
- }
- PARFLT par_easings__in_cubic(PARFLT t, PARFLT b, PARFLT c, PARFLT d)
- {
- t /= d;
- return c * t * t * t + b;
- }
- PARFLT par_easings__out_cubic(PARFLT t, PARFLT b, PARFLT c, PARFLT d)
- {
- t = t / d - 1;
- return c * (t * t * t + 1) + b;
- }
- PARFLT par_easings__in_out_cubic(PARFLT t, PARFLT b, PARFLT c, PARFLT d)
- {
- t /= d / 2;
- if (t < 1) {
- return c / 2 * t * t * t + b;
- }
- t -= 2;
- return c / 2 * (t * t * t + 2) + b;
- }
- PARFLT par_easings__in_quad(PARFLT t, PARFLT b, PARFLT c, PARFLT d)
- {
- t /= d;
- return c * t * t + b;
- }
- PARFLT par_easings__out_quad(PARFLT t, PARFLT b, PARFLT c, PARFLT d)
- {
- t /= d;
- return -c * t * (t - 2) + b;
- }
- PARFLT par_easings__in_out_quad(PARFLT t, PARFLT b, PARFLT c, PARFLT d)
- {
- t /= d / 2;
- if (t < 1) {
- return c / 2 * t * t + b;
- }
- --t;
- return -c / 2 * (t * (t - 2) - 1) + b;
- }
- PARFLT par_easings__in_quart(PARFLT t, PARFLT b, PARFLT c, PARFLT d)
- {
- t /= d;
- return c * t * t * t * t + b;
- }
- PARFLT par_easings__out_quart(PARFLT t, PARFLT b, PARFLT c, PARFLT d)
- {
- t = t / d - 1;
- return -c * (t * t * t * t - 1) + b;
- }
- PARFLT par_easings__in_out_quart(PARFLT t, PARFLT b, PARFLT c, PARFLT d)
- {
- t /= d / 2;
- if (t < 1) {
- return c / 2 * t * t * t * t + b;
- }
- t -= 2;
- return -c / 2 * (t * t * t * t - 2) + b;
- }
- PARFLT par_easings__in_quint(PARFLT t, PARFLT b, PARFLT c, PARFLT d)
- {
- t /= d;
- return c * t * t * t * t * t + b;
- }
- PARFLT par_easings__out_quint(PARFLT t, PARFLT b, PARFLT c, PARFLT d)
- {
- t = t / d - 1;
- return c * (t * t * t * t * t + 1) + b;
- }
- PARFLT par_easings__in_out_quint(PARFLT t, PARFLT b, PARFLT c, PARFLT d)
- {
- t /= d / 2;
- if (t < 1) {
- return c / 2 * t * t * t * t * t + b;
- }
- t -= 2;
- return c / 2 * (t * t * t * t * t + 2) + b;
- }
- PARFLT par_easings__in_sine(PARFLT t, PARFLT b, PARFLT c, PARFLT d)
- {
- return -c * cos(t / d * (PAR_PI / 2)) + c + b;
- }
- PARFLT par_easings__out_sine(PARFLT t, PARFLT b, PARFLT c, PARFLT d)
- {
- return c * sin(t / d * (PAR_PI / 2)) + b;
- }
- PARFLT par_easings__in_out_sine(PARFLT t, PARFLT b, PARFLT c, PARFLT d)
- {
- return -c / 2 * (cos(PAR_PI * t / d) - 1) + b;
- }
- PARFLT par_easings__in_out_expo(PARFLT t, PARFLT b, PARFLT c, PARFLT d)
- {
- t /= d / 2;
- if (t < 1) {
- return c / 2 * pow(2, 10 * (t - 1)) + b;
- }
- return c / 2 * (-pow(2, -10 * --t) + 2) + b;
- }
- PARFLT par_easings__in_circ(PARFLT t, PARFLT b, PARFLT c, PARFLT d)
- {
- t /= d;
- return -c * (sqrt(1 - t * t) - 1) + b;
- }
- PARFLT par_easings__out_circ(PARFLT t, PARFLT b, PARFLT c, PARFLT d)
- {
- t = t / d - 1;
- return c * sqrt(1 - t * t) + b;
- }
- PARFLT par_easings__in_out_circ(PARFLT t, PARFLT b, PARFLT c, PARFLT d)
- {
- t /= d / 2;
- if (t < 1) {
- return -c / 2 * (sqrt(1 - t * t) - 1) + b;
- }
- t -= 2;
- return c / 2 * (sqrt(1 - t * t) + 1) + b;
- }
- PARFLT par_easings__in_elastic(PARFLT t, PARFLT b, PARFLT c, PARFLT d)
- {
- PARFLT a, p, s;
- s = 1.70158;
- p = 0;
- a = c;
- if (!p) {
- p = d * 0.3;
- }
- if (a < fabsf(c)) {
- a = c;
- s = p / 4;
- } else {
- s = p / (2 * PAR_PI) * asin(c / a);
- }
- t -= 1;
- return -(a * pow(2, 10 * t) *
- sin((t * d - s) * (2 * PAR_PI) / p)) + b;
- }
- PARFLT par_easings__out_elastic(PARFLT t, PARFLT b, PARFLT c, PARFLT d)
- {
- PARFLT a, p, s;
- s = 1.70158;
- p = 0;
- a = c;
- if (!p) {
- p = d * 0.3;
- }
- if (a < fabsf(c)) {
- a = c;
- s = p / 4;
- } else {
- s = p / (2 * PAR_PI) * asin(c / a);
- }
- return a * pow(2, -10 * t) *
- sin((t * d - s) * (2 * PAR_PI) / p) + c + b;
- }
- PARFLT par_easings__in_out_elastic(PARFLT t, PARFLT b, PARFLT c, PARFLT d)
- {
- PARFLT a, p, s;
- s = 1.70158;
- p = 0;
- a = c;
- if (!p) {
- p = d * (0.3 * 1.5);
- }
- if (a < fabsf(c)) {
- a = c;
- s = p / 4;
- } else {
- s = p / (2 * PAR_PI) * asin(c / a);
- }
- if (t < 1) {
- t -= 1;
- return -0.5 * (a * pow(2, 10 * t) *
- sin((t * d - s) * (2 * PAR_PI) / p)) + b;
- }
- t -= 1;
- return a * pow(2, -10 * t) *
- sin((t * d - s) * (2 * PAR_PI) / p) * 0.5 + c + b;
- }
- PARFLT par_easings__in_back(PARFLT t, PARFLT b, PARFLT c, PARFLT d)
- {
- PARFLT s = 1.70158;
- t/=d;
- return c*t*t*((s+1)*t - s) + b;
- }
- PARFLT par_easings__out_back(PARFLT t, PARFLT b, PARFLT c, PARFLT d)
- {
- PARFLT s = 1.70158;
- t=t/d-1;
- return c*(t*t*((s+1)*t + s) + 1) + b;
- }
- PARFLT par_easings__in_out_back(PARFLT t, PARFLT b, PARFLT c, PARFLT d)
- {
- PARFLT s = 1.70158;
- t/=d/2;
- s*=1.525;
- if (t < 1) { return c/2*(t*t*((s+1)*t - s)) + b; }
- s*=1.525;
- t-=2;
- return (c/2*(t*t*(s+1)*t + s) + 2) + b;
- }
- PARFLT par_easings__out_bounce(PARFLT t, PARFLT b, PARFLT c, PARFLT d);
- PARFLT par_easings__in_bounce(PARFLT t, PARFLT b, PARFLT c, PARFLT d)
- {
- PARFLT v = par_easings__out_bounce(d - t, 0, c, d);
- return c - v + b;
- }
- PARFLT par_easings__out_bounce(PARFLT t, PARFLT b, PARFLT c, PARFLT d)
- {
- t /= d;
- if (t < 1.0 / 2.75) {
- return c * (7.5625 * t * t) + b;
- }
- if (t < 2.0 / 2.75) {
- t -= 1.5 / 2.75;
- return c * (7.5625 * t * t + 0.75) + b;
- }
- if (t < 2.5 / 2.75) {
- t -= 2.25 / 2.75;
- return c * (7.5625 * t * t + 0.9375) + b;
- }
- t -= 2.625 / 2.75;
- return c * (7.5625 * t * t + 0.984375) + b;
- }
- PARFLT par_easings__in_out_bounce(PARFLT t, PARFLT b, PARFLT c, PARFLT d)
- {
- PARFLT v;
- if (t < d / 2) {
- v = par_easings__in_bounce(t * 2, 0, c, d);
- return v * 0.5 + b;
- }
- v = par_easings__out_bounce(t * 2 - d, 0, c, d);
- return v * 0.5 + c * 0.5 + b;
- }
- PARFLT par_easings_linear(PARFLT t)
- {
- return par_easings__linear(t, 0, 1, 1);
- }
- PARFLT par_easings_in_cubic(PARFLT t)
- {
- return par_easings__in_cubic(t, 0, 1, 1);
- }
- PARFLT par_easings_out_cubic(PARFLT t)
- {
- return par_easings__out_cubic(t, 0, 1, 1);
- }
- PARFLT par_easings_in_out_cubic(PARFLT t)
- {
- return par_easings__in_out_cubic(t, 0, 1, 1);
- }
- PARFLT par_easings_in_quad(PARFLT t)
- {
- return par_easings__in_quad(t, 0, 1, 1);
- }
- PARFLT par_easings_out_quad(PARFLT t)
- {
- return par_easings__out_quad(t, 0, 1, 1);
- }
- PARFLT par_easings_in_out_quad(PARFLT t)
- {
- return par_easings__in_out_quad(t, 0, 1, 1);
- }
- PARFLT par_easings_in_elastic(PARFLT t)
- {
- return par_easings__in_elastic(t, 0, 1, 1);
- }
- PARFLT par_easings_out_elastic(PARFLT t)
- {
- return par_easings__out_elastic(t, 0, 1, 1);
- }
- PARFLT par_easings_in_out_elastic(PARFLT t)
- {
- return par_easings__in_out_elastic(t, 0, 1, 1);
- }
- PARFLT par_easings_in_bounce(PARFLT t)
- {
- return par_easings__in_bounce(t, 0, 1, 1);
- }
- PARFLT par_easings_out_bounce(PARFLT t)
- {
- return par_easings__out_bounce(t, 0, 1, 1);
- }
- PARFLT par_easings_in_out_bounce(PARFLT t)
- {
- return par_easings__in_out_bounce(t, 0, 1, 1);
- }
- PARFLT par_easings_in_back(PARFLT t)
- {
- return par_easings__in_back(t, 0, 1, 1);
- }
- PARFLT par_easings_out_back(PARFLT t)
- {
- return par_easings__out_back(t, 0, 1, 1);
- }
- PARFLT par_easings_in_out_back(PARFLT t)
- {
- return par_easings__in_out_back(t, 0, 1, 1);
- }
- #undef PARFLT
- #endif // PAR_EASINGS_IMPLEMENTATION
- #endif // PAR_EASINGS_H
- // par_easings is distributed under the MIT license:
- //
- // Copyright (c) 2019 Philip Rideout
- //
- // Permission is hereby granted, free of charge, to any person obtaining a copy
- // of this software and associated documentation files (the "Software"), to deal
- // in the Software without restriction, including without limitation the rights
- // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- // copies of the Software, and to permit persons to whom the Software is
- // furnished to do so, subject to the following conditions:
- //
- // The above copyright notice and this permission notice shall be included in
- // all copies or substantial portions of the Software.
- //
- // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
- // SOFTWARE.
|