troubleshooting Question

Coordinate question for Matrix transformation in System.Drawing.Drawing2D

Avatar of HansChr
HansChr asked on
C#.NET ProgrammingVisual C++.NET
8 Comments1 Solution987 ViewsLast Modified:
I have used a lot of time understanding the graphics class in C# because I need to make a program with function plotting.

I have gone through this interesting program about function plotting:

I have understood the basic matrix transformation that makes objects rotate, flip, scale etc. but I still cant figure out one thing:

If you make a graphics transformation from world coordinates to page like this:

int wid = picGraph.ClientSize.Width;
            int hgt = picGraph.ClientSize.Height;
            Bitmap bm = new Bitmap(wid, hgt);
            Graphics gr = Graphics.FromImage(bm))

 RectangleF rect = new RectangleF(xmin, ymin, xmax - xmin, ymax - ymin);
                PointF[] pts = 
                    new PointF(0, hgt),
                    new PointF(wid, hgt),
                    new PointF(0, 0),

gr.Transform = new Matrix(rect, pts);
 gr.DrawLine(graph_pen, xmin, 0, xmax, 0);
gr.DrawLine(graph_pen, 0, ymin, 0, ymax);

Then you get a coordinate system that is stretched vertically and horizontally to the sides of the picturebox:Screenshot
But if you manually enter the values from the transformation matrix then you only get a small coordinate system that is centered correctly but isnt scaled properly:

 How come the transformation matrix's scale values are not higher then? (look at the other attached screen capture.

If you print the matrix elements you get when x-min/max  and y-min/max are -3,3, -3,3 by:
gr.Transform = new Matrix(rect, pts);
Matrix ele = gr.Transform;
 float[] ele = inverse.Elements;
            float el = ele.ElementAt(0);
            float el2 = ele.ElementAt(1);
            float el3 = ele.ElementAt(2);
            float el4 = ele.ElementAt(3);
            float el5 = ele.ElementAt(4);
            float el6 = ele.ElementAt(5);
            textBox1.Text += el.ToString() + " ";
            textBox1.Text += el2.ToString() + " ";
            textBox1.Text += el3.ToString() + " ";
            textBox1.Text += el4.ToString() + " ";
            textBox1.Text += el5.ToString() + " ";
            textBox1.Text += el6.ToString();

Matrix elements are then:

[m11,m12,m21,m22,dx,dy] = [5,96,0,0,-3,58,179,107,5]

If I instead enter the transformation Matrix by "hand" like this and of course make no other transformation before (remove the line:  gr.Transform = new Matrix(rect, pts);):
gr.Transform = new Matrix((float)5.966666, 0, 0, (float)-3.583333, 179,107.5);

I get the much smaller coordinate system if I draw the two lines:

gr.DrawLine(graph_pen, xmin, 0, xmax, 0);
gr.DrawLine(graph_pen, ymin, 0, ymax, 0);

In theory this is correct as far as I understand, because what actually happens is that the coordinate transformation is done by (look at the bottom of the page )

multiplying the original coordinates by the transformation matrix (here just for the x-axis):

One can see that the pixel distance between the two coordinate sets are: 35,76. Then it is obvious why the coordinate system is so small.

My question is then: Why does the transformation matrix have scaling values that are so small and how does it stretch according to the first matrix constructor? This is very confusing. Its like the constructor adds something to stretch the rectangle to the border other than just the transformation matrix but I cant understand the theory and why.

If the scaling was greater in the transformation Matrix for example like this:

This would result in a larger coordinate system closer to the border of the picturebox:

I hope I have made myself clear and apologize if something is confusing.

Thanks a lot.


Robert Schutt
Software Engineer

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 1 Answer and 8 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 8 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros