Пример #1
0
        public static IEnumerable <float> NormalDistribution()
        {
            bool  ready  = false;
            float second = 0.0f;

            while (true)
            {
                if (ready)
                {
                    ready = false;
                    yield return(second);
                }
                else
                {
                    var cr  = UnityEngine.Random.insideUnitCircle;
                    var lcr = cr.magnitude;
                    var r   = MathExOps.Sqrt(-2.0f * MathExOps.Log(lcr) / lcr);

                    second = r * cr.x;
                    ready  = true;
                    yield return(r * cr.y);
                }
            }

            // yield break;
        }
Пример #2
0
 public static float easeExpoInOut(float t)
 {
     if (t == 0f)
     {
         return(0f);
     }
     if (t == 1f)
     {
         return(1f);
     }
     t *= 2f;
     if (t < 1f)
     {
         return(0.5f * MathExOps.Pow(2f, 10f * (t - 1f)));
     }
     return(1f - 0.5f * MathExOps.Pow(2f, -10f * (t - 1f)));
 }
Пример #3
0
        protected int calculateT(ref float t)
        {
            int i;

            if (t >= 1f)
            {
                t = 1f;
                i = p.Length - 4;
            }
            else
            {
                t  = MathExOps.Clamp(t, 0, 1) * (numberOfNodes - 1);
                i  = (int)t;
                t -= i;
                i *= 3;
            }

            return(i);
        }
Пример #4
0
        public static IEnumerable <CurveIterator <vec2> > Iterate(this circle c, int sectors)
        {
            if (c.isEmpty)
            {
                yield break;
            }

            float dA0 = 0;
            float dA  = 2f * MathExOps.PI / sectors;

            for (int i = 0; i <= sectors; i++)
            {
                Foreach.CurveIterator <vec2> ci = new Foreach.CurveIterator <vec2>();
                float a = dA0 + i * dA;
                ci.t     = ((float)i) / sectors;
                ci.value = c.o + c.r * (new vec2(MathExOps.Cos(a), MathExOps.Sin(a)));
                yield return(ci);
            }
            yield break;
        }
Пример #5
0
        public circle(vec2 a, vec2 b, vec2 c)
        {
            float det = (a.x - b.x) * (b.y - c.y) - (b.x - c.x) * (a.y - b.y);

            if (MathExOps.Abs(det) < float.Epsilon)
            {
                o = vec2.empty;
                r = 0f;
            }
            else
            {
                float offset = b ^ b;
                float bc     = ((a ^ a) - offset) / 2f;
                float cd     = (offset - (c ^ c)) / 2f;

                float idet = 1f / det;

                o = new vec2((bc * (b.y - c.y) - cd * (a.y - b.y)) * idet
                             , (cd * (a.x - b.x) - bc * (b.x - c.x)) * idet);
                r = (a - o).length;
            }
        }
Пример #6
0
        public static IEnumerable <CurveIterator <T> > IterateEquidistant <T>(this curve <T> c, float fraction)
        {
            var   d      = c.distance;
            float length = d.length;
            float delta  = d.length * fraction;

            float cd = 0;

            while (true)
            {
                CurveIterator <T> i;
                i.t        = d.time(cd);
                i.value    = c.value(i.t);
                i.velocity = c.velocity(i.t);
                yield return(i);

                if (cd == length)
                {
                    yield break;
                }

                cd = MathExOps.Clamp(cd + delta, 0, length);
            }
        }
Пример #7
0
        public static IEnumerable <CurveIterator <T> > Iterate <T>(this curve <T> c, float stepMultiplier)
        {
            float sl    = c.length;
            float islsl = 1 / (sl * sl);

            float t = 0;

            while (true)
            {
                CurveIterator <T> i;
                i.t        = t;
                i.value    = c.value(t);
                i.velocity = c.velocity(t);
                yield return(i);

                if (t == 1)
                {
                    yield break;
                }

                float dt = MathExOps.Clamp(MathTypeTag <T> .Get().scalar(i.velocity) * islsl * stepMultiplier, islsl, 1);
                t = MathExOps.Clamp01(t + dt);
            }
        }
Пример #8
0
 public static int Clamp(this vec2 v, int f)
 {
     return(MathExOps.Clamp(f, (int)v.x, (int)v.y));
 }
Пример #9
0
 public static vec2 Adc(this vec2 v, float th)
 {
     return(new vec2(MathExOps.Abs(v.x) < th ? 0 : MathExOps.Sign(v.x), MathExOps.Abs(v.y) < th ? 0 : MathExOps.Sign(v.y)));
 }
Пример #10
0
 public static vec2i Clamp(this vec2i v, vec2i min, vec2i max)
 {
     return(MathExOps.Clamp(v, min, max));
 }
Пример #11
0
 public static float Clamp(this vec2i v, float f)
 {
     return(MathExOps.Clamp(f, v.x, v.y));
 }
Пример #12
0
 public static bool eq(float a, float b, float eps) => MathExOps.Abs(a - b) <= eps;
Пример #13
0
 public static float easeExpoOut(float t)
 {
     return(t == 1f ? 1f
                         : 1f - MathExOps.Pow(2f, -10f * t));
 }
Пример #14
0
 public static float easeExpoIn(float t)
 {
     return(t == 0f ? 0f
                         : MathExOps.Pow(2f, 10f * (t - 1f)));
 }