Example #1
0
        /// <summary>
        /// Spherically interpolates between a and b by a factor.
        /// </summary>
        /// <param name="a"></param>
        /// <param name="b"></param>
        /// <param name="factor"></param>
        /// <returns></returns>
        public static Quaternion Slerp(Quaternion a, Quaternion b, float factor)
        {
            float x, y, z, w;
            float opposite, inverse;
            var   dot = Dot(a, b);

            if (Maths.Abs(dot) > (1.0 - Mathf.Epsilon))
            {
                inverse  = 1.0f - factor;
                opposite = factor * Maths.Sign(dot);
            }
            else
            {
                var acos       = (float)Maths.Acos(Maths.Abs(dot));
                var inverseSin = (float)(1.0 / Maths.Sin(acos));

                inverse  = (float)(Maths.Sin((1.0f - factor) * acos) * inverseSin);
                opposite = (float)(Maths.Sin(factor * acos) * inverseSin * Maths.Sign(dot));
            }

            x = (inverse * a.X) + (opposite * b.X);
            y = (inverse * a.Y) + (opposite * b.Y);
            z = (inverse * a.Z) + (opposite * b.Z);
            w = (inverse * a.W) + (opposite * b.W);

            return(new Quaternion(x, y, z, w));
        }
 /// <summary>
 /// 判断两数是否符号相同
 /// </summary>
 /// <param name="value"></param>
 /// <param name="other"></param>
 /// <returns>同号时返回true;异号返回false</returns>
 public static bool IsSameSign(this double value, double other)
 {
     if (value.IsZero())
     {
         return(other.IsZero());
     }
     else if (other.IsZero())
     {
         return(false);
     }
     else//两数皆不为0
     {
         return(Math.Sign(value) * Math.Sign(other) >= 0);
     }
 }
Example #3
0
        public void Sign()
        {
            //Sign(int)
            const int ip1 = 1;
            const int in1 = -1;

            Cmpr(Fast.Sign(ip1), Def.Sign(ip1), "Sign(1)");
            Cmpr(Fast.Sign(in1), Def.Sign(in1), "Sign(-1)");
            Cmpr(Fast.Sign(int.MaxValue), Def.Sign(int.MaxValue), "Sign(int.max)");
            Cmpr(Fast.Sign(int.MaxValue - 1), Def.Sign(int.MaxValue - 1), "Sign(int.max-1)");
            Cmpr(Fast.Sign(int.MinValue), Def.Sign(int.MinValue), "Sign(int.min)");
            Cmpr(Fast.Sign(int.MinValue + 1), Def.Sign(int.MinValue + 1), "Sign(int.min+1)");

            //Sign(long)
            const long lp1 = 1;
            const long ln1 = -1;

            Cmpr(Fast.Sign(lp1), Def.Sign(lp1), "Sign(1L)");
            Cmpr(Fast.Sign(ln1), Def.Sign(ln1), "Sign(-1L)");
            Cmpr(Fast.Sign(long.MaxValue), Def.Sign(long.MaxValue), "Sign(long.max)");
            Cmpr(Fast.Sign(long.MaxValue - 1L), Def.Sign(long.MaxValue - 1L), "Sign(long.max-1)");
            Cmpr(Fast.Sign(long.MinValue), Def.Sign(long.MinValue), "Sign(long.min)");
            Cmpr(Fast.Sign(long.MinValue + 1L), Def.Sign(long.MinValue + 1L), "Sign(long.min+1)");

            //Sign(short)
            const short sp1 = 1;
            const short sn1 = -1;

            Cmpr(Fast.Sign(sp1), Def.Sign(sp1), "Sign(1s)");
            Cmpr(Fast.Sign(sn1), Def.Sign(sn1), "Sign(-1s)");
            Cmpr(Fast.Sign(short.MaxValue), Def.Sign(short.MaxValue), "Sign(short.max)");
            Cmpr(Fast.Sign((short)(short.MaxValue - 1)), Def.Sign((short)(short.MaxValue - 1)), "Sign(short.max-1)");
            Cmpr(Fast.Sign(short.MinValue), Def.Sign(short.MinValue), "Sign(short.min)");
            Cmpr(Fast.Sign((short)(short.MinValue + 1)), Def.Sign((short)(short.MinValue + 1)), "Sign(short.min+1)");

            //Sign(sbyte)
            const sbyte bp1 = 1;
            const sbyte bn1 = -1;

            Cmpr(Fast.Sign(bp1), Def.Sign(bp1), "Sign(1b)");
            Cmpr(Fast.Sign(bn1), Def.Sign(bn1), "Sign(-1b)");
            Cmpr(Fast.Sign(sbyte.MaxValue), Def.Sign(sbyte.MaxValue), "Sign(sbyte.max)");
            Cmpr(Fast.Sign((sbyte)(sbyte.MaxValue - 1)), Def.Sign((sbyte)(sbyte.MaxValue - 1)), "Sign(sbyte.max-1)");
            Cmpr(Fast.Sign(sbyte.MinValue), Def.Sign(sbyte.MinValue), "Sign(sbyte.min)");
            Cmpr(Fast.Sign((sbyte)(sbyte.MinValue + 1)), Def.Sign((sbyte)(sbyte.MinValue + 1)), "Sign(sbyte.min+1)");
        }
Example #4
0
 public static Vector3d Sign(Vector3d vector)
 {
     return(new Vector3d(Math.Sign(vector.x), Math.Sign(vector.y), Math.Sign(vector.z)));
 }
Example #5
0
 /// <summary>
 /// -1: left 0:lined 1:right
 /// </summary>
 public static int PointBelongedSide(Vector2 point, Vector2 from, Vector2 to)
 {
     return(MathLib.Sign((point.X - from.X) * (to.X - from.X) + (point.Y - from.Y) * (to.Y - from.Y)));
 }
Example #6
0
 // Inverse Hyperbolic Cosecant
 public static double HArccosec(double x)
 {
     return(MathObj.Log((MathObj.Sign(x) * MathObj.Sqrt(x * x + 1) + 1) / x));
 }
Example #7
0
 // Inverse Secant
 public static double Arcsec(double x)
 {
     return(2 * MathObj.Atan(1) - MathObj.Atan(MathObj.Sign(x) / MathObj.Sqrt(x * x - 1)));
 }
Example #8
0
        public static PointPair GetIntersectionOfCircles(
            Point center1,
            double radius1,
            Point center2,
            double radius2)
        {
            var result = Math.InfinitePointPair;
            var x1     = center1.X;
            var y1     = center1.Y;
            var x2     = center2.X;
            var y2     = center2.Y;

            var r3 = center1.Distance(center2);

            if (r3 == 0)
            {
                return(result);
            }

            if (y1 == y2)
            {
                if (radius1 + radius1 > r3 + Epsilon &&
                    radius1 + r3 > radius2 + Epsilon &&
                    radius2 + r3 > radius1 + Epsilon &&
                    x1 != x2)
                {
                    var x3   = (radius1.Sqr() + r3.Sqr() - radius2.Sqr()) / (2 * r3);
                    var tsqr = (radius1.Sqr() - x3.Sqr()).SquareRoot();
                    result.P1.X = x1 + (x2 - x1) * x3 / r3;
                    result.P1.Y = y1 - tsqr;
                    result.P2.X = result.P1.X;
                    result.P2.Y = y1 + tsqr;
                    if (x2 < x1)
                    {
                        var t = result.P1.Y;
                        result.P1.Y = result.P2.Y;
                        result.P2.Y = t;
                    }
                }
                else if ((radius1 + radius2 - r3).Abs() <= Epsilon)
                {
                    result.P1.X = x1 + M.Sign(x2 - x1) * radius1;
                    result.P1.Y = y1;
                    result.P2   = result.P1;
                }
                else if ((radius1 + r3 - radius2).Abs() <= Epsilon ||
                         (r3 + radius2 - radius1).Abs() <= Epsilon)
                {
                    result.P1.X = x1 + M.Sign(x2 - x1) * M.Sign(radius1 - radius2) * radius1;
                    result.P1.Y = y1;
                    result.P2   = result.P1;
                }
                return(result);
            }

            if ((radius1 + radius2 - r3).Abs() <= Epsilon)
            {
                r3          = radius1 / r3;
                result.P1.X = x1 + (x2 - x1) * r3;
                result.P1.Y = y1 + (y2 - y1) * r3;
                result.P2   = result.P1;
                return(result);
            }

            if ((radius1 + r3 - radius2).Abs() <= Epsilon ||
                (radius2 + r3 - radius1).Abs() <= Epsilon)
            {
                r3          = radius1 / r3 * M.Sign(radius1 - radius2);
                result.P1.X = x1 + (x2 - x1) * r3;
                result.P1.Y = y1 + (y2 - y1) * r3;
                result.P2   = result.P1;
                return(result);
            }

            var k = -(x2 - x1) / (y2 - y1);
            var b = ((radius1 - radius2) * (radius1 + radius2)
                     + (x2 - x1) * (x2 + x1)
                     + (y2 - y1) * (y2 + y1))
                    / (2 * (y2 - y1));
            var ea    = k * k + 1;
            var eb    = 2 * (k * b - x1 - k * y1);
            var ec    = x1.Sqr() + b.Sqr() - 2 * b * y1 + y1.Sqr() - radius1.Sqr();
            var roots = SolveSquareEquation(ea, eb, ec);

            if (roots == null || roots.Count != 2)
            {
                return(result);
            }
            result.P1.X = roots[0];
            result.P1.Y = roots[0] * k + b;
            result.P2.X = roots[1];
            result.P2.Y = roots[1] * k + b;

            if (y2 > y1)
            {
                var t = result.P1;
                result.P1 = result.P2;
                result.P2 = t;
            }

            return(result);
        }
Example #9
0
 public static int Sign(this double num)
 {
     return(M.Sign(num));
 }