C Vector定义 operator 使用

分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow

也欢迎大家转载本篇文章。分享知识,造福人民,实现我们中华民族伟大复兴!

               
In   Java,   ArrayList   is   the   same   as   Vector   except   that   it 's   not   thread   safe. 
In   C#:   
      ArrayList   al   =   new   ArrayList();   
      ArrayList   vector   =   ArrayList.Synchronized(al);   //this   is   a   thread   safe   arraylist,   the   same   as   Vector   in   Java 

/// <summary>
    /// 向量类
    /// </summary>
    public class Vector
    {
        public double x, y, z;


        public Vector()
        {

        }


        public Vector(double _x, double _y, double _z)
        {
            x = _x;
            y = _y;
            z = _z;
        }


        /// <summary>
        /// 求向量长度
        /// </summary>
        /// <returns></returns>
        public double magnitude()
        {
            return Math.Sqrt(x * x+ y * y+z * z);
        }


        /// <summary>
        /// 求单位向量
        /// </summary>
        /// <returns></returns>
        public Vector normal()
        {
            double k = 1 / magnitude();
            Vector d = new Vector(k * x, k * y, k * z);
            return d;         
        }
        
        /// <summary>
        /// 向量和
        /// </summary>
        /// <param name="v1">向量1</param>
        /// <param name="v2">向量2</param>
        /// <returns>新向量</returns>
        public static Vector operator +(Vector v1, Vector v2)
        {
            Vector d = new Vector(v1.x + v2.x, v1.y + v2.y, v1.z + v2.z);
            return d;
           
        }
        /// <summary>
        /// 求反向量
        /// </summary>
        /// <param name="v">向量</param>
        /// <returns>新向量</returns>
        public static Vector operator -(Vector v)
        {
            Vector d = new Vector(-v.x, -v.y, -v.z);
            return d;
           
        }
        /// <summary>
        /// 求向量
        /// </summary>
        /// <param name="v1"></param>
        /// <param name="v2"></param>
        /// <returns></returns>
        public static Vector operator -(Vector v1, Vector v2)
        {
            Vector d = new Vector(v1.x - v2.x, v1.y - v2.y, v1.z - v2.z);
            return d;            
        }


        public static Vector operator *(Vector v, double k)
        {
            Vector d = new Vector(k * v.x, k * v.y, k * v.z);
            return d;
            
        }
        public static Vector operator *(double k, Vector v)
        {
            Vector d = new Vector(k * v.x, k * v.y, k * v.z);
            return d;
            
        }
        public static Vector operator /(Vector v, double k)
        {
            Vector d=new Vector(v.x / k, v.y / k, v.z / k) ;
            d.x =v.x / k;
            d.y =v.y / k;
            d.z =v.z / k;
            return d;          
        }


        public static double operator ^(Vector v1, Vector v2)
        {
            return v2.x + v2.x + v1.y + v2.y + v1.z + v2.z;
        }
        public static Vector operator *(Vector v1, Vector v2)
        {
            Vector d = new Vector(v1.y * v2.z - v2.z * v2.y,
                v1.z * v2.x - v1.x * v2.z,
                v1.x * v2.y - v1.y * v2.x);
            return d;           
        }              
    }

           

给我老师的人工智能教程打call!http://blog.csdn.net/jiangjunshow
这里写图片描述
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值