因为太多了,而且在我项目里是策略实现,这三十多种搞完真的吐一口老血

如果有类似需要,建议是用文本替换去直接用现成的,(又吐一口老血)

曲线演示与函数

在这里插入图片描述

曲线的接口

public interface ICurve
    {
    	// 0 <= x <= 1
        public float Evaluate(float x);
		
    }

获取曲线的方式

  public class BuiltInCurve
    {
        public static ICurve GetCurve(EaseCurve curveType)
        {
            switch (curveType)
            {
                case EaseCurve.Linear:
                    return LinearCurve.Instance;

                case EaseCurve.InSine:
                    return InSineCurve.Instance;
                case EaseCurve.OutSine:
                    return OutSineCurve.Instance;
                case EaseCurve.InOutSine:
                    return InOutSineCurve.Instance;


                case EaseCurve.InQuad:
                    return InQuadCurve.Instance;
                case EaseCurve.OutQuad:
                    return OutQuadCurve.Instance;
                case EaseCurve.InOutQuad:
                    return InOutQuadCurve.Instance;


                case EaseCurve.InCubic:
                    return InCubicCurve.Instance;
                case EaseCurve.OutCubic:
                    return OutCubicCurve.Instance;
                case EaseCurve.InOutCubic:
                    return InOutCubicCurve.Instance;


                case EaseCurve.InQuart:
                    return InQuartCurve.Instance;
                case EaseCurve.OutQuart:
                    return OutQuartCurve.Instance;
                case EaseCurve.InOutQuart:
                    return InOutQuartCurve.Instance;


                case EaseCurve.InQuint:
                    return InQuintCurve.Instance;
                case EaseCurve.OutQuint:
                    return OutQuintCurve.Instance;
                case EaseCurve.InOutQuint:
                    return InOutQuintCurve.Instance;


                case EaseCurve.InExpo:
                    return InExpoCurve.Instance;
                case EaseCurve.OutExpo:
                    return OutExpoCurve.Instance;
                case EaseCurve.InOutExpo:
                    return InOutExpoCurve.Instance;


                case EaseCurve.InCirc:
                    return InCircCurve.Instance;
                case EaseCurve.OutCirc:
                    return OutCircCurve.Instance;
                case EaseCurve.InOutCirc:
                    return InOutCircCurve.Instance;


                case EaseCurve.InBack:
                    return InBackCurve.Instance;
                case EaseCurve.OutBack:
                    return OutBackCurve.Instance;
                case EaseCurve.InOutBack:
                    return InOutBackCurve.Instance;


                case EaseCurve.InBounce:
                    return InBounceCurve.Instance;
                case EaseCurve.OutBounce:
                    return OutBounceCurve.Instance;
                case EaseCurve.InOutBounce:
                    return InOutBounceCurve.Instance;


                case EaseCurve.InElastic:
                    return InElasticCurve.Instance;
                case EaseCurve.OutElastic:
                    return OutElasticCurve.Instance;
                case EaseCurve.InOutElastic:
                    return InOutElasticCurve.Instance;


                default:
                    throw new ArgumentException("Invalid EaseCurve value", "curveType");
            }
        }

    }

曲线的枚举



namespace AirFramework
{

    public enum EaseCurve
    {
        Linear,         // 线性缓动,匀速运动

        InSine,         // 正弦加速缓动,开始缓慢,后来加速
        OutSine,        // 正弦减速缓动,开始快速,后来减速
        InOutSine,      // 正弦波缓动,先加速后减速

        InQuad,         // 二次方加速缓动,开始缓慢,后来加速
        OutQuad,        // 二次方减速缓动,开始快速,后来减速
        InOutQuad,      // 二次方波缓动,先加速后减速

        InCubic,        // 三次方加速缓动,开始缓慢,后来加速
        OutCubic,       // 三次方减速缓动,开始快速,后来减速
        InOutCubic,     // 三次方波缓动,先加速后减速

        InQuart,        // 四次方加速缓动,开始缓慢,后来加速
        OutQuart,       // 四次方减速缓动,开始快速,后来减速
        InOutQuart,     // 四次方波缓动,先加速后减速

        InQuint,        // 五次方加速缓动,开始缓慢,后来加速
        OutQuint,       // 五次方减速缓动,开始快速,后来减速
        InOutQuint,     // 五次方波缓动,先加速后减速

        InExpo,         // 指数加速缓动,开始缓慢,后来加速
        OutExpo,        // 指数减速缓动,开始快速,后来减速
        InOutExpo,      // 指数波缓动,先加速后减速

        InCirc,         // 圆形加速缓动,开始缓慢,后来加速
        OutCirc,        // 圆形减速缓动,开始快速,后来减速
        InOutCirc,      // 圆形波缓动,先加速后减速

        InOutBack,      // 先正弦减速后正弦加速缓动,开始快速,后来减速,再加速
        InBack,         // 回退加速(类似弹簧),开始缓慢,后来加速并回退一定距离,然后减速回弹
        OutBack,        // 回退减速(类似弹簧),开始快速,后来减速并回退一定距离,然后加速回

        InBounce,       // 弹跳加速缓动,开始缓慢,后来加速并反弹
        OutBounce,      // 弹跳减速缓动,开始快速,后来减速并反弹
        InOutBounce,    // 弹跳波缓动,先加速后减速并反弹

        InElastic,      // 弹性加速缓动,开始缓慢,后来加速并弹性拉伸
        OutElastic,     // 弹性减速缓动,开始快速,后来减速并弹性拉伸
        InOutElastic,   // 弹性波缓动,先加速后减速并弹性拉伸

    }

}

以下是曲线的实现

using System;

namespace AirFramework
{
    #region Linear
    public class LinearCurve : Singleton<LinearCurve>, ICurve
    {
        public float Evaluate(float x)
        {
            return x;
        }
    }

    #endregion

    #region Sine

    public class InSineCurve : Singleton<InSineCurve>, ICurve
    {
        public float Evaluate(float x)
        {
            return -(float)Math.Cos((double)(x * 1.5707964f)) + 1f;
        }
    }
    //2
    public class OutSineCurve : Singleton<OutSineCurve>, ICurve
    {
        public float Evaluate(float x)
        {
            return (float)Math.Sin((double)(x * 1.5707964f));
        }
    }
    //3
    public class InOutSineCurve : Singleton<InOutSineCurve>, ICurve
    {
        public float Evaluate(float x)
        {
            return -0.5f * ((float)Math.Cos((double)(Math.PI * x)) - 1f);
        }
    }

    #endregion

    #region Quad
    //4
    public class InQuadCurve : Singleton<InQuadCurve>, ICurve
    {
        public float Evaluate(float x)
        {
            return x * x;
        }
    }
    //5
    public class OutQuadCurve : Singleton<OutQuadCurve>, ICurve
    {
        public float Evaluate(float x)
        {
            return 1f - (1f - x) * (1f - x);
        }
    }
    //6
    public class InOutQuadCurve : Singleton<InOutQuadCurve>, ICurve
    {
        public float Evaluate(float x)
        {
            return x < 0.5f ? 2f * x * x : 1f - 2f * (1f - x) * (1f - x);
        }
    }
    #endregion


    #region Cubic
    //7
    public class InCubicCurve : Singleton<InCubicCurve>, ICurve
    {
        public float Evaluate(float x)
        {
            return x * x * x;
        }
    }
    //8
    public class OutCubicCurve : Singleton<OutCubicCurve>, ICurve
    {
        public float Evaluate(float x)
        {
            var t = x - 1f;
            return t * t * t + 1f;
        }
    }
    //9
    public class InOutCubicCurve : Singleton<InOutCubicCurve>, ICurve
    {
        public float Evaluate(float x)
        {
            return x < 0.5f ? 4f * x * x * x : 1f + 4f * (x - 1f) * (x - 1f) * (x - 1f);
        }
    }

    #endregion


    #region Quart
    //10
    public class InQuartCurve : Singleton<InQuartCurve>, ICurve
    {
        public float Evaluate(float x)
        {
            return x * x * x * x;
        }
    }
    //11
    public class OutQuartCurve : Singleton<OutQuartCurve>, ICurve
    {
        public float Evaluate(float x)
        {
            var t = x - 1f;
            return 1f - t * t * t * t;
        }
    }
    //12
    public class InOutQuartCurve : Singleton<InOutQuartCurve>, ICurve
    {
        public float Evaluate(float x)
        {
            return x < 0.5f ? 8f * x * x * x * x : 1f - 8f * (x - 1f) * (x - 1f) * (x - 1f) * (x - 1f);
        }
    }

    #endregion


    #region Quint
    //13
    public class InQuintCurve : Singleton<InQuintCurve>, ICurve
    {
        public float Evaluate(float x)
        {
            return x * x * x * x * x;
        }
    }
    //14
    public class OutQuintCurve : Singleton<OutQuintCurve>, ICurve
    {
        public float Evaluate(float x)
        {
            var t = x - 1f;
            return t * t * t * t * t + 1f;
        }
    }
    //15
    public class InOutQuintCurve : Singleton<InOutQuintCurve>, ICurve
    {
        public float Evaluate(float x)
        {
            return x < 0.5f ? 16f * x * x * x * x * x : 1f + 16f * (x - 1f) * (x - 1f) * (x - 1f) * (x - 1f) * (x - 1f);
        }
    }

    #endregion


    #region Expo
    //16
    public class InExpoCurve : Singleton<InExpoCurve>, ICurve
    {
        public float Evaluate(float x)
        {
            return x == 0f ? 0f : (float)Math.Pow(2f, 10f * (x - 1f));
        }
    }
    //17
    public class OutExpoCurve : Singleton<OutExpoCurve>, ICurve
    {
        public float Evaluate(float x)
        {
            return x == 1f ? 1f : -(float)Math.Pow(2f, -10f * x) + 1f;
        }
    }
    //18
    public class InOutExpoCurve : Singleton<InOutExpoCurve>, ICurve
    {
        public float Evaluate(float x)
        {
            if (x == 0f) return 0f;
            if (x == 1f) return 1f;
            return x < 0.5f ? 0.5f * (float)Math.Pow(2f, 10f * (2f * x - 1f)) : 0.5f * (-(float)Math.Pow(2f, -10f * (2f * x - 1f)) + 2f);
        }
    }


    #endregion
  


    #region Circ
    public class InCircCurve : Singleton<InCircCurve>, ICurve
    {
        public float Evaluate(float x)
        {
            return 1f - (float)Math.Sqrt(1f - x * x);
        }
    }
    //20
    public class OutCircCurve : Singleton<OutCircCurve>, ICurve
    {
        public float Evaluate(float x)
        {
            return (float)Math.Sqrt(1f - (x - 1f) * (x - 1f));
        }
    }
    //21
    public class InOutCircCurve : Singleton<InOutCircCurve>, ICurve
    {
        public float Evaluate(float x)
        {
            return x < 0.5f ? 0.5f * (1f - (float)Math.Sqrt(1f - 4f * x * x)) : 0.5f * ((float)Math.Sqrt(1f - 4f * (x - 1f) * (x - 1f)) + 1f);
        }
    }

    #endregion


    #region Back

    //22
    public class InBackCurve : Singleton<InBackCurve>, ICurve
    {
        private const float c1 = 1.70158f;
        private const float c2 = c1 * 1.525f;

        public float Evaluate(float x)
        {
            return c2 * x * x * x - c1 * x * x;
        }

    }

    //23
    public class OutBackCurve : Singleton<OutBackCurve>, ICurve
    {
        private const float c1 = 1.70158f;
        private const float c2 = c1 * 1.525f;


        public float Evaluate(float x)
        {

            return x * x * ((c1 + 1) * x + c1) + 1;
			
        }
    }


    //27
    public class InOutBackCurve : Singleton<InOutBackCurve>, ICurve
    {
        public float Evaluate(float x)
        {
            const float c1 = 1.70158f;
            const float c2 = c1 * 1.525f;
            return (float)(x < 0.5f ? 2 * x * x * ((c2 + 1) * 2 * x - c2) : (float)(1 + Math.Pow(2 * x - 2, 3) + c2 * Math.Pow(2 * x - 2, 2)) / 2);
        }
    }

    #endregion


    #region Elastic
    //24
    public class InElasticCurve : Singleton<InElasticCurve>, ICurve
    {
        public float Evaluate(float x)
        {
            const float c4 = (2 * (float)Math.PI) / 3;
            return (float)((x == 0) ? 0 : ((x == 1) ? 1 : -Math.Pow(2, 10 * x - 10) * Math.Sin((x * 10 - 10.75) * c4)));
        }
    }
    //25
    public class OutElasticCurve : Singleton<OutElasticCurve>, ICurve
    {
        public float Evaluate(float x)
        {
            const float c4 = (2 * (float)Math.PI) / 3;
            return (float)((x == 0) ? 0 : ((x == 1) ? 1 : Math.Pow(2, -10 * x) * Math.Sin((x * 10 - 0.75) * c4) + 1));
        }
    }
    //26
    public class InOutElasticCurve : Singleton<InOutElasticCurve>, ICurve
    {
        public float Evaluate(float x)
        {
            const float c5 = (2f * (float)Math.PI) / 4.5f;
            return (float)((x == 0) ? 0 : ((x == 1) ? 1 : (x < 0.5f ? -(Math.Pow(2, 20 * x - 10) * Math.Sin((20 * x - 11.125f) * c5)) / 2 : (Math.Pow(2, -20 * x + 10) * Math.Sin((20 * x - 11.125f) * c5)) / 2 + 1)));
        }
    }
    #endregion



    #region Bounce
    //28
    public class InBounceCurve : Singleton<InBounceCurve>, ICurve
    {
        public float Evaluate(float x)
        {
            return 1 - OutBounceCurve.Instance.Evaluate(1 - x);
        }
    }
    //29
    public class OutBounceCurve : Singleton<OutBounceCurve>, ICurve
    {
        public float Evaluate(float x)
        {
            const float n1 = 7.5625f;
            const float d1 = 2.75f;

            if (x < 1 / d1)
            {
                return n1 * x * x;
            }
            else if (x < 2 / d1)
            {
                return n1 * (x -= 1.5f / d1) * x + 0.75f;
            }
            else if (x < 2.5 / d1)
            {
                return n1 * (x -= 2.25f / d1) * x + 0.9375f;
            }
            else
            {
                return n1 * (x -= 2.625f / d1) * x + 0.984375f;
            }
        }
    }
    //30

    public class InOutBounceCurve : Singleton<OutBounceCurve>, ICurve
    {
        public float Evaluate(float x)
        {
            return x < 0.5f? (1 - Evaluate(1 - 2 * x)) / 2: (1 + Evaluate(2 * x - 1)) / 2;
        }
    }
    #endregion
}