Solved

# Coordinate transformations

Posted on 2005-04-19
544 Views
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
Question by:AlexFM

LVL 11

Expert Comment

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

LVL 48

Author Comment

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

LVL 11

Expert Comment

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

LVL 11

Accepted Solution

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

LVL 48

Author Comment

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

## Featured Post

### Suggested Solutions

In one of my recent projects, I was working with IP cameras, I need to take some pictures from the camera and do some processing on it. The first step, was to retrieve the image from camera into Image object. So that it can be displayed or …
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Hi everyone! This is Experts Exchange customer support.  This quick video will show you how to change your primary email address.  If you have any questions, then please Write a Comment below!