/// <summary>
        /// Build a transformation taking the standard frame (0,0), (1, 0), (0, 1), and (1, 1) to the points
        /// p0, p1, p2, and p3.
        /// </summary>
        /// <param name="p0">Where (0, 0) is sent</param>
        /// <param name="p1">Where (1, 0) is sent</param>
        /// <param name="p2">Where (0, 1) is sent</param>
        /// <param name="p3">Where (1, 1) is sent</param>
        /// <returns>The projective transformation effecting the specified mappings</returns>
        public static ProjectiveTransform2 StandardFrameToPoints(Point p0, Point p1, Point p2, Point p3)
        {
            //
            ProjectiveTransform2 T = new ProjectiveTransform2();
            // idea:
            // Send e1, e2, e3 to p0, p1, p2 by a map K.
            // Let L be Kinverse.
            // Then L sends p0, p1, p2 to e1, e2 and e3 . See where p4 goes; call this q.
            // build projective map P sending e1, e2, e3, and u= (e1+e2+e3) to e1, e2, e3, and q.
            // then let L = Kinverse; K * P sends e1 to p1; e2 to p2; e3 to p3; and u to q to e4.
            ProjectiveTransform2 K = new ProjectiveTransform2();

            for (int i = 0; i < 3; i++)
            {
                K.mat[2, i] = 1.0d;
            }
            K.mat[0, 0] = p0.X;
            K.mat[1, 0] = p0.Y;
            K.mat[0, 1] = p1.X;
            K.mat[1, 1] = p1.Y;
            K.mat[0, 2] = p2.X;
            K.mat[1, 2] = p2.Y;

            ProjectiveTransform2 L = new ProjectiveTransform2();

            L.mat = LinearTransform2.MatrixInverse(K.mat);
            double[] v = new double[3];
            v[0] = p3.X;
            v[1] = p3.Y;
            v[2] = 1.0d;

            double[] q = new double[3];
            for (int i = 0; i < 3; i++)
            {
                double tally = 0.0d;
                for (int j = 0; j < 3; j++)
                {
                    tally += L.mat[i, j] * v[j];
                }
                q[i] = tally;
            }
            double[,] p = new double[3, 3];
            for (int i = 0; i < 3; i++)
            {
                p[i, i] = q[i];
            }
            ProjectiveTransform2 S = new ProjectiveTransform2();

            S.mat = ProjectiveTransform2.MatrixProduct(K.mat, p);
            return(S);
        }
        /// <summary>
        /// Compute the inverse of a 3 x 3 matrix.
        /// <param name="mat">A 3 x 3 matrix </param>
        /// <returns> The inverse of matrix mat1</returns>
        /// </summary>
        new private static double[,] MatrixInverse(double[,] mat)
        {
            double[] translation = new double[3];
            translation[0] = mat[0, 2];
            translation[1] = mat[1, 2];
            translation[2] = 1.0d;

            double[,] mm = new double[3, 3];
            for (int i = 0; i < 2; i++)
            {
                for (int j = 0; j < 2; j++)
                {
                    mm[i, j] = mat[i, j];
                }
            }
            mm[2, 2] = 1.0d;

            double[,] minv = LinearTransform2.MatrixInverse(mm);
            double[] reverseTranslation = new double[3];
            for (int i = 0; i < 2; i++)
            {
                for (int j = 0; j < 2; j++)
                {
                    reverseTranslation[i] -= minv[i, j] * translation[j];
                }
            }
            reverseTranslation[2] = 1.0d;


            double[,] res = new double[3, 3];
            for (int i = 0; i < 2; i++)
            {
                for (int j = 0; j < 2; j++)
                {
                    res[i, j] = minv[i, j];
                }
                res[i, 2] = reverseTranslation[i];
            }
            res[2, 2] = 1.0d;
            return(res);
        }