向量(unity3d)

向量

Vector3
namespace 向量
{
    struct Vector3
    {
        public float x { get; set; }

        public float y { get; set; }

        public float z{ get; set; }


        public Vector3(float x, float y, float z)
        {
            this.x = x;
            this.y = y;
            this.z = z;
        }

        //向前
        public static Vector3 forward
        {
            get
            {
                return new Vector3(0, 0, 1);
            }
        }

        //向后
        public static Vector3 back
        {
            get
            {
                return new Vector3(0, 0, -1);
            }
        }



        public static Vector3 up
        {
            get
            {
                return new Vector3(0, 1, 0);
            }
        }


        public static Vector3 down
        {
            get
            {
                return new Vector3(0, -1, 0);
            }
        }

        public static Vector3 left
        {
            get
            {
                return new Vector3(-1, 0, 0);
            }
        }

        public static Vector3 right
        {
            get
            {
                return new Vector3(1, 0, 0);
            }
        }


        public static Vector3 zero
        {
            get
            {
                return new Vector3(0, 0, 0);
            }
        }

        public static Vector3 one
        {
            get
            {
                return new Vector3(1, 1, 1);
            }
        }


        //向量的模长
        public float magnitude
        {
            get
            {
                return (float)Math.Sqrt(x * x + y * y + z * z);
            }
        }

        //单位向量
        public Vector3 normalized
        {
          
            get
            {
                if (x == 0 && y == 0 && z == 0)
                {
                    throw new Exception("零向量无法获取单位向量");
                }

                return new Vector3(x / magnitude, y / magnitude, z / magnitude);

            }
        }

        //加法
        public static Vector3 operator +(Vector3 v1, Vector3 v2)
        {
            return new Vector3(v1.x + v2.x, v1.y + v2.y, v1.z + v2.z);
        }

        //减法
        public static Vector3 operator -(Vector3 v)
        {
            return new Vector3(-v.x, -v.y, -v.z);
        }

        public static Vector3 operator -(Vector3 v1, Vector3 v2)
        {
            return v1 + (-v2);
        }


        //数乘
        public static Vector3 operator *(float a, Vector3 v)
        {
            return new Vector3(v.x * a, v.y * a, v.z * a);
        }


        public static Vector3 operator *(Vector3 v, float a)
        {
            return new Vector3(v.x * a, v.y * a, v.z * a);
        }


        //点乘
        // public static float Dot()


        //点乘
        public static float Dot(Vector3 v1, Vector3 v2)
        {
            return v1.x * v2.x  + v1.y * v2.y  + v1.z * v2.z;
        }

        //向量夹角
        public static float Angle(Vector3 v1, Vector3 v2)
        {
            //retur float  alpha = Dot(v1, V2/ (v1.magnitude * v2.magnitude
            float alpha = Dot(v1.normalized, v2.normalized);

            float angle = (float)Math.Acos(alpha);

            return (float)(angle * 180 / Math.PI);
        }

        //投影向量
        public static Vector3 Project(Vector3 v1, Vector3 v2)
        {
            return v2.normalized * (Dot(v1, v2) / v2.magnitude);
        }


        //叉乘
        public static Vector3 Cross(Vector3 v1, Vector3 v2)
        {
            return new Vector3(v1.y * v2.z - v1.z * v2.y, v1.z * v2.x - v1.z * v2.y, v1.x * v2.y - v1.y * v2.x);
        }

    }
}

Vector2
namespace 向量
{
    struct Vector2
    {
        public float x { get; set; }

        public float y { get; set; }


        public Vector2(float x, float y)
        {
            this.x = x;
            this.y = y;
        }

        public static Vector2 up
        {
            get
            {
                return new Vector2(0, 1);
            }
        }


        public static Vector2 down
        {
            get
            {
                return new Vector2(0, -1);
            }
        }

        public static Vector2 left
        {
            get
            {
                return new Vector2(-1, 0);
            }
        }

        public static Vector2 right
        {
            get
            {
                return new Vector2(1, 0);
            }
        }


        public static Vector2 zero
        {
            get
            {
                return new Vector2(0, 0);
            }
        }

        public static Vector2 one
        {
            get
            {
                return new Vector2(1, 1);
            }
        }


        //向量的模长
        public float magnitude
        {
            get
            {
                return (float)Math.Sqrt(x * x + y * y);
            }
        }

        //单位向量
        public Vector2 normalized
        {
            get {
                if(x == 0 && y == 0)
                {
                    throw new Exception("零向量无法获取单位向量");
                }
                return new  Vector2(x / magnitude, y / magnitude);
            } 
        }

        //加法
        public static Vector2 operator  +(Vector2 v1, Vector2 v2)
        {
            return new Vector2(v1.x + v2.x, v1.y + v2.y);
        }

        //减法
        public static Vector2 operator -(Vector2 v)
        {
            return new Vector2(-v.x, -v.y);
        }

        public static Vector2 operator -(Vector2 v1, Vector2 v2)
        {
            return  v1 + (-v2);
        }


        //数乘
        public static Vector2 operator *(float a, Vector2 v)
        {
            return new Vector2(v.x * a,  v.y * a);
        }


        public static Vector2 operator *(Vector2 v, float a)
        {
            return new Vector2(v.x * a, v.y * a);
        }


        //点乘
        public static float Dot(Vector2 V1, Vector2 V2)
        {
            return V1.x * V2.x + V1.y * V2.y;
        }

        //向量夹角
        public static float Angle(Vector2 v1, Vector2 v2)
        {
            //retur float  alpha = Dot(v1, V2/ (v1.magnitude * v2.magnitude
            float alpha = Dot(v1.normalized, v2.normalized); 

            float angle  = (float)Math.Acos(alpha);

            return (float)(angle * 180 / Math.PI);
        }

        //投影向量
        public static Vector2 Project(Vector2 v1, Vector2 v2)
        {
            return v2.normalized * (Dot(v1, v2) / v2.magnitude);
        }


        //两个向量之间的距离
        public static float Distance(Vector2 v1, Vector2 v2)
        {
            return (v1 - v2).magnitude;
        }

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值