類比GDI+中的座標系轉換

來源:互聯網
上載者:User

附:轉換類原始碼

using System;using System.Collections.Generic;using System.Drawing;using System.Linq;using System.Text;using System.Threading.Tasks;namespace TestDraw{    class MyTransform    {        private MyMatrix M = new MyMatrix();    //當前的轉換矩陣        //平移        public void Translate(float dx, float dy)        {            float[,] K = {                {1,0,dx},                {0,1,dy},                {0,0,1}            };            this.M = this.M.IR(new MyMatrix(K));        }        //縮放        public void Scale(float sx, float sy)        {            float[,] K = {                             {sx, 0, 0},                             {0, sy, 0},                             {0, 0, 1}                         };            this.M = this.M.IR(new MyMatrix(K));        }        //旋轉        public void Rotate(float sita)        {            float[,] K = {                             {(float)Math.Cos(sita), -(float)Math.Sin(sita), 0},                             {(float)Math.Sin(sita), (float)Math.Cos(sita), 0},                             {0,0,1}                         };            this.M = this.M.IR(new MyMatrix(K));        }        //重設轉換矩陣        public void Rest()        {            this.M = new MyMatrix();        }                //計算新座標        public PointF CalculatePoint(PointF jmP)        {            MyPoint newP = this.M.IR(new MyPoint(jmP.X, jmP.Y));            return new PointF(newP.P[0], newP.P[1]);        }        private MyPoint CalculatePoint(MyPoint jmP)        {            return this.M.IR(jmP);        }      }    //點矩陣    class MyPoint    {        public Single[] P = {0,0,1};        public MyPoint(float x, float y)        {            P[0] = x;            P[1] = y;        }            }    //轉換矩陣    class MyMatrix    {        public Single[,] K = {                            {1,0,0},                            {0,1,0},                            {0,0,1}                        };        public MyMatrix()        {        }        public MyMatrix(Single[,] K)        {            for (int i = 0; i < 3; i++)            {                for (int j = 0; j < 3; j++)                {                    this.K[i,j] = K[i,j];                }            }        }        //本矩陣 x R        public MyMatrix IR(MyMatrix R)        {            Single[,] result = new float[3, 3];            for (int i = 0; i < 3; i++)            {                for (int j = 0; j < 3; j++)                {                    result[i, j] = this.K[i, 0] * R.K[0, j] + this.K[i, 1] * R.K[1, j] + this.K[i, 2] * R.K[2, j];                }            }            return new MyMatrix(result);        }        //本矩陣 x P        public MyPoint IR(MyPoint P)        {            Single[] result = new float[3];            for (int i = 0; i < 3; i++)            {                result[i] = this.K[i, 0] * P.P[0] + this.K[i, 1] * P.P[1] + this.K[i, 2] * P.P[2];            }            return new MyPoint(result[0], result[1]);        }        //L x 本矩陣        public MyMatrix LI(MyMatrix L)        {            return L.IR(this);        }    }}

測試原始碼:

using System.Drawing;using System.Windows.Forms;namespace TestDraw{    public partial class Form1 : Form    {        public Form1()        {            InitializeComponent();        }        private MyTransform transform = new MyTransform();        private void pictureBox1_Paint(object sender, PaintEventArgs e)        {            Graphics g = e.Graphics;            //建模座標系中四個點            PointF p1 = new PointF(0, 0);            PointF p2 = new PointF(5, 0);            PointF p3 = new PointF(5, 2);            PointF p4 = new PointF(0, 2);            ////繪圖座標系向建模座標系轉變            //transform.Translate(100, 100);            //transform.Scale(1, -1);            //transform.Scale(10, 10);            //transform.Rotate((float)(3.14 / 6.0));            ////計算點在繪圖座標系中的座標            //PointF np1 = transform.CalculatePoint(p1);            //PointF np2 = transform.CalculatePoint(p2);            //PointF np3 = transform.CalculatePoint(p3);            //PointF np4 = transform.CalculatePoint(p4);            ////繪製矩形            //g.DrawLines(Pens.Red, new PointF[]{np1, np2, np3, np4, np1});            //transform.Rest();            g.TranslateTransform(100, 100);            g.ScaleTransform(1, -1);            g.ScaleTransform(10, 10);            g.RotateTransform(30);            g.DrawLines(new Pen(Color.Red, 1/10), new PointF[] { p1, p2, p3, p4, p1 });            g.ResetTransform();        }    }}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.