• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 551
  • Last Modified:

Coordinate transformations

I want to draw some object (for example, line) in the form client area using standard (non-Windows) coordinate system. (0,0) point should be in the left-bottom corner of the window, X axis goes right, Y axis goes up. How can I transform coordinate system inside of Paint event handler for Graphics object?
Having point coordinates in some other function (for example, MouseDown event handler), how can I convert this point to the same coordinate system which is used for drawing?
0
AlexFM
Asked:
AlexFM
  • 3
  • 2
1 Solution
 
AgariciCommented:
the transform should be simple ...
Xt = WindowCoord.X
Yt = Window.Height - WindowCoord.Y

and if you want to find the Window coords for your custom coordinates system point (Xt,Yt):
WindowCoord.X = Xt
WindowCoord.Y = Window.Height - Yt

hth,
A.
0
 
AlexFMAuthor Commented:
Thanks, but I want to make point transformation by generic way using the same Graphics functions which are used for painting.
0
 
AgariciCommented:
you want a matrix to use as transform in a graphics object?
0
 
AgariciCommented:
if that is the case,
you should use this matrix
[  1         0                0
   0        -1                0  
   0  Window.Height   1 ]

that is: reverse Y axis and translate with Window.Height on Y.

in code this should be something like this:

//in paint
Matrix mx=new Matrix(1,0,0,-1,0, Window.Height);
YourGraphics.Transform = mx;

//on mouse event
Matrix mx=new Matrix(1,0,0,-1,0, Window.Height);
Point[] SomePoints=new Point[]{curMousePos};
mx.TransformPoints(SomePoints);
curMousePos = SomePoints[0];


hth,
A.
0
 
AlexFMAuthor Commented:
Thanks.
My code for future reference:

       ArrayList points = new ArrayList();

        private void TransformGraphics(Graphics g)
        {
            g.Transform = new Matrix(1, 0, 0, -1, 0, pictureBox1.Height);
        }

        private void pictureBox1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
        {
            TransformGraphics(e.Graphics);

            foreach (Point p in points)
            {
                e.Graphics.DrawEllipse(pen, p.X, p.Y, 3, 3);
            }
        }

        private void pictureBox1_Click(object sender, System.EventArgs e)
        {
            Point[] p = new Point[]{pictureBox1.PointToClient(Control.MousePosition)};
            Graphics g = pictureBox1.CreateGraphics();
            TransformGraphics(g);
            g.TransformPoints(CoordinateSpace.Device, CoordinateSpace.World, p);
            points.Add(p[0]);
            g.Dispose();
            pictureBox1.Invalidate();
        }
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now