We help IT Professionals succeed at work.
Get Started

Coordinate question for Matrix transformation in System.Drawing.Drawing2D

986 Views
Last Modified: 2012-07-12
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:
http://blog.csharphelper.com/2012/01/23/graph-an-equation-y--fx-in-c.aspx

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);
            {

Open in new window


http://msdn.microsoft.com/en-us/library/749kbe3w.aspx

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:
Screenshot

 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();

Open in new window


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);

Open in new window


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);

Open in new window


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 http://msdn.microsoft.com/en-us/library/w8s1ct0z) )

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


Screenshot
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:

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

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

Thanks a lot.

Regards

Hans
Comment
Watch Question
Software Engineer
CERTIFIED EXPERT
Commented:
This problem has been solved!
Unlock 1 Answer and 8 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE