1. 程式人生 > >C Vector定義 operator 使用

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

這裡寫圖片描述