Text Transformation in GDI+(二)

现在我们改变一下变换顺序,顺序为Translate -> Rotate -> Scale,使用Append

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Drawing;
 6 using System.Windows.Forms;
 7 
 8 namespace MatrixOrderDemo
 9 {
10     class MatrixOrderDemo:Form
11     {
12         static void Main(string[] args)
13         {
14             Application.Run(new MatrixOrderDemo());
15         }
16         public MatrixOrderDemo()
17         {
18             Text = "转换顺序测试";
19         }
20         protected override void OnPaint(PaintEventArgs e)
21         {
22             Graphics g = CreateGraphics();
23             g.Clear(this.BackColor);
24 
25             Rectangle rect =
26                 new Rectangle(20,20,100,100);
27             Brush brush = new SolidBrush(Color.Red);
28 
29             //此为绘制方格网
30             for (int i = 0; i < ClientSize.Width; i +=10)
31             {
32                 for (int j = 0; j < ClientSize.Height; j +=10)
33                 {
34                     g.DrawLine(new Pen(Color.Gray),new Point(i,0),new Point(i,ClientSize.Height));
35                     g.DrawLine(new Pen(Color.Gray),new Point(0,j),new Point(ClientSize.Width,j));
36                 }
37             }
38             //g.DrawLine(new Pen(Color.Green),new PointF(14.14f,0),new PointF(14.14f,28.28f));
39             //g.DrawLine(new Pen(Color.Green),new PointF(0,28.28f),new PointF(14.14f,28.28f));
40 
41             g.FillRectangle(brush,rect);
42 
43             g.TranslateTransform(150.0f, 50.0f, System.Drawing.Drawing2D.MatrixOrder.Append);
44             //g.ScaleTransform(1.5f,0.5f);
45             //g.RotateTransform(45.0f,System.Drawing.Drawing2D.MatrixOrder.Append);
46             
47             g.FillRectangle(brush,rect);
48 
49             //此为验证的矩形
50             Rectangle rect1 =
51                 new Rectangle(20, 20, 100, 100);
52             g.FillRectangle(new SolidBrush(Color.Green), rect1);
53 
54             brush.Dispose();
55             g.Dispose();
56         }
57     }
58 }

 

 

 第43行代码运用了TranslateTransform,从实际运行效果来看,绿色正方形的开状没有发生变化,绿色正方向左上角点的坐标为(170,70);符合20+150=170,20+50=70;

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Drawing;
 6 using System.Windows.Forms;
 7 
 8 namespace MatrixOrderDemo
 9 {
10     class MatrixOrderDemo:Form
11     {
12         static void Main(string[] args)
13         {
14             Application.Run(new MatrixOrderDemo());
15         }
16         public MatrixOrderDemo()
17         {
18             Text = "转换顺序测试";
19         }
20         protected override void OnPaint(PaintEventArgs e)
21         {
22             Graphics g = CreateGraphics();
23             g.Clear(this.BackColor);
24 
25             Rectangle rect =
26                 new Rectangle(20,20,100,100);
27             Brush brush = new SolidBrush(Color.Red);
28 
29             //此为绘制方格网
30             for (int i = 0; i < ClientSize.Width; i +=10)
31             {
32                 for (int j = 0; j < ClientSize.Height; j +=10)
33                 {
34                     g.DrawLine(new Pen(Color.Gray),new Point(i,0),new Point(i,ClientSize.Height));
35                     g.DrawLine(new Pen(Color.Gray),new Point(0,j),new Point(ClientSize.Width,j));
36                 }
37             }
38             //g.DrawLine(new Pen(Color.Green),new PointF(14.14f,0),new PointF(14.14f,28.28f));
39             //g.DrawLine(new Pen(Color.Green),new PointF(0,28.28f),new PointF(14.14f,28.28f));
40 
41             g.FillRectangle(brush,rect);
42 
43             g.TranslateTransform(150.0f, 50.0f, System.Drawing.Drawing2D.MatrixOrder.Append);
44             g.ScaleTransform(1.5f,0.5f);
45             //g.RotateTransform(45.0f,System.Drawing.Drawing2D.MatrixOrder.Append);
46             
47             g.FillRectangle(brush,rect);
48 
49             //此为验证的矩形
50             Rectangle rect1 =
51                 new Rectangle(20, 20, 100, 100);
52             g.FillRectangle(new SolidBrush(Color.Green), rect1);
53 
54             brush.Dispose();
55             g.Dispose();
56         }
57     }
58 }

 

 上述代码启用第44行,使用ScaleTransform进行缩放;从上图可以看出绿色矩形的形状已经由长度100扩大了1.5倍后,变为了150长;高度100缩小了0.5倍后,变为了50高。绿色矩形的左上角坐标为(180,60),可以推断出自定义坐标经过移动(150,50)后,再进行ScaleTransform缩放,则仅仅是对于自定义坐标中的实体进行了缩放或扩大,也就是说世界坐标系没有变化,坐标(20,20),长宽为100的实体绿色正方形在自定义坐标系中横坐标扩大1.5倍,为30(注意:这是自定义坐标系中的坐标),纵坐标缩小0.5倍,为10(这也是自定义坐标系中的坐标),结合第一步中的平移(150,50),则最终绿色矩形呈现出来的左上角坐标是:横轴150+30=180;纵轴50+10=60;

 

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Drawing;
 6 using System.Windows.Forms;
 7 
 8 namespace MatrixOrderDemo
 9 {
10     class MatrixOrderDemo:Form
11     {
12         static void Main(string[] args)
13         {
14             Application.Run(new MatrixOrderDemo());
15         }
16         public MatrixOrderDemo()
17         {
18             Text = "转换顺序测试";
19         }
20         protected override void OnPaint(PaintEventArgs e)
21         {
22             Graphics g = CreateGraphics();
23             g.Clear(this.BackColor);
24 
25             Rectangle rect =
26                 new Rectangle(20,20,100,100);
27             Brush brush = new SolidBrush(Color.Red);
28 
29             //此为绘制方格网
30             for (int i = 0; i < ClientSize.Width; i +=10)
31             {
32                 for (int j = 0; j < ClientSize.Height; j +=10)
33                 {
34                     g.DrawLine(new Pen(Color.Gray),new Point(i,0),new Point(i,ClientSize.Height));
35                     g.DrawLine(new Pen(Color.Gray),new Point(0,j),new Point(ClientSize.Width,j));
36                 }
37             }
38             g.DrawLine(new Pen(Color.Purple), new PointF(84.8528f, 0), new PointF(84.8528f, 169.7056f));
39             g.DrawLine(new Pen(Color.Purple), new PointF(0, 169.7056f), new PointF(84.8528f, 169.7056f));
40 
41             g.FillRectangle(brush,rect);
42 
43             g.TranslateTransform(150.0f, 50.0f, System.Drawing.Drawing2D.MatrixOrder.Append);
44             g.ScaleTransform(1.5f,0.5f);
45             g.RotateTransform(45.0f,System.Drawing.Drawing2D.MatrixOrder.Append);
46             
47             g.FillRectangle(brush,rect);
48 
49             //此为验证的矩形
50             Rectangle rect1 =
51                 new Rectangle(20, 20, 100, 100);
52             g.FillRectangle(new SolidBrush(Color.Green), rect1);
53 
54             brush.Dispose();
55             g.Dispose();
56         }
57     }
58 }

上述代码使用RotateTransform顺时针旋转45度;此旋转的基础仍然是以世界坐标系(0,0)为原点进行旋转;以上一示例中绿色矩形的左上角坐标为(180,60)顺时针旋转45度进行验证,具体计算公式如下:

x1=180*cos45-60*sin45 = 84.8528;

y1=180*sin45+60*cos45=169.7056;

程序运行如下(注:代码中特意增加了两行验证代码,如图中紫色横线和紫色竖线所示):

 

posted @ 2022-07-19 23:00  chenlight  阅读(62)  评论(1)    收藏  举报