• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            天行健 君子當自強而不息

            一個3D向量類

            新建網頁 1 提供以下基本操作:

            1.   存取向量的各分量(x, y , z)
            2.   向量間的賦值操作
            3.   比較兩向量是否相同
            4.   將向量置為零向量
            5.   向量求負
            6.   求向量的模
            7.   向量與標量的乘除法
            8.   向量標準化
            9.   向量加減法
            10. 計算兩點(點用向量表示)間距離
            11. 向量點乘
            12. 向量叉乘

            該向量的操作運算對3D點同樣適合。
                #include <math.h>
                
                
            class cVector3
                {
                
            public:
                    
            float x, y, z;
                
                
            public:
                    cVector3() { }
                
                    cVector3(
            const cVector3& v)
                    {    
                        x = v.x;    y = v.y;    z = v.z;    
                    }
                
                    cVector3(
            float vx, float vy, float vz)
                    {
                        x = vx;        y = vy;        z = vz;
                    }
                
                    cVector3& 
            operator=(const cVector3& v)
                    {
                        x = v.x;    y = v.y;    z = v.z;
                
                        
            return *this;
                    }
                
                    
            bool operator==(const cVector3& v)
                    {
                        
            return (x == v.x && y == v.y && z == v.z);
                    }
                
                    
            bool operator!=(const cVector3& v)
                    {
                        
            return (x != v.x || y != v.y || z != v.z);
                    }
                
                    
            void zero()
                    {
                        
            // set the vector to zero
                
                    x = y = z = 0.0f;
                    }
                
                    cVector3 
            operator-()
                    {
                        
            // unary minus returns the negative of the vector
                
                    return cVector3(-x, -y, -z);
                    }
                
                    cVector3 
            operator+(const cVector3& v)
                    {
                        
            return cVector3(x + v.x, y + v.y, z + v.z);
                    }
                
                    cVector3 
            operator-(const cVector3& v)
                    {
                        
            return cVector3(x - v.x, y - v.y, z - v.z);
                    }
                
                    cVector3 
            operator*(float scale)
                    {
                        
            // multiplication and division by scalar
                
                    return cVector3(x * scale, y * scale, z * scale);
                    }
                
                    cVector3 
            operator/(float scale)
                    {
                        
            float temp = 1.0f / scale;    // NOTE: no check for divide by zero here
                

                        
            return cVector3(x * temp, y * temp, z * temp);
                    }
                
                    cVector3& 
            operator+=(const cVector3& v)
                    {
                        x += v.x;    y += v.y;    z += v.z;
                
                        
            return *this;
                    }
                
                    cVector3& 
            operator-=(const cVector3& v)
                    {
                        x -= v.x;    y -= v.y;    z -= v.z;
                
                        
            return *this;
                    }
                
                    cVector3& 
            operator*=(float scale)
                    {
                        x *= scale;    y *= scale;    z *= scale;
                
                        
            return *this;
                    }
                
                    cVector3& 
            operator/=(float scale)
                    {
                        
            float temp = 1.0f / scale;
                
                        x *= temp;    y *= temp;    z *= temp;
                
                        
            return *this;
                    }
                
                    
            void normalize()
                    {
                        
            // normalize the vector
                

                        
            float mag = x * x + y * y + z * z;
                
                        
            if(mag > 0.0f)    // check for divide-by-zero
                
                    {
                            
            float one_over_mag = 1.0f / sqrt(mag);
                
                            x *= one_over_mag;
                            y *= one_over_mag;
                            z *= one_over_mag;
                        }
                    }
                
                    
            float operator*(const cVector3& v)
                    {
                        
            // vector dot product, we overload the standard multiplication symbol to do this.
                

                        
            return (x * v.x + y * v.y + z * v.z);
                    }
                };
                
                
                /***************************************************************************************************/
                
                inline 
            float vector_mag(const cVector3& v)
                {
                    
            // compute the magnitude of a vector
                
                return sqrt(v.x * v.x + v.y * v.y + v.z * v.z);
                }
                
                inline cVector3 cross_product(
            const cVector3& a, const cVector3& b)
                {
                    
            // compute the cross product of two vectors
                
                return cVector3(a.y * b.z - a.z * b.y, a.z * b.x - a.x * b.z, a.x * b.y - a.y * b.x);
                }
                
                inline cVector3 
            operator*(float k, const cVector3& v)
                {
                    
            // scalar on the left multiplication, for symmetry.
                
                return cVector3(k * v.x, k * v.y, k * v.z);
                }
                
                inline 
            float distance(const cVector3& a, const cVector3& b)
                {
                    
            // compute the distance between two points
                

                    
            float dx = a.x - b.x;
                    
            float dy = a.y - b.y;
                    
            float dz = a.z - b.z;
                
                    
            return sqrt(dx * dx + dy * dy + dz * dz);
                }
                
                inline 
            float distance_squared(const cVector3& a, const cVector3& b)
                {
                    
            // compute the distance between two points
                

                    
            float dx = a.x - b.x;
                    
            float dy = a.y - b.y;
                    
            float dz = a.z - b.z;
                
                    
            return (dx * dx + dy * dy + dz * dz);
                }
                
                
            extern const cVector3 g_zero_vector;

            posted on 2008-01-08 21:58 lovedday 閱讀(1946) 評論(0)  編輯 收藏 引用 所屬分類: ■ 3D Math Basis

            公告

            導航

            統計

            常用鏈接

            隨筆分類(178)

            3D游戲編程相關鏈接

            搜索

            最新評論

            亚洲国产精品人久久| 国产精品福利一区二区久久| 久久ZYZ资源站无码中文动漫| 99久久精品费精品国产一区二区| 国产精品99精品久久免费| 久久精品国产99国产电影网 | 久久ww精品w免费人成| 久久性精品| 无码伊人66久久大杳蕉网站谷歌| 久久午夜无码鲁丝片| 中文字幕久久精品| 精品无码久久久久久久动漫 | 国产亚洲综合久久系列| 久久国产香蕉一区精品| 国产精品久久久久久福利69堂| 久久热这里只有精品在线观看| 亚洲狠狠久久综合一区77777| 无码8090精品久久一区| 久久综合九色综合久99| 久久国产综合精品五月天| 999久久久免费国产精品播放| 久久久久国产一级毛片高清版| 2021久久精品国产99国产精品| 欧美午夜A∨大片久久| 日韩久久久久中文字幕人妻| 色青青草原桃花久久综合| 无码国内精品久久人妻麻豆按摩| 久久久91人妻无码精品蜜桃HD| 久久er国产精品免费观看8| 中文字幕精品久久| 久久被窝电影亚洲爽爽爽| 青青草原综合久久大伊人导航| 青青青青久久精品国产h久久精品五福影院1421 | 久久精品国产男包| 久久国产成人| 国产精品美女久久久久网| 久久精品国产亚洲av日韩| 亚洲国产成人精品91久久久| 久久99国产精品久久| 日韩乱码人妻无码中文字幕久久 | 久久天天躁狠狠躁夜夜躁2014|