CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!
CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!
Path: blob/master/Common/Math/curves.cpp
Views: 1401
1#include <cmath>23#include "Common/Math/math_util.h"4#include "curves.h"56float linearInOut(int t, int fadeInLength, int solidLength, int fadeOutLength) {7if (t < 0) return 0;8if (t < fadeInLength) {9return (float)t / fadeInLength;10}11t -= fadeInLength;12if (t < solidLength) {13return 1.0f;14}15t -= solidLength;16if (t < fadeOutLength) {17return 1.0f - (float)t / fadeOutLength;18}19return 0.0f;20}2122float linearIn(int t, int fadeInLength) {23if (t < 0) return 0;24if (t < fadeInLength) {25return (float)t / fadeInLength;26}27return 1.0f;28}2930float linearOut(int t, int fadeOutLength) {31return 1.0f - linearIn(t, fadeOutLength);32}3334float ease(float val) {35if (val >= 1.0f) return 1.0f;36if (val <= 0.0f) return 0.0f;37return (float)(((-cosf(val * PI)) + 1.0f) * 0.5);38}3940float ease(int t, int fadeLength)41{42if (t <= 0.0f) return 0.0f;43if (t >= fadeLength) return 1.0f;44return ease((float)t / (float)fadeLength);45}4647template <int hundredthsX1, int hundredthsX2, int hundredthsY1 = 0, int hundredthsY2 = 100>48inline float bezierEaseFunc(float val) {49constexpr float x1 = hundredthsX1 / 100.0f;50constexpr float x2 = hundredthsX2 / 100.0f;51constexpr float a = 1.0f - 3.0f * x2 + 3.0f * x1;52constexpr float b = 3.0f * x2 - 6.0f * x1;53constexpr float c = 3.0f * x1;5455constexpr float y1 = hundredthsY1 / 100.0f;56constexpr float y2 = hundredthsY2 / 100.0f;57constexpr float ya = 1.0f - 3.0f * y2 + 3.0f * y1;58constexpr float yb = 3.0f * y2 - 6.0f * y1;59constexpr float yc = 3.0f * y1;6061float guess = val;62// Newton-Raphson calculation, no need to be too precise.63for (int i = 0; i < 4; ++i) {64float slope = 3.0f * a * guess * guess + 2.0f * b * guess + c;65if (slope == 0.0f) {66break;67}6869float x = ((a * guess + b) * guess + c) * guess - val;70guess -= x / slope;71}7273return ((ya * guess + yb) * guess + yc) * guess;74}7576float bezierEase(float val) {77return bezierEaseFunc<25, 25, 10, 100>(val);78}7980float bezierEaseInOut(float val) {81return bezierEaseFunc<42, 58>(val);82}8384float bezierEaseIn(float val) {85return bezierEaseFunc<42, 100>(val);86}8788float bezierEaseOut(float val) {89return bezierEaseFunc<0, 58>(val);90}9192float sawtooth(int t, int period) {93return (t % period) * (1.0f / (period - 1));94}9596float passWithPause(int t, int fadeInLength, int pauseLength, int fadeOutLength)97{98if (t < fadeInLength) {99return -1.0f + (float)t / fadeInLength;100}101t -= fadeInLength;102if (t < pauseLength) {103return 0.0f;104}105t -= pauseLength;106if (t < fadeOutLength) {107return (float)t / fadeOutLength;108}109return 1.0f;110}111112113