Problem Translating Window Coordinates
-
Hi, I have asked this question about a day ago and I was given a very good answer about some concepts I was not aware of-- Region.Translate, and Matrix.Transform. So I've been experimenting with them and I'm able to now draw Rects and Translate and Rotate them but I still haven't solved what I ultimately want to do... ///////////////////// PointF pt_1 = new PointF(0,0) PointF pt_1 = new PointF(100,100) public void DrawLine(Graphics g) { SolidBrush brush = new SolidBrush(Color.SteelBlue); Pen pen = new Pen(brush, 10); g.DrawLine(pen, pt_1.X, pt_1.Y, pt_2.X, pt_2.Y); } private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e) { DrawLine(e.Graphics); } //////////////////////////////////////////// How do I change to my own coordinate system and have it permanently stay that way? That is, I want (0,0) to always be the center of the Form, no matter what event fires. What I have (not the test code above) is a program that can draw vectors on an XY axis. I can do it-- only because I've hard-coded the coordinates for the Graph Origin, which besides being a lousy way to do things, also prevents me from any more progress.... Any (further) help would be greatly appreciated.. thanks
-
Hi, I have asked this question about a day ago and I was given a very good answer about some concepts I was not aware of-- Region.Translate, and Matrix.Transform. So I've been experimenting with them and I'm able to now draw Rects and Translate and Rotate them but I still haven't solved what I ultimately want to do... ///////////////////// PointF pt_1 = new PointF(0,0) PointF pt_1 = new PointF(100,100) public void DrawLine(Graphics g) { SolidBrush brush = new SolidBrush(Color.SteelBlue); Pen pen = new Pen(brush, 10); g.DrawLine(pen, pt_1.X, pt_1.Y, pt_2.X, pt_2.Y); } private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e) { DrawLine(e.Graphics); } //////////////////////////////////////////// How do I change to my own coordinate system and have it permanently stay that way? That is, I want (0,0) to always be the center of the Form, no matter what event fires. What I have (not the test code above) is a program that can draw vectors on an XY axis. I can do it-- only because I've hard-coded the coordinates for the Graph Origin, which besides being a lousy way to do things, also prevents me from any more progress.... Any (further) help would be greatly appreciated.. thanks
Since reading your post two days ago, I've tried implementing this in as many ways as I could think. Unfortunately, I just can't get it to stick. I think that you will have to translate the coordinates for every message/event that you receive. Admittedly, you don't have too much work ahead of you. For messages/events that require painting, you only need to do the
Graphics.TranslateTransform( Width / 2, Height / 2 )
call. For messages/events that have client coordinates associated with them, you'll need to subtractWidth / 2
from thex
andHeight / 2
from they
. Finally, with messages/events that have screen coordintates, you need to callPointToClient(Point)
and then subtract like the last call. I would write three static methods in a helper class with signatures like:Point CenteredPointFromClient( Point point, Form form );
int CenteredPointFromScreen( Point point, Form form );
void CenteredGraphics( Graphics g, Form form );I wish a better solution existed. I don't think that it does because it would mess up the way that the .NET runtime draws child controls. Sorry. "we must lose precision to make significant statements about complex systems." -deKorvin on uncertainty