3D矢量类CVector3D

写一个3D矢量类CVector3D,要求含三种构造函数,并有拷贝,点积,叉积,求模,单位化等操作,并实现”+; -; =; ==; +=; -=; *; [ ]” 等运算符的重载。

 

注:由于开始定义的时候,没有将三维数据对应的成员变量使用数组存放,因此重载下标运算符'[ ]'没有意义。

 

================================================

 

#include <iostream>
#include <cmath>

using namespace std;

template<class T>
class CVector3D
{
public:
 CVector3D();
 //CVector3D(T x=0,T y=0,T z=0);
 CVector3D(T x,T y,T z);

 ~CVector3D(){};

 //基本运算
 CVector3D(CVector3D &aVector);             //拷贝构造函数
 double DotProduct(CVector3D& aVector);     //向量点积
 CVector3D CrossProduct(CVector3D& aVector);//向量叉积
 CVector3D Normalized();                    //向量单位化
 double VectorModulo();                     //向量模运算
 double VectorAngle(CVector3D& aVector);    //向量夹角
 void VectorPrint();

 //运算符重载
 CVector3D operator +(const CVector3D &ob1);
 CVector3D operator -(const CVector3D &ob1);
 CVector3D& operator =(const CVector3D& src);
 bool operator ==(const CVector3D& obj);
 CVector3D& operator +=(const CVector3D &ob1);
 CVector3D& operator -=(const CVector3D &ob1);
    //CVector3D& operator *(); //指针运算符的重载
 void operator *();         //指针运算符的重载

private:
 T x_offset;
 T y_offset;
 T z_offset;
};

template<class T>
void CVector3D<T>::VectorPrint()
{
 cout<<"("<<x_offset<<","<<y_offset<<","<<z_offset<<")"<<endl;
}
template<class T>
CVector3D<T>::CVector3D()
{
 x_offset = 0;
 y_offset = 0;
 z_offset = 0;
}

template<class T>
void CVector3D<T>::operator*()
{
 cout<<"("<<x_offset<<","<<y_offset<<","<<z_offset<<")"<<endl;
}

//template<class T>
//CVector3D<T>& CVector3D<T>::operator*()
//{
// return *this; 
//}

template<class T>
CVector3D<T>& CVector3D<T>::operator-=( const CVector3D &ob1 )
{
 x_offset -= ob1.x_offset;
 y_offset -= ob1.y_offset;
 z_offset -= ob1.z_offset;
 return *this;
}
template<class T>
CVector3D<T>& CVector3D<T>::operator+=( const CVector3D &ob1 )
{
 x_offset += ob1.x_offset;
 y_offset += ob1.y_offset;
 z_offset += ob1.z_offset;
 return *this;
}

template<class T>
bool CVector3D<T>::operator==( const CVector3D& obj )
{
 if(x_offset==obj.x_offset && y_offset==obj.y_offset && z_offset==obj.z_offset)
  return true;
 else
  return false;
}

template<class T>
CVector3D<T>& CVector3D<T>::operator=( const CVector3D& src )
{
 if(this==&src)
  return *this;
 x_offset = src.x_offset;
 y_offset = src.y_offset;
 z_offset = src.z_offset;
 return *this;
}
template<class T>
CVector3D<T> CVector3D<T>::operator-( const CVector3D &ob1 )
{
 CVector3D temp;
 temp.x_offset = x_offset - ob1.x_offset;
 temp.y_offset = y_offset - ob1.y_offset;
 temp.z_offset = z_offset - ob1.z_offset;
 return temp;
}
template<class T>
CVector3D<T> CVector3D<T>::operator+( const CVector3D &ob1 )
{
 CVector3D temp;
 temp.x_offset = x_offset + ob1.x_offset;
 temp.y_offset = y_offset + ob1.y_offset;
 temp.z_offset = z_offset + ob1.z_offset;
 return temp;
}

template<class T>
CVector3D<T> CVector3D<T>::Normalized()
{
 double moudle = this->VectorModulo();
 CVector3D temp;
 temp.x_offset = x_offset/moudle;
 temp.y_offset = y_offset/moudle;
 temp.z_offset = z_offset/moudle;
 return temp;
}

template<class T>
CVector3D<T> CVector3D<T>::CrossProduct( CVector3D<T>& aVector )
{
 CVector3D temp;
 temp.x_offset = y_offset*aVector.z_offset - z_offset*aVector.y_offset; //y1z2-z1y2
 temp.y_offset = z_offset*aVector.x_offset - x_offset*aVector.z_offset; //z1x2-x1z2
 temp.z_offset = x_offset*aVector.y_offset - y_offset*aVector.x_offset; //x1y2-y1x2
 return temp;
}

template<class T>
double CVector3D<T>::VectorAngle( CVector3D& aVector )
{
 return (x_offset*aVector.x_offset + y_offset*aVector.y_offset + z_offset*aVector.z_offset)
  /this->VectorModulo()*aVector.VectorModulo();
}

template<class T>
double CVector3D<T>::VectorModulo()
{
 //要加上long double
 return sqrt(long double(x_offset*x_offset+y_offset*y_offset+z_offset*z_offset));
}
template<class T>
double CVector3D<T>::DotProduct( CVector3D& aVector )
{
 return x_offset*aVector.x_offset + y_offset*aVector.y_offset + z_offset*aVector.z_offset;


}
template<class T>
CVector3D<T>::CVector3D( CVector3D &aVector )
{
 this->x_offset = aVector.x_offset;
 this->y_offset = aVector.y_offset;
 this->z_offset = aVector.z_offset;
}

 

template<class T>
CVector3D<T>::CVector3D( T x,T y,T z )
:x_offset(x)
,y_offset(y)
,z_offset(z)
{

}

 


int main()
{
 CVector3D<int> a(5,6,7),b(8,9,10);
 CVector3D<int> c;     //无参构造
 *c;                   //指针运算符'*'重载调用
 c = a+b;              // '+'
 *c;
 c += b;               // '+='
 *c;
 cout<<c.DotProduct(b)<<endl;
 cout<<c.VectorModulo()<<endl;
 CVector3D<int> d = c.CrossProduct(a);
 *d;
 CVector3D<int> e = d;  // '='
 *e;
 return 1;
}

 

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Python 3D人脸图矢量图指的是使用Python程序语言进行编写的,能够生成3D立体效果的人脸图,并以矢量图的形式保存。 Python是一种功能强大的编程语言,它提供了许多库和工具来处理图形和图像。其中有一些库可以用来生成3D图形,例如Matplotlib、Mayavi和OpenCV等。这些库提供了各种功能和方法,可以根据输入的数据创建或加载3D模型,并将其渲染成立体效果的人脸图。 为了生成3D人脸图,首先需要使用Python提供的图像处理库(如OpenCV)来获取或生成人脸图像数据。这可以通过从摄像头获取实时视频流或加载已有的图像来实现。然后,可以使用3D图形库(如Mayavi)来将图像数据转换为3D模型。算法可以通过人脸识别和重新构建深度等技术来实现。 一旦有了3D模型,可以使用库中的方法和函数来设置光照、材质、摄像机视角等属性,以及选择适当的渲染方法。最后,可以使用该库提供的保存函数,将生成的3D立体人脸图以矢量图的形式保存下来。矢量图格式如SVG(Scalable Vector Graphics)或PDF(Portable Document Format)可以实现无损缩放并保持图像质量。 总结起来,用Python生成3D人脸图矢量图的过程涉及到使用图像处理和图形处理库,识别人脸,生成3D模型,并设置渲染属性,最后保存为矢量图格式。这样可以实现高质量图像的生成和无损缩放的输出。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值