C++ Exercises(一)
一个3D向量类
// Vertex3D.h: interface for the Vertex3D class.
//
//////////////////////////////////////////////////////////////////////
class Vertex3D
{//3维向量类
private:
double x,y,z;
public:
Vertex3D();
Vertex3D(double tx,double ty,double tz);
Vertex3D(const Vertex3D& vt);//拷贝构造函数
Vertex3D& operator = (const Vertex3D &vt);//重载赋值运算符
bool operator == (const Vertex3D &vt)const;//重载"=="运算符
bool operator != (const Vertex3D &vt)const;//重载"!="运算符
Vertex3D operator -()const;//负向量
Vertex3D operator - (const Vertex3D &vt)const;//向量减法
Vertex3D operator + (const Vertex3D &vt)const;//向量加法
Vertex3D operator * (double fac)const;//与标量相乘
Vertex3D operator / (double fac)const;//与标量相除
Vertex3D& operator += (const Vertex3D &vt);
Vertex3D& operator -= (const Vertex3D &vt);
Vertex3D& operator *= (double fac);//与标量相乘
Vertex3D& operator /= (double fac);//与标量相除
double operator * (const Vertex3D &vt)const;//向量点乘
void reset();
void normalize();//向量单位化
double VertexLength()const;//向量模
virtual ~Vertex3D();
friend Vertex3D crossProduct(const Vertex3D &a,const Vertex3D &b);
friend double Distance(const Vertex3D &a,const Vertex3D &b);
};
//
//////////////////////////////////////////////////////////////////////
class Vertex3D
{//3维向量类
private:
double x,y,z;
public:
Vertex3D();
Vertex3D(double tx,double ty,double tz);
Vertex3D(const Vertex3D& vt);//拷贝构造函数
Vertex3D& operator = (const Vertex3D &vt);//重载赋值运算符
bool operator == (const Vertex3D &vt)const;//重载"=="运算符
bool operator != (const Vertex3D &vt)const;//重载"!="运算符
Vertex3D operator -()const;//负向量
Vertex3D operator - (const Vertex3D &vt)const;//向量减法
Vertex3D operator + (const Vertex3D &vt)const;//向量加法
Vertex3D operator * (double fac)const;//与标量相乘
Vertex3D operator / (double fac)const;//与标量相除
Vertex3D& operator += (const Vertex3D &vt);
Vertex3D& operator -= (const Vertex3D &vt);
Vertex3D& operator *= (double fac);//与标量相乘
Vertex3D& operator /= (double fac);//与标量相除
double operator * (const Vertex3D &vt)const;//向量点乘
void reset();
void normalize();//向量单位化
double VertexLength()const;//向量模
virtual ~Vertex3D();
friend Vertex3D crossProduct(const Vertex3D &a,const Vertex3D &b);
friend double Distance(const Vertex3D &a,const Vertex3D &b);
};
// Vertex3D.cpp: implementation of the Vertex3D class.
//
//////////////////////////////////////////////////////////////////////
#include "Vertex3D.h"
#include <math.h>
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
Vertex3D::Vertex3D()
{
this->x = 0.0f;
this->y = 0.0f;
this->z = 0.0f;
}
Vertex3D::Vertex3D(double tx,double ty,double tz)
{
this->x = tx;
this->y = ty;
this->z = tz;
}
Vertex3D::Vertex3D(const Vertex3D &vt)
{
this->x = vt.x;
this->y = vt.y;
this->z = vt.z;
}
Vertex3D& Vertex3D::operator = (const Vertex3D& vt)
{
this->x = vt.x;
this->y = vt.y;
this->z = vt.z;
return *this;
}
bool Vertex3D::operator == (const Vertex3D& vt)const
{//判断向量是否相等
return this->x==vt.x&&this->y==vt.y&&this->z==vt.z;
}
bool Vertex3D::operator != (const Vertex3D& vt)const
{
return this->x!=vt.x&&this->y!=vt.y&&this->z!=vt.z;
}
Vertex3D Vertex3D::operator - ()const
{//负向量
return Vertex3D(-x,-y,-z);
}
Vertex3D Vertex3D::operator - (const Vertex3D& vt)const
{//向量减法
return Vertex3D(x-vt.x,y-vt.y,z-vt.z);
}
Vertex3D Vertex3D::operator + (const Vertex3D& vt)const
{//向量加法
return Vertex3D(x+vt.x,y+vt.y,z+vt.z);
}
Vertex3D Vertex3D::operator * (double fac)const
{//与标量乘法
return Vertex3D(x*fac,y*fac,z*fac);
}
Vertex3D Vertex3D::operator / (double fac)const
{//与标量相除
return Vertex3D(x/fac,y/fac,z/fac);
}
Vertex3D& Vertex3D::operator += (const Vertex3D &vt)
{
this->x += vt.x;
this->y += vt.y;
this->z += vt.z;
return *this;
}
Vertex3D& Vertex3D::operator -= (const Vertex3D &vt)
{
this->x -= vt.x;
this->y -= vt.y;
this->z -= vt.z;
return *this;
}
Vertex3D& Vertex3D::operator *= (double fac)
{
this->x *= fac;
this->y *= fac;
this->z *= fac;
return *this;
}
Vertex3D& Vertex3D::operator /= (double fac)
{
this->x /= fac;
this->y /= fac;
this->z /= fac;
return *this;
}
void Vertex3D::reset()
{//置为零向量
this->x = 0.0f;
this->y = 0.0f;
this->z = 0.0f;
}
double Vertex3D::VertexLength()const
{//向量长度
double tmp = x*x+y*y+z*z;
return sqrt(tmp);
}
void Vertex3D::normalize()
{//向量单位化
double len = this->VertexLength();//获取向量长度
if(len>0.0f)
{
double tmp = 1.0f/len;
this->x *= tmp;
this->y *= tmp;
this->z *= tmp;
}
}
double Vertex3D::operator * (const Vertex3D &vt)const
{//向量点乘
return x*vt.x+y*vt.y+z*vt.z;
}
Vertex3D::~Vertex3D()
{
}
Vertex3D crossProduct(const Vertex3D &a,const Vertex3D &b)
{//向量叉乘
return Vertex3D(a.y*b.z - a.z*b.y,a.z*b.x - a.x*b.z,a.x*b.y - a.y*b.x);
}
double Distance(const Vertex3D &a,const Vertex3D &b)
{//向量距离
double dx = a.x - b.x,dy = a.y - b.y,dz = a.z - b.z;
return sqrt(dx*dx+dy*dy+dz*dz);
}
//
//////////////////////////////////////////////////////////////////////
#include "Vertex3D.h"
#include <math.h>
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
Vertex3D::Vertex3D()
{
this->x = 0.0f;
this->y = 0.0f;
this->z = 0.0f;
}
Vertex3D::Vertex3D(double tx,double ty,double tz)
{
this->x = tx;
this->y = ty;
this->z = tz;
}
Vertex3D::Vertex3D(const Vertex3D &vt)
{
this->x = vt.x;
this->y = vt.y;
this->z = vt.z;
}
Vertex3D& Vertex3D::operator = (const Vertex3D& vt)
{
this->x = vt.x;
this->y = vt.y;
this->z = vt.z;
return *this;
}
bool Vertex3D::operator == (const Vertex3D& vt)const
{//判断向量是否相等
return this->x==vt.x&&this->y==vt.y&&this->z==vt.z;
}
bool Vertex3D::operator != (const Vertex3D& vt)const
{
return this->x!=vt.x&&this->y!=vt.y&&this->z!=vt.z;
}
Vertex3D Vertex3D::operator - ()const
{//负向量
return Vertex3D(-x,-y,-z);
}
Vertex3D Vertex3D::operator - (const Vertex3D& vt)const
{//向量减法
return Vertex3D(x-vt.x,y-vt.y,z-vt.z);
}
Vertex3D Vertex3D::operator + (const Vertex3D& vt)const
{//向量加法
return Vertex3D(x+vt.x,y+vt.y,z+vt.z);
}
Vertex3D Vertex3D::operator * (double fac)const
{//与标量乘法
return Vertex3D(x*fac,y*fac,z*fac);
}
Vertex3D Vertex3D::operator / (double fac)const
{//与标量相除
return Vertex3D(x/fac,y/fac,z/fac);
}
Vertex3D& Vertex3D::operator += (const Vertex3D &vt)
{
this->x += vt.x;
this->y += vt.y;
this->z += vt.z;
return *this;
}
Vertex3D& Vertex3D::operator -= (const Vertex3D &vt)
{
this->x -= vt.x;
this->y -= vt.y;
this->z -= vt.z;
return *this;
}
Vertex3D& Vertex3D::operator *= (double fac)
{
this->x *= fac;
this->y *= fac;
this->z *= fac;
return *this;
}
Vertex3D& Vertex3D::operator /= (double fac)
{
this->x /= fac;
this->y /= fac;
this->z /= fac;
return *this;
}
void Vertex3D::reset()
{//置为零向量
this->x = 0.0f;
this->y = 0.0f;
this->z = 0.0f;
}
double Vertex3D::VertexLength()const
{//向量长度
double tmp = x*x+y*y+z*z;
return sqrt(tmp);
}
void Vertex3D::normalize()
{//向量单位化
double len = this->VertexLength();//获取向量长度
if(len>0.0f)
{
double tmp = 1.0f/len;
this->x *= tmp;
this->y *= tmp;
this->z *= tmp;
}
}
double Vertex3D::operator * (const Vertex3D &vt)const
{//向量点乘
return x*vt.x+y*vt.y+z*vt.z;
}
Vertex3D::~Vertex3D()
{
}
Vertex3D crossProduct(const Vertex3D &a,const Vertex3D &b)
{//向量叉乘
return Vertex3D(a.y*b.z - a.z*b.y,a.z*b.x - a.x*b.z,a.x*b.y - a.y*b.x);
}
double Distance(const Vertex3D &a,const Vertex3D &b)
{//向量距离
double dx = a.x - b.x,dy = a.y - b.y,dz = a.z - b.z;
return sqrt(dx*dx+dy*dy+dz*dz);
}
作者:洞庭散人
出处:http://phinecos.cnblogs.com/
本博客遵从Creative Commons Attribution 3.0 License,若用于非商业目的,您可以自由转载,但请保留原作者信息和文章链接URL。
posted on 2007-04-29 16:04 Phinecos(洞庭散人) 阅读(1003) 评论(1) 编辑 收藏 举报
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· .NET周刊【3月第1期 2025-03-02】
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· [AI/GPT/综述] AI Agent的设计模式综述