当前位置: 首页 > news >正文

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);
};


// 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);
}


本文转自Phinecos(洞庭散人)博客园博客,原文链接:http://www.cnblogs.com/phinecos/archive/2007/04/29/732444.html,如需转载请自行联系原作者

相关文章:

  • restful+springmvc+mybatis+ webservice 分布式架构
  • 有歧义(AMBIGUOUS LAYOUT)的约束布局调试方法2
  • CSU-ACM2018寒假集训选拔-入门题
  • 云数据库 Redis 版功能特性
  • bootstrap和elementUI真的会冲突
  • LeetCode:26. Remove Duplicates from Sorted Array(Easy)
  • jvm 内存分配
  • 从Storm和Spark 学习流式实时分布式计算的设计
  • Nginx + Tomcat + HTTPS 配置原来不需要在 Tomcat 上启用 SSL 支持
  • 应用多级缓存模式支撑海量读服务
  • iOS 兼容多个有crash 收集机制的SDK
  • 37.3. HQL
  • 详细解析漏洞4个boom
  • HSRP、VRRP、ACL
  • 407. 加一
  • python3.6+scrapy+mysql 爬虫实战
  • 【347天】每日项目总结系列085(2018.01.18)
  • 【跃迁之路】【699天】程序员高效学习方法论探索系列(实验阶段456-2019.1.19)...
  • CSS盒模型深入
  • Docker 笔记(1):介绍、镜像、容器及其基本操作
  • download使用浅析
  • Java到底能干嘛?
  • Netty源码解析1-Buffer
  • node-sass 安装卡在 node scripts/install.js 解决办法
  • Puppeteer:浏览器控制器
  • React as a UI Runtime(五、列表)
  • React的组件模式
  • SOFAMosn配置模型
  • 从零到一:用Phaser.js写意地开发小游戏(Chapter 3 - 加载游戏资源)
  • 大快搜索数据爬虫技术实例安装教学篇
  • 关于for循环的简单归纳
  • 基于axios的vue插件,让http请求更简单
  • 利用DataURL技术在网页上显示图片
  • 目录与文件属性:编写ls
  • 漂亮刷新控件-iOS
  • 人脸识别最新开发经验demo
  • 正则与JS中的正则
  • 【干货分享】dos命令大全
  • Mac 上flink的安装与启动
  • scrapy中间件源码分析及常用中间件大全
  • 基于django的视频点播网站开发-step3-注册登录功能 ...
  • 蚂蚁金服CTO程立:真正的技术革命才刚刚开始
  • 浅谈sql中的in与not in,exists与not exists的区别
  • ​软考-高级-系统架构设计师教程(清华第2版)【第9章 软件可靠性基础知识(P320~344)-思维导图】​
  • (二)c52学习之旅-简单了解单片机
  • (论文阅读26/100)Weakly-supervised learning with convolutional neural networks
  • (转)Groupon前传:从10个月的失败作品修改,1个月找到成功
  • (转)项目管理杂谈-我所期望的新人
  • ./indexer: error while loading shared libraries: libmysqlclient.so.18: cannot open shared object fil
  • .NET 2.0中新增的一些TryGet,TryParse等方法
  • .net 4.0发布后不能正常显示图片问题
  • .NET/C# 使用 #if 和 Conditional 特性来按条件编译代码的不同原理和适用场景
  • .NET是什么
  • .NET运行机制
  • /bin/rm: 参数列表过长"的解决办法