Example #1
0
        public static Matrix3x2 CreateRotation(float radians, Vector2 centerPoint)
        {
            Matrix3x2 result;

            radians = (float)SM.IEEERemainder(radians, SM.PI * 2);

            float c, s;

            const float epsilon = 0.001f * (float)SM.PI / 180f;     // 0.1% of a degree

            if (radians > -epsilon && radians < epsilon)
            {
                // Exact case for zero rotation.
                c = 1;
                s = 0;
            }
            else if (radians > SM.PI / 2 - epsilon && radians < SM.PI / 2 + epsilon)
            {
                // Exact case for 90 degree rotation.
                c = 0;
                s = 1;
            }
            else if (radians < -SM.PI + epsilon || radians > SM.PI - epsilon)
            {
                // Exact case for 180 degree rotation.
                c = -1;
                s = 0;
            }
            else if (radians > -SM.PI / 2 - epsilon && radians < -SM.PI / 2 + epsilon)
            {
                // Exact case for 270 degree rotation.
                c = 0;
                s = -1;
            }
            else
            {
                // Arbitrary rotation.
                c = (float)SM.Cos(radians);
                s = (float)SM.Sin(radians);
            }

            float x = centerPoint.X * (1 - c) + centerPoint.Y * s;
            float y = centerPoint.Y * (1 - c) - centerPoint.X * s;

            // [  c  s ]
            // [ -s  c ]
            // [  x  y ]
            result.M11 = c; result.M12 = s;
            result.M21 = -s; result.M22 = c;
            result.M31 = x; result.M32 = y;

            return(result);
        }
Example #2
0
 private static int Math_Fmod(ILuaState lua)
 {
     lua.PushNumber(Math.IEEERemainder(lua.L_CheckNumber(1),
                                       lua.L_CheckNumber(2)));
     return(1);
 }