?? vector3d.h
字號:
/////////////////////////////////////////////////////////////////////////////////
//
// Vector3d.h: interface for the CVector3d class.
//
////////////////////////////////////////////////////////////////////////////////
// 版權所有(2002)
// Copyright(2002)
// 編寫者: 向世明
// Author: Xiang Shiming
//三維自由向量
#ifndef _CVECTOR3D_H
#define _CVECTOR3D_H
#include "Grphcs.h"
#include "math.h"
class CVector3d
{
public:
CVector3d();
CVector3d(float x,float y,float z);
CVector3d(const VECTOR3D& vector);
virtual ~CVector3d();
public:
//為了符合習慣,我們不采有匈牙利命名法
float x,y,z;
public:
//設置與獲取函數
void Set(float x,float y,float z);
VECTOR3D Get() const;
public:
//運算符重載(=)
inline CVector3d operator = (const CVector3d& vector);
inline CVector3d operator = (const VECTOR3D& vector);
//運算符重載(+),返回另一個CVector3d
inline CVector3d operator + (const CVector3d& vector);
inline CVector3d operator + (const VECTOR3D& vector); //交換律不成立(結構沒有重載 "+")
//運算符重載(+=)
inline CVector3d operator += (const CVector3d& vector);
inline CVector3d operator += (const VECTOR3D& vector);
//運算符重載(-),參數為減數,返回另一個CVector3d
inline CVector3d operator - (const CVector3d& vector);
inline CVector3d operator - (const VECTOR3D& vector);
//運算符重載(-),參數為減數
inline CVector3d operator -= (const CVector3d& vector);
inline CVector3d operator -= (const VECTOR3D& vector);
//運算符重載(*),參數為乘數,返回另一個CVector3d
//向量叉積
inline CVector3d operator * (const CVector3d& vector);
inline CVector3d operator * (const VECTOR3D& vector);
//標量與向量積
inline CVector3d operator * (float fs);
//運算符重載(*),參數為乘數,返回另一個CVector3d
//向量叉積
inline CVector3d operator *= (const CVector3d& vector);
inline CVector3d operator *= (const VECTOR3D& vector);
//標量與向量積
inline CVector3d operator *= (float fs);
//對于"/"運算符,沒有必要重載
public:
//向量點積
inline float Dot(const CVector3d& vector);
inline float Dot(const VECTOR3D& vector);
inline float Dot(float x,float y, float z);
//向量的模
inline float Mag();
inline float MagSquared();
//向量矢徑端點之間的距離
inline float Dist(const CVector3d& vector);
inline float Dist(const VECTOR3D& vector);
inline float Dist(float x,float y,float z);
//向量單位化
inline CVector3d Unit();
inline void Unitize();
public:
//將this向量縮放
void Scale(CVector3d vector);
void Scale(VECTOR3D vector);
void Scale(float x, float y, float z);
//將this向量繞三軸旋轉,fTheta為角度單位
void RotateX(float fTheta);
void RotateY(float fTheta);
void RotateZ(float fTheta);
//將this向量繞任意軸旋轉,fTheta為角度單位
void Rotate(float fTheta, CVector3d axis);
void Rotate(float fTheta, VECTOR3D axis);
//(x,y,z)為旋轉軸向量
void Rotate(float fTheta, float x, float y, float z);
//計算小面法線
VECTOR3D Noraml(const VERTEX3D& v1, const VERTEX3D& v2, const VERTEX3D& v3);
VECTOR3D Noraml(const HOMOCOORD& v1, const HOMOCOORD& v2, const HOMOCOORD& v3);
};
#endif
//////////////////////////////////////////////////////////////////////////////////////////////
//
// 運算符重載
//
//////////////////////////////////////////////////////////////////////////////////////////////
//運算符重載(=)
//參數為CVector3d對象
inline CVector3d CVector3d::operator = (const CVector3d& vector)
{
x = vector.x;
y = vector.y;
z = vector.z;
return *this;
}
//運算符重載(=)
//參數為VECTOR3D結構
inline CVector3d CVector3d::operator = (const VECTOR3D& vector)
{
x = vector.x;
y = vector.y;
z = vector.z;
return *this;
}
//運算符重載(+)
//參數為CVector3d對象,返回另一個CVector3d對象
inline CVector3d CVector3d::operator + (const CVector3d& vector)
{
CVector3d v;
v.x = x + vector.x;
v.y = y + vector.y;
v.z = z + vector.z;
return v;
}
//運算符重載(+)
//參數為VECTOR3D結構,返回另一個CVector3d對象
inline CVector3d CVector3d::operator + (const VECTOR3D& vector)
{
CVector3d v;
v.x = x + vector.x;
v.y = y + vector.y;
v.z = z + vector.z;
return v;
}
//運算符重載(+=)
//參數為CVector3d對象
inline CVector3d CVector3d::operator += (const CVector3d& vector)
{
x += vector.x;
y += vector.y;
z += vector.z;
return *this;
}
//運算符重載(+=)
//參數為VECTOR3D結構
inline CVector3d CVector3d::operator += (const VECTOR3D& vector)
{
x += vector.x;
y += vector.y;
z += vector.z;
return *this;
}
//運算符重載(-)
//參數為CVector3d對象,為減數,返回另一個CVector3d對象
inline CVector3d CVector3d::operator - (const CVector3d& vector)
{
CVector3d v;
v.x = x - vector.x;
v.y = y - vector.y;
v.z = z - vector.z;
return v;
}
//運算符重載(-)
//參數為VECTOR3D結構,為減數,返回另一個CVector3d對象
inline CVector3d CVector3d::operator - (const VECTOR3D& vector)
{
CVector3d v;
v.x = x - vector.x;
v.y = y - vector.y;
v.z = z - vector.z;
return v;
}
//運算符重載(-=)
//參數為CVector3d對象,為減數
inline CVector3d CVector3d::operator -= (const CVector3d& vector)
{
x -= vector.x;
y -= vector.y;
z -= vector.z;
return *this;
}
//運算符重載(-=)
//參數為VECTOR3D結構,為減數
inline CVector3d CVector3d::operator -= (const VECTOR3D& vector)
{
x -= vector.x;
y -= vector.y;
z -= vector.z;
return *this;
}
//運算符重載(*),作向量叉積
//參數為CVector3d對象,為乘數,返回另一個CVector3d對象
inline CVector3d CVector3d::operator * (const CVector3d& vector)
{
CVector3d v;
v.x = (y * vector.z - z * vector.y);
v.y = (z * vector.x - x * vector.z);
v.z = (x * vector.y - y * vector.x);
return v;
}
//運算符重載(*),作向量叉積
//參數為VECTOR3D結構,為乘數,返回另一個CVector3d對象
inline CVector3d CVector3d::operator * (const VECTOR3D& vector)
{
CVector3d v;
v.x = (y * vector.z - z * vector.y);
v.y = (z * vector.x - x * vector.z);
v.z = (x * vector.y - y * vector.x);
return v;
}
//運算符重載(*),標量與向量之積
//參數為浮點標量,返回另一個CVector3d對象
inline CVector3d CVector3d::operator * (float fs)
{
CVector3d v;
v.x = x * fs;
v.y = y * fs;
v.z = z * fs;
return v;
}
//運算符重載(*=),作向量叉積
//參數為CVector3d對象,為乘數,
inline CVector3d CVector3d::operator *= (const CVector3d& vector)
{
float xx = (y * vector.z - z * vector.y);
float yy = (z * vector.x - x * vector.z);
float zz = (x * vector.y - y * vector.x);
x = xx;
y = yy;
z = zz;
return *this;
}
//運算符重載(*=),作向量叉積
//參數為VECTOR3D結構,為乘數
inline CVector3d CVector3d::operator *= (const VECTOR3D& vector)
{
float xx = (y * vector.z - z * vector.y);
float yy = (z * vector.x - x * vector.z);
float zz = (x * vector.y - y * vector.x);
x = xx;
y = yy;
z = zz;
return *this;
}
//運算符重載(*),標量與向量之積
//參數為浮點標量
inline CVector3d CVector3d::operator *= (float fs)
{
x *= fs;
y *= fs;
z *= fs;
return *this;
}
//////////////////////////////////////////////////////////////////////////////////////////////
//
// 其它基本運算
//
//////////////////////////////////////////////////////////////////////////////////////////////
//向量內積
inline float CVector3d::Dot(const CVector3d& vector)
{
return (x * vector.x + y * vector.y + z * vector.z);
}
//向量內積
inline float CVector3d::Dot(const VECTOR3D& vector)
{
return (x * vector.x + y * vector.y + z * vector.z);
}
//向量內積
inline float CVector3d::Dot(float x,float y, float z)
{
return (this->x * x + this->y * y + this->z * z);
}
//向量的模
inline float CVector3d::Mag()
{
return (float)(sqrt(x * x + y * y + z * z));
}
//向量模的平方
inline float CVector3d::MagSquared()
{
return (x * x + y * y + z * z);
}
//兩向量矢量端點之間的距離
inline float CVector3d::Dist(const CVector3d& vector)
{
return (*this - vector).Mag();
}
//兩向量矢量端點之間的距離
inline float CVector3d::Dist(const VECTOR3D& vector)
{
return (*this - vector).Mag();
}
//兩向量矢量端點之間的距離
inline float CVector3d::Dist(float x,float y,float z)
{
return (float)sqrt((this->x - x) * (this->x - x) + (this->y - y) * (this->y - y) + (this->z - z) * (this->z - z));
}
//向量單位化
//向量單位化,返回一個單位向量
inline CVector3d CVector3d::Unit()
{
CVector3d vector;
float fMag = (float)sqrt(x * x + y * y + z * z);
if(fMag < 0.0001f)fMag = 1.0f;
vector.x = x / fMag;
vector.y = y / fMag;
vector.z = z / fMag;
return vector;
}
//自身單位化
inline void CVector3d::Unitize()
{
float fMag = (float)sqrt(x * x + y * y + z * z);
if(fMag < 0.0001f)fMag = 1.0f;
x /= fMag;
y /= fMag;
z /= fMag;
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -