Ejemplo n.º 1
0
        public static void DrawGizmos(Func <float, float> function, Vector3 position, Vector2 size, Color axis, Color line, int precision = 100)
        {
            Color gizmosColor = Gizmos.color;

            Gizmos.color = axis;
            Gizmos.DrawLine(position, position + Vector3.right * size.x);
            Gizmos.DrawLine(position, position + Vector3.up * size.y);

            Gizmos.color = line;
            Vector3 to, from = position;

            for (int i = 0; i < precision; i++)
            {
                float t = HMath.Clamp01((i + 1F) / precision);

                float d = function(t);

                to = position + new Vector3(t * size.x, d * size.y);

                Gizmos.DrawLine(from, to);

                from = to;
            }

            Gizmos.color = gizmosColor;
        }
Ejemplo n.º 2
0
        public static float FirstDerivative(float t, params float[] points)
        {
            t = HMath.Clamp01(t);

            if (t == 1F)
            {
                return(0F);
            }

            int n = points.Length;

            switch (n)
            {
            case 0:
            case 1:
                return(0F);

            case 2: return(points[1] - points[0]);

            case 3: return(2F * ((points[2] - 2F * points[1] + points[0]) * t + points[1] - points[0]));

            case 4:
                return(3F * ((points[3] - 3F * points[2] + 3F * points[1] - points[0]) * (t * t)
                             + 2F * (points[2] - 2F * points[1] + points[0]) * t + points[1] - points[0]));

            default:
                n--;

                float combination, d = 1F - t;

                float add = points[0] * (-n * HMath.Pow(d, n - 1));

                for (int i = 1; i <= n; i++)
                {
                    combination = HMath.Combinations(n, i);
                    add        += combination * points[i] * (-(n - i) * HMath.Pow(d, n - i - 1)) * HMath.Pow(t, i)
                                  + combination * points[i] * HMath.Pow(d, n - i) * i * HMath.Pow(t, i - 1);
                }

                return(add);
            }
        }
Ejemplo n.º 3
0
        public static Vector2 Lerp(float t, params Vector2[] points)
        {
            t = HMath.Clamp01(t);

            float d = 1F - t;

            int n = points.Length;

            switch (n)
            {
            case 0: return(Vector2.zero);

            case 1: return(points[0]);

            case 2: return(points[0] * d + points[1] * t);

            case 3: return(points[0] * d * d + 2F * points[1] * d * t + points[2] * t * t);

            case 4:
                float td3 = 3F * t * d;
                return(points[0] * d * d * d + points[1] * td3 * d + points[2] * td3 * t + points[3] * t * t * t);

            default:
                n--;

                float pt = 1F;

                Vector2 add = Vector2.zero;

                for (int i = 0; i <= n; i++, pt *= t)
                {
                    add += HMath.Combinations(n, i) * points[i] * HMath.Pow(d, n - i) * pt;
                }

                return(add);
            }
        }
Ejemplo n.º 4
0
        public static Bounds GetBounds(params Bounds[] bounds)
        {
            float mx = bounds[0].min.x;
            float my = bounds[0].min.y;
            float mz = bounds[0].min.z;
            float Mx = bounds[0].max.x;
            float My = bounds[0].max.y;
            float Mz = bounds[0].max.z;

            for (int i = 1; i < bounds.Length; i++)
            {
                mx = HMath.Min(mx, bounds[i].min.x);
                my = HMath.Min(my, bounds[i].min.y);
                mz = HMath.Min(mz, bounds[i].min.z);
                Mx = HMath.Max(Mx, bounds[i].max.x);
                My = HMath.Max(My, bounds[i].max.y);
                Mz = HMath.Max(Mz, bounds[i].max.z);
            }

            Vector3 size   = new Vector3(Mx - mx, My - my, Mz - mz);
            Vector3 center = new Vector3(mx, my, mz) + size / 2F;

            return(new Bounds(center, size));
        }
Ejemplo n.º 5
0
 /// <summary>
 ///
 /// </summary>
 public T[] Clamp(T[] values)
 {
     return(HMath.Clamp(this, values));
 }
Ejemplo n.º 6
0
 /// <summary>
 ///
 /// </summary>
 public T Clamp(T value)
 {
     return(HMath.Clamp <T>(value, Min, Max));
 }
Ejemplo n.º 7
0
 public static Func <float, float> PingPong(float loops)
 {
     return((t) => { return HMath.PingPong(t * loops * 2F, 1F); });
 }
Ejemplo n.º 8
0
 /// <summary>
 /// Linearly interpolates between min and max by t.
 /// </summary>
 /// <param name="t">
 /// The interpolation value between 0f and 1f.
 /// </param>
 public override float Lerp(float t)
 {
     return(HMath.Lerp(min, max, t));
 }
Ejemplo n.º 9
0
 /// <summary>
 /// Linearly interpolates between min and max by t.
 /// </summary>
 /// <param name="t">
 /// The interpolation value between 0f and 1f.
 /// </param>
 public override int Lerp(float t)
 {
     return(HMath.FloorToInt(HMath.Lerp(min, max, t)));
 }
Ejemplo n.º 10
0
 /// <summary>
 ///
 /// </summary>
 public static float EaseInElastic(float t)
 {
     return(t != 0F && t != 1F ? -HMath.Pow(2F, 10F * (t -= 1F)) * HMath.Sin((t - 0.3f / 4F) * (2F * HMath.PI) / 0.3f) : t);
 }
Ejemplo n.º 11
0
 /// <summary>
 ///
 /// </summary>
 public static float EaseInCirc(float t)
 {
     return(1F - HMath.Sqrt(1F - t * t));
 }
Ejemplo n.º 12
0
 /// <summary>
 ///
 /// </summary>
 public static float EaseOutExp(float t)
 {
     return(t != 1F ? 1F - HMath.Pow(2F, -10F * t) : 1F);
 }
Ejemplo n.º 13
0
 /// <summary>
 ///
 /// </summary>
 public static float EaseInSine(float t)
 {
     return(1F - HMath.Cos(t * HMath.HalfPI));
 }
Ejemplo n.º 14
0
 /// <summary>
 ///
 /// </summary>
 public static float EaseInExp(float t)
 {
     return(t != 0 ? HMath.Pow(2F, 10F * (t - 1F)) : 0F);
 }
Ejemplo n.º 15
0
 /// <summary>
 ///
 /// </summary>
 public static float EaseOutElastic(float t)
 {
     return(t != 0F && t != 1F ? HMath.Pow(2F, -10F * t) * HMath.Sin((t - 0.3f / 4F) * (2F * HMath.PI) / 0.3f) + 1F : t);
 }
Ejemplo n.º 16
0
 /// <summary>
 ///
 /// </summary>
 public static float EaseOutCirc(float t)
 {
     t = 1F - t;
     return(HMath.Sqrt(1F - t * t));
 }
Ejemplo n.º 17
0
 /// <summary>
 ///
 /// </summary>
 public static float EaseOutSine(float t)
 {
     return(HMath.Sin(t * HMath.HalfPI));
 }