C# 坐标变换::

坐标变换

所谓坐标变换其实就是坐标系的变换,默认情况下坐标系在窗体或空间的左上角为原点


平移坐标:

Rectangle redRectangle = new Rectangle(new Point(0, 0), new Size(120, 80));//Rectangle是矩形的类
Graphics g = this.CreateGraphics();
g.FillRectangle(Brushes.Red, redRectangle);

g.TranslateTransform(150,120);//平移坐标系,从(0,0)平移至(150,120)

Pen pen = new Pen(Color.Red, 3);
g.DrawRectangle(pen, redRectangle);

g.ResetTransform();//将坐标系重新定位到原始位置


旋转坐标:

Rectangle buleRectangle = new Rectangle(new Point(100, 20), new Size(120, 80));//矩形的位置和大小
Graphics g = this.CreateGraphics();//绘制的位置
g.FillRectangle(Brushes.Blue,buleRectangle);//绘制
g.RotateTransform(45);//将坐标系顺时针旋转45度
g.FillRectangle(Brushes.Blue,buleRectangle);//再次绘制图形
g.Dispose();//销毁对象


伸缩坐标:

Rectangle greenRectangle = new Rectangle(new Point(30,100),new Size(80,80));
Graphics g = CreateGraphics();
g.FillRectangle(Brushes.Green,greenRectangle);
g.ScaleTransform(3f,0.5f);//此处是坐标的伸缩,宽是原来的3倍,高是原来的0.5倍,其中3f、0.5f是单精度浮点型float
g.FillRectangle(Brushes.Green,greenRectangle);



你可能感兴趣的:(C#)