# 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?
LVL 48
###### Who is Participating?

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

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

Author Commented:
Thanks, but I want to make point transformation by generic way using the same Graphics functions which are used for painting.
0

Commented:
you want a matrix to use as transform in a graphics object?
0

Author 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);
g.Dispose();
pictureBox1.Invalidate();
}
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.