[C++][代码库]Vector3空间向量类
//Vecotr3.h
#pragma once extern const double uZero; class Vector3
{
float x, y, z;
public:
Vector3():x(), y(), z(){}
Vector3(float x1, float y1, float z1):x(x1), y(y1), z(z1){}
Vector3(const Vector3 &v);
~Vector3();
void operator=(const Vector3 &v);
Vector3 operator+(const Vector3 &v);
Vector3 operator-(const Vector3 &v);
Vector3 operator/(const Vector3 &v);
Vector3 operator*(const Vector3 &v);
Vector3 operator+(float f);
Vector3 operator-(float f);
Vector3 operator/(float f);
Vector3 operator*(float f);
float dot(const Vector3 &v);
float length();
void normalize();
Vector3 crossProduct(const Vector3 &v);
void printVec3();
};
//Vector3.cpp
#include"Plane.h"
#include<iostream> const double uZero = 1e-; //复制构造函数,必须为常量引用参数,否则编译不通过
Vector3::Vector3(const Vector3 &v):x(v.x), y(v.y), z(v.z)
{
} Vector3::~Vector3()
{
} void Vector3::operator=(const Vector3 &v)
{
x = v.x;
y = v.y;
z = v.z;
} Vector3 Vector3::operator+(const Vector3 &v)
{
return Vector3(x+v.x, y+v.y, z+v.z);
} Vector3 Vector3::operator-(const Vector3 &v)
{
return Vector3(x-v.x, y-v.y, z-v.z);
} Vector3 Vector3::operator/(const Vector3 &v)
{
if (fabsf(v.x) <= uZero || fabsf(v.y) <= uZero || fabsf(v.z) <= uZero)
{
std::cerr<<"Over flow!\n";
return *this;
}
return Vector3(x/v.x, y/v.y, z/v.z);
} Vector3 Vector3::operator*(const Vector3 &v)
{
return Vector3(x*v.x, y*v.y, z*v.z);
} Vector3 Vector3::operator+(float f)
{
return Vector3(x+f, y+f, z+f);
} Vector3 Vector3::operator-(float f)
{
return Vector3(x-f, y-f, z-f);
} Vector3 Vector3::operator/(float f)
{
if (fabsf(f) < uZero)
{
std::cerr<<"Over flow!\n";
return *this;
}
return Vector3(x/f, y/f, z/f);
} Vector3 Vector3::operator*(float f)
{
return Vector3(x*f, y*f, z*f);
} float Vector3::dot(const Vector3 &v)
{
return x*v.x + y*v.y + z*v.z;
} float Vector3::length()
{
return sqrtf(dot(*this));
} void Vector3::normalize()
{
float len = length();
if (len < uZero) len = ;
len = /len; x *= len;
y *= len;
z *= len;
} /*
Cross Product叉乘公式
aXb = | i, j, k |
| a.x a.y a.z|
| b.x b.y b.z| = (a.x*b.z -a.z*b.y)i + (a.z*b.x - a.x*b.z)j + (a.x+b.y - a.y*b.x)k
*/
Vector3 Vector3::crossProduct(const Vector3 &v)
{
return Vector3(y * v.z - z * v.y,
z * v.x - x * v.z,
x * v.y - y * v.x);
} void Vector3::printVec3()
{
std::cout<<"("<<x<<", "<<y<<", "<<z<<")"<<std::endl;
}
#include<iostream>
#include<vector>
#include"Vector3.h" using namespace std; int main()
{
Vector3 v31;
Vector3 v32(2.0f,3.0f,4.0f);
Vector3 v33(v32 - 1.0f);
cout<<"We have original Vector3s:\n";
v31.printVec3();
v32.printVec3();
v33.printVec3(); cout<<"v32 crossproduct v33 is:\n";
Vector3 v3233 = v32.crossProduct(v33);
v3233.printVec3(); cout<<"Now we normalize them:\n";
v31.normalize();
v32.normalize();
v33.normalize();
v3233.normalize();
v31.printVec3();
v32.printVec3();
v33.printVec3();
v3233.printVec3(); system("pause");
return ;
}
![](http://www.2cto.com/uploadfile/2013/1127/20131127022727239.jpg)
[C++][代码库]Vector3空间向量类的更多相关文章
- C++实现一个Vector3空间向量类(转)
转自:http://www.2cto.com/kf/201311/260139.html ector2,3,4类在DirectX中都有现成的可以调用,不过要实现其中的功能其实也不难,也都是一些简单的数 ...
- 如何统计TFS代码库中的团队项目所占用的磁盘空间
在一个开发团队较多的研发中心,当开发人员的代码数据积累到一定程度,TFS系统的磁盘空间的使用率会逐渐成为系统管理员关注的问题.你可能会关注代码库中每个团队项目,甚至每个目录占用的的磁盘空间.不幸的,即 ...
- 文本相似度算法——空间向量模型的余弦算法和TF-IDF
1.信息检索中的重要发明TF-IDF TF-IDF是一种统计方法,TF-IDF的主要思想是,如果某个词或短语在一篇文章中出现的频率TF高,并且在其他文章中很少出现,则认为此词或者短语具有很好的类别区分 ...
- 第六章:3D向量类
第一节:类接口的设计 1.好的类在设计之前首先要回答下列问题:“这些类将包含哪些数据?”,“这个类将提供什么样的操作?”,“在哪些数据上执行操作?”. 我们已经知道我们要设计的是3D向量类,用来存储x ...
- 2018-8-10-C#-代码占用的空间
title author date CreateTime categories C# 代码占用的空间 lindexi 2018-08-10 19:16:52 +0800 2018-2-13 17:23 ...
- Overview of the Oppia codebase(Oppia代码库总览)
Oppia is built with Google App Engine. Its backend is written in Python, and its frontend is written ...
- STORM_0008_Structure-of-the-codebase_Storm的代码库的结构
http://storm.apache.org/releases/1.0.1/Structure-of-the-codebase.html Structure of the codebase 源码分成 ...
- 使用libzplay库封装一个音频类
装载请说明原地址,谢谢~~ 前两天我已经封装好一个duilib中使用的webkit内核的浏览器控件和一个基于vlc的用于播放视频的视频控件,这两个控件可以分别用在放酷狗播放器的乐库功能和MV ...
- gtest代码库浅析
代码库工程概述 IDE:Visual Studio 2010 sln路径:gtest\msvc\gtest.sln 用IDE打开上面的sln,可以看到以下四个工程,算不上复杂.展开之后更是感觉这几个工 ...
随机推荐
- iOS 从手机相册里选取图片
#import <UIKit/UIKit.h> @interface AppDelegate : UIResponder <UIApplicationDelegate> @pr ...
- JSON Web Token
What is JSON Web Token? JSON Web Token (JWT) is an open standard (RFC 7519) that defines a compact a ...
- Simple GDB case
to be added... gdb a.out [Inferior 1 (process 9718) exited with code 05] (gdb) list Line number ...
- spring 小结
第一步:配置 web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xs ...
- Java 分布式应用
- ads 调试
1.路径错误,中文名称 2.定义错误
- M面经Prepare: Positive-Negative partitioning preserving order
Given an array which has n integers,it has both positive and negative integers.Now you need sort thi ...
- 数据库SQL 查询
查询 1.简单查询 select * from info(表名) --查所有数据 select code(列名),name(列名) from 表名 --查指定列的数据 selec ...
- Python和Ruby开发中源文件中文注释乱码的解决方法(Eclipse和Aptana Studio3均适用)
Eclipse的设置(Aptana Studio3与Eclipse基本完全相同,此处略) window->preferences->general->editors->text ...
- myeclipse项目里有红色感叹号
myeclipse项目里有红色感叹号 这种情况是因为 .classpath 文件里面配置引用了某个jar,但是实际上你的 lib 里面并没有这个jar 所以才会有红色的提示. 不用拿.classpat ...