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);
            }
        }