Пример #1
0
        public static GsMatrix operator /(GsMatrix matrix1, GsMatrix matrix2)
        {
            GsMatrix inverse = GsMatrix.Invert(matrix2), result;

            result.M11 = matrix1.M11 * inverse.M11 + matrix1.M12 * inverse.M21 + matrix1.M13 * inverse.M31 + matrix1.M14 * inverse.M41;
            result.M12 = matrix1.M11 * inverse.M12 + matrix1.M12 * inverse.M22 + matrix1.M13 * inverse.M32 + matrix1.M14 * inverse.M42;
            result.M13 = matrix1.M11 * inverse.M13 + matrix1.M12 * inverse.M23 + matrix1.M13 * inverse.M33 + matrix1.M14 * inverse.M43;
            result.M14 = matrix1.M11 * inverse.M14 + matrix1.M12 * inverse.M24 + matrix1.M13 * inverse.M34 + matrix1.M14 * inverse.M44;

            result.M21 = matrix1.M21 * inverse.M11 + matrix1.M22 * inverse.M21 + matrix1.M23 * inverse.M31 + matrix1.M24 * inverse.M41;
            result.M22 = matrix1.M21 * inverse.M12 + matrix1.M22 * inverse.M22 + matrix1.M23 * inverse.M32 + matrix1.M24 * inverse.M42;
            result.M23 = matrix1.M21 * inverse.M13 + matrix1.M22 * inverse.M23 + matrix1.M23 * inverse.M33 + matrix1.M24 * inverse.M43;
            result.M24 = matrix1.M21 * inverse.M14 + matrix1.M22 * inverse.M24 + matrix1.M23 * inverse.M34 + matrix1.M24 * inverse.M44;

            result.M31 = matrix1.M31 * inverse.M11 + matrix1.M32 * inverse.M21 + matrix1.M33 * inverse.M31 + matrix1.M34 * inverse.M41;
            result.M32 = matrix1.M31 * inverse.M12 + matrix1.M32 * inverse.M22 + matrix1.M33 * inverse.M32 + matrix1.M34 * inverse.M42;
            result.M33 = matrix1.M31 * inverse.M13 + matrix1.M32 * inverse.M23 + matrix1.M33 * inverse.M33 + matrix1.M34 * inverse.M43;
            result.M34 = matrix1.M31 * inverse.M14 + matrix1.M32 * inverse.M24 + matrix1.M33 * inverse.M34 + matrix1.M34 * inverse.M44;

            result.M41 = matrix1.M41 * inverse.M11 + matrix1.M42 * inverse.M21 + matrix1.M43 * inverse.M31 + matrix1.M44 * inverse.M41;
            result.M42 = matrix1.M41 * inverse.M12 + matrix1.M42 * inverse.M22 + matrix1.M43 * inverse.M32 + matrix1.M44 * inverse.M42;
            result.M43 = matrix1.M41 * inverse.M13 + matrix1.M42 * inverse.M23 + matrix1.M43 * inverse.M33 + matrix1.M44 * inverse.M43;
            result.M44 = matrix1.M41 * inverse.M14 + matrix1.M42 * inverse.M24 + matrix1.M43 * inverse.M34 + matrix1.M44 * inverse.M44;

            return(result);
        }
Пример #2
0
        public static GsMatrix CreateTranslation(float xPosition, float yPosition, float zPosition)
        {
            GsMatrix returnMatrix = GsMatrix.Identity;

            returnMatrix.M41 = xPosition;
            returnMatrix.M42 = yPosition;
            returnMatrix.M43 = zPosition;

            return(returnMatrix);
        }
Пример #3
0
        public static GsMatrix CreateScale(float xScale, float yScale, float zScale)
        {
            GsMatrix returnMatrix = GsMatrix.Identity;

            returnMatrix.M11 = xScale;
            returnMatrix.M22 = yScale;
            returnMatrix.M33 = zScale;

            return(returnMatrix);
        }
Пример #4
0
        public static GsMatrix CreateScale(float scale)
        {
            GsMatrix returnMatrix = GsMatrix.Identity;

            returnMatrix.M11 = scale;
            returnMatrix.M22 = scale;
            returnMatrix.M33 = scale;

            return(returnMatrix);
        }
Пример #5
0
        public static GsMatrix CreateRotationY(float radians)
        {
            GsMatrix returnMatrix = GsMatrix.Identity;

            returnMatrix.M11 = (float)Math.Cos(radians);
            returnMatrix.M13 = (float)Math.Sin(radians);
            returnMatrix.M31 = -returnMatrix.M13;
            returnMatrix.M33 = returnMatrix.M11;

            return(returnMatrix);
        }
Пример #6
0
        public static GsMatrix CreateRotationX(float radians)
        {
            GsMatrix returnMatrix = GsMatrix.Identity;

            returnMatrix.M22 = (float)Math.Cos(radians);
            returnMatrix.M23 = (float)Math.Sin(radians);
            returnMatrix.M32 = -returnMatrix.M23;
            returnMatrix.M33 = returnMatrix.M22;

            return(returnMatrix);
        }
Пример #7
0
        public static GsMatrix CreateRotationZ(float radians)
        {
            GsMatrix returnMatrix = GsMatrix.Identity;

            returnMatrix.M11 = (float)Math.Cos(radians);
            returnMatrix.M12 = (float)Math.Sin(radians);
            returnMatrix.M21 = -returnMatrix.M12;
            returnMatrix.M22 = returnMatrix.M11;

            return(returnMatrix);
        }
Пример #8
0
        public static void Invert(ref GsMatrix matrix, out GsMatrix result)
        {
            //
            // Use Laplace expansion theorem to calculate the inverse of a 4x4 matrix
            //
            // 1. Calculate the 2x2 determinants needed and the 4x4 determinant based on the 2x2 determinants
            // 2. Create the adjugate matrix, which satisfies: A * adj(A) = det(A) * I
            // 3. Divide adjugate matrix with the determinant to find the inverse

            float det1  = matrix.M11 * matrix.M22 - matrix.M12 * matrix.M21;
            float det2  = matrix.M11 * matrix.M23 - matrix.M13 * matrix.M21;
            float det3  = matrix.M11 * matrix.M24 - matrix.M14 * matrix.M21;
            float det4  = matrix.M12 * matrix.M23 - matrix.M13 * matrix.M22;
            float det5  = matrix.M12 * matrix.M24 - matrix.M14 * matrix.M22;
            float det6  = matrix.M13 * matrix.M24 - matrix.M14 * matrix.M23;
            float det7  = matrix.M31 * matrix.M42 - matrix.M32 * matrix.M41;
            float det8  = matrix.M31 * matrix.M43 - matrix.M33 * matrix.M41;
            float det9  = matrix.M31 * matrix.M44 - matrix.M34 * matrix.M41;
            float det10 = matrix.M32 * matrix.M43 - matrix.M33 * matrix.M42;
            float det11 = matrix.M32 * matrix.M44 - matrix.M34 * matrix.M42;
            float det12 = matrix.M33 * matrix.M44 - matrix.M34 * matrix.M43;

            float detMatrix = (float)(det1 * det12 - det2 * det11 + det3 * det10 + det4 * det9 - det5 * det8 + det6 * det7);

            float invDetMatrix = 1f / detMatrix;

            GsMatrix ret; // Allow for matrix and result to point to the same structure

            ret.M11 = (matrix.M22 * det12 - matrix.M23 * det11 + matrix.M24 * det10) * invDetMatrix;
            ret.M12 = (-matrix.M12 * det12 + matrix.M13 * det11 - matrix.M14 * det10) * invDetMatrix;
            ret.M13 = (matrix.M42 * det6 - matrix.M43 * det5 + matrix.M44 * det4) * invDetMatrix;
            ret.M14 = (-matrix.M32 * det6 + matrix.M33 * det5 - matrix.M34 * det4) * invDetMatrix;
            ret.M21 = (-matrix.M21 * det12 + matrix.M23 * det9 - matrix.M24 * det8) * invDetMatrix;
            ret.M22 = (matrix.M11 * det12 - matrix.M13 * det9 + matrix.M14 * det8) * invDetMatrix;
            ret.M23 = (-matrix.M41 * det6 + matrix.M43 * det3 - matrix.M44 * det2) * invDetMatrix;
            ret.M24 = (matrix.M31 * det6 - matrix.M33 * det3 + matrix.M34 * det2) * invDetMatrix;
            ret.M31 = (matrix.M21 * det11 - matrix.M22 * det9 + matrix.M24 * det7) * invDetMatrix;
            ret.M32 = (-matrix.M11 * det11 + matrix.M12 * det9 - matrix.M14 * det7) * invDetMatrix;
            ret.M33 = (matrix.M41 * det5 - matrix.M42 * det3 + matrix.M44 * det1) * invDetMatrix;
            ret.M34 = (-matrix.M31 * det5 + matrix.M32 * det3 - matrix.M34 * det1) * invDetMatrix;
            ret.M41 = (-matrix.M21 * det10 + matrix.M22 * det8 - matrix.M23 * det7) * invDetMatrix;
            ret.M42 = (matrix.M11 * det10 - matrix.M12 * det8 + matrix.M13 * det7) * invDetMatrix;
            ret.M43 = (-matrix.M41 * det4 + matrix.M42 * det2 - matrix.M43 * det1) * invDetMatrix;
            ret.M44 = (matrix.M31 * det4 - matrix.M32 * det2 + matrix.M33 * det1) * invDetMatrix;

            result = ret;
        }
Пример #9
0
        private GsVector[] GetPoints(GsVector[] hull, GsMatrix transform)
        {
            mMinX = float.MaxValue;
            mMaxX = float.MinValue;
            mMinY = float.MaxValue;
            mMaxY = float.MinValue;

            List <GsVector> points = new List <GsVector>(hull.Length);

            foreach (GsVector pt in hull)
            {
                points.Add(GsVector.Transform(pt, transform));

                float x = points[points.Count - 1].X;
                float y = points[points.Count - 1].Y;

                mMinX = Math.Min(mMinX, x);
                mMinY = Math.Min(mMinY, y);

                mMaxX = Math.Max(mMaxX, x);
                mMaxY = Math.Max(mMaxY, y);
            }
            return(points.ToArray());
        }
Пример #10
0
 /// <summary>
 /// Creates a new polygon from a collection of points (connecting them together) and applies
 /// the transformation on the result.
 /// </summary>
 /// <param name="points">The points to use to construct the polygon.</param>
 /// <param name="transform">The transform to apply to the polygon.</param>
 public GsPolygon(GsVector[] points, GsMatrix transform)
 {
     mPoints = GetPoints(points, transform);
     mEdges  = GetEdges(mPoints);
 }
Пример #11
0
 public static GsMatrix Invert(GsMatrix matrix)
 {
     Invert(ref matrix, out matrix);
     return(matrix);
 }
Пример #12
0
 public static GsVector Transform(GsVector position, GsMatrix matrix)
 {
     return(new GsVector((position.X * matrix.M11) + (position.Y * matrix.M21) + matrix.M41,
                         (position.X * matrix.M12) + (position.Y * matrix.M22) + matrix.M42));
 }