Unity2D游戏中Matrix2D的创建

namespace UniSeks
{
    using System;
    using System.IO;
    using System.Runtime.InteropServices;
    using UnityEngine;

    
    public struct Matrix2D 
    {
        public float A;
        public float B;
        public float C;
        public float D;
        public float Tx;
        public float Ty;
        public static readonly Matrix2D Identity;
        public Matrix2D(float tx, float ty, float a = 1f, float b = 0f, float c = 0f, float d = 1f)
        {
            this.Tx = tx;
            this.Ty = ty;
            this.A = a;
            this.B = b;
            this.C = c;
            this.D = d;
        }

        static Matrix2D()
        {
            Identity = new Matrix2D(0f, 0f, 1f, 0f, 0f, 1f);
        }

        public void ConcatRight(ref Matrix2D right)
        {
            float a = this.A;
            float b = this.B;
            float c = this.C;
            float d = this.D;
            float tx = this.Tx;
            float ty = this.Ty;
            this.A = (right.A * a) + (right.C * b);
            this.B = (a * right.B) + (b * right.D);
            this.C = (c * right.A) + (d * right.C);
            this.D = (c * right.B) + (d * right.D);
            this.Tx = ((right.A * tx) + (right.C * ty)) + right.Tx;
            this.Ty = ((right.B * tx) + (right.D * ty)) + right.Ty;
        }
        /// <summary>
        /// 矩阵的右乘
        /// </summary>
        /// <param name="right"></param>
        public void ConcatRight(Matrix2D right)
        {
            float a = this.A;
            float b = this.B;
            float c = this.C;
            float d = this.D;
            float tx = this.Tx;
            float ty = this.Ty;
            this.A = (right.A * a) + (right.C * b);
            this.B = (a * right.B) + (b * right.D);
            this.C  = (c * right.A) + (d * right.C);
            this.D = (c * right.B) + (d * right.D);
            this.Tx = ((right.A * tx) + (right.C * ty)) + right.Tx;
            this.Ty = ((right.B * tx) + (right.D * ty)) + right.Ty;
        }

        public void Invert(ref Matrix2D m)
        {
            float num = 1f / ((m.A * m.D) - (m.C * m.B));
            this.A = num * m.D;
            this.B = -num * m.B;
            this.C = -num * m.C;
            this.D = num * m.A;
            this.Tx = num * ((m.B * m.Ty) - (m.D * m.Tx));
            this.Ty = num * ((m.C * m.Tx) - (m.A * m.Ty));
        }
        /// <summary>
        /// 逆矩阵
        /// </summary>
        public void Invert()
        {
            float a = this.A;
            float b = this.B;
            float c = this.C;
            float d = this.D;
            float tx = this.Tx;
            float ty = this.Ty;
            //1/det
            float num7 = 1f / ((a * d) - (c * b));
            this.A = num7 * d;
            this.B = -num7 * b;
            this.C = -num7 * c;
            this.D = num7 * a;
            this.Tx = num7 * ((b * ty) - (d * tx));
            this.Ty = num7 * ((c * tx) - (a * ty));
        }
        /// <summary>
        /// 初始化矩阵
        /// </summary>
        public void MakeIdentity()
        {
            this.A = 1f;
            this.B = 0f;
            this.C = 0f;
            this.D = 1f;
            this.Tx = 0f;
            this.Ty = 0f;
        }
       /// <summary>
       /// 移动点
       /// </summary>
       /// <param name="p"></param>
       /// <returns></returns>
        public Vector2 TransformPoint(ref Vector2 p)
        {
            return new Vector2(((p.x * this.A) + (p.y * this.B)) + this.Tx, ((p.x * this.C) + (p.y * this.D)) + this.Ty);
        }

        public Vector2 TransformPoint(float x, float y)
        {
            return new Vector2(((x * this.A) + (y * this.B)) + this.Tx, ((x * this.C) + (y * this.D)) + this.Ty);
        }

        public Vector2 Translation
        {
            get
            {
                return new Vector2(this.Tx, this.Ty);
            }
            set
            {
                this.Tx = value.x;
                this.Ty = value.y;
            }
        }
        /// <summary>
        /// 复制
        /// </summary>
        /// <param name="m"></param>
        public void CopyFrom(ref Matrix2D m)
        {
            this.A = m.A;
            this.B = m.B;
            this.C = m.C;
            this.D = m.D;
            this.Tx = m.Tx;
            this.Ty = m.Ty;
        }

        public bool Equals(ref Matrix2D m)
        {
            return (((((this.A == m.A) && (this.B == m.B)) && ((this.C == m.C) && (this.D == m.D))) && (this.Tx == m.Tx)) && (this.Ty == m.Ty));
        }

      
        
        public override bool Equals(object other)
        {
            if (!(other is Matrix2D))
            {
                return false;
            }
            Matrix2D matrixd = (Matrix2D) other;
            return (((((this.A == matrixd.A) && (this.B == matrixd.B)) && ((this.C == matrixd.C) && (this.D == matrixd.D))) && (this.Tx == matrixd.Tx)) && (this.Ty == matrixd.Ty));
        }

        public override int GetHashCode()
        {
            return (((((this.A.GetHashCode() ^ this.B.GetHashCode()) ^ this.C.GetHashCode()) ^ this.D.GetHashCode()) ^ this.Tx.GetHashCode()) ^ this.Ty.GetHashCode());
        }

        public float ScaleX
        {
            get
            {
                float num = FastMath.SqrtRough((this.A * this.A) + (this.C * this.C));
                if (this.A < 0.0)
                {
                    num = -num;
                }
                return num;
            }
        }
        public float ScaleY
        {
            get
            {
                float num = FastMath.SqrtRough((this.B * this.B) + (this.D * this.D));
                if (this.D < 0.0)
                {
                    num = -num;
                }
                return num;
            }
        }
        public float Rotation
        {
            get
            {
                return (Mathf.Atan2(-this.C, this.A) * 57.29578f);
            }
        }
        public void MakeTransform(float rotation, float scaleX, float scaleY)
        {
            float num = FastMath.Sin(rotation);
            float num2 = FastMath.Cos(rotation);
            this.A = scaleX * num2;
            this.B = scaleY * num;
            this.C = -scaleX * num;
            this.D = scaleY * num2;
        }

        public void Read(BinaryReader reader)
        {
            MatrixFields fields = (MatrixFields) reader.ReadByte();
            if (fields != MatrixFields.None)
            {
                if (((byte) (fields & MatrixFields.A)) != 0)
                {
                    this.A = reader.ReadSingle();
                }
                if (((byte) (fields & MatrixFields.B)) != 0)
                {
                    this.B = reader.ReadSingle();
                }
                if (((byte) (fields & MatrixFields.C)) != 0)
                {
                    this.C = reader.ReadSingle();
                }
                if (((byte) (fields & MatrixFields.D)) != 0)
                {
                    this.D = reader.ReadSingle();
                }
                if (((byte) (fields & (MatrixFields.None | MatrixFields.Tx))) != 0)
                {
                    this.Tx = reader.ReadSingle();
                }
                if (((byte) (fields & (MatrixFields.None | MatrixFields.Ty))) != 0)
                {
                    this.Ty = reader.ReadSingle();
                }
            }
        }
        [Flags]
        private enum MatrixFields : byte
        {
            A = 1,
            B = 2,
            C = 4,
            D = 8,
            None = 0,
            Tx = 0x10,
            Ty = 0x20
        }
    }
}



  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值