|
左手坐标系的直观表示: ![LH.JPG](http://www.cppblog.com/images/cppblog_com/mybios/ArticlePics/LH.JPG) 向量的表示(轴对齐包围盒(AABB(axially aligned bounding box))): ![Vector.JPG](http://www.cppblog.com/images/cppblog_com/mybios/ArticlePics/Vector.JPG) 2D向量的长度: ||v|| = sqrt(vx*vx + vy*vy)3D向量的长度: ||v|| = sqrt(vx*vx + vy*vy + vz*vz)
标准化向量=此向量/此向量的长度=vx / ||v|| , vy / ||v|| , vz / ||v||
标准化后的向量的头接触到圆心在原点的单位圆(单位圆的半径为1)
向量加法的几何意义(三角形法则):
![vAdd.JPG](http://www.cppblog.com/images/cppblog_com/mybios/ArticlePics/vAdd.JPG)
计算一个点到另一个点的位移可以使用三角形法则和向量减法来解决这个问题:
![Vector1.JPG](http://www.cppblog.com/images/cppblog_com/mybios/ArticlePics/Vector1.JPG) 两点间距离的公式:设两点分别为3D向量a和b 则距离(a,b)=||b-a||=sqrt((bx - ax)2 +(by - ay)2 +(bz - az)2 )
向量点乘:点乘等于向量大小与向量夹角的cos值的积,其中c为两点a和b的夹角: a点乘b=||a||*||b||*cos(c)
计算向量的夹角:c =acos( (a*b)/(||a|| * ||b||))
如果a和b是单位向量,则c=acos(a*b)
当点乘结果大于0,则夹角小于90度 当点乘结果等于0,则夹角等于90度 当点乘结果小于0,则夹角大于90度
向量差乘:
![Vector2.JPG](http://www.cppblog.com/images/cppblog_com/mybios/ArticlePics/Vector2.JPG)
向量的封装类:
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif) /**////////////////////////////////////////////////////////////////////////////// //
// 3D Math Primer for Games and Graphics Development
//
// Vector3.h - Declarations for 3D vector class
//
// Visit gamemath.com for the latest version of this file.
//
// For additional comments, see Chapter 6.
//
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif) /**//////////////////////////////////////////////////////////////////////////////![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
#ifndef __VECTOR3_H_INCLUDED__
#define __VECTOR3_H_INCLUDED__
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
#include <math.h>
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif) /**////////////////////////////////////////////////////////////////////////////// //
// class Vector3 - a simple 3D vector class
//
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif) /**//////////////////////////////////////////////////////////////////////////////![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif) class Vector3 {
public:
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
// Public representation: Not many options here.
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
float x,y,z;
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
// Constructors
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
// Default constructor leaves vector in
// an indeterminate state
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) Vector3() {}
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
// Copy constructor
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) Vector3(const Vector3 &a) : x(a.x), y(a.y), z(a.z) {}
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
// Construct given three values
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) Vector3(float nx, float ny, float nz) : x(nx), y(ny), z(nz) {}
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
// Standard object maintenance
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
// Assignment. We adhere to C convention and
// return reference to the lvalue
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) Vector3 &operator =(const Vector3 &a) {
x = a.x; y = a.y; z = a.z;
return *this;
}
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
// Check for equality
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) bool operator ==(const Vector3 &a) const {
return x==a.x && y==a.y && z==a.z;
}
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) bool operator !=(const Vector3 &a) const {
return x!=a.x || y!=a.y || z!=a.z;
}
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
// Vector operations
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
// Set the vector to zero
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) void zero() { x = y = z = 0.0f; }
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
// Unary minus returns the negative of the vector
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) Vector3 operator -() const { return Vector3(-x,-y,-z); }
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
// Binary + and - add and subtract vectors
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) Vector3 operator +(const Vector3 &a) const {
return Vector3(x + a.x, y + a.y, z + a.z);
}
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) Vector3 operator -(const Vector3 &a) const {
return Vector3(x - a.x, y - a.y, z - a.z);
}
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
// Multiplication and division by scalar
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) Vector3 operator *(float a) const {
return Vector3(x*a, y*a, z*a);
}
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) Vector3 operator /(float a) const {
float oneOverA = 1.0f / a; // NOTE: no check for divide by zero here
return Vector3(x*oneOverA, y*oneOverA, z*oneOverA);
}
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
// Combined assignment operators to conform to
// C notation convention
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) Vector3 &operator +=(const Vector3 &a) {
x += a.x; y += a.y; z += a.z;
return *this;
}
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) Vector3 &operator -=(const Vector3 &a) {
x -= a.x; y -= a.y; z -= a.z;
return *this;
}
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) Vector3 &operator *=(float a) {
x *= a; y *= a; z *= a;
return *this;
}
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) Vector3 &operator /=(float a) {
float oneOverA = 1.0f / a;
x *= oneOverA; y *= oneOverA; z *= oneOverA;
return *this;
}
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
// Normalize the vector
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) void normalize() {
float magSq = x*x + y*y + z*z;
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) if (magSq > 0.0f) { // check for divide-by-zero
float oneOverMag = 1.0f / sqrt(magSq);
x *= oneOverMag;
y *= oneOverMag;
z *= oneOverMag;
}
}
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
// Vector dot product. We overload the standard
// multiplication symbol to do this
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) float operator *(const Vector3 &a) const {
return x*a.x + y*a.y + z*a.z;
}
};
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif) /**////////////////////////////////////////////////////////////////////////////// //
// Nonmember functions
//
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif) /**//////////////////////////////////////////////////////////////////////////////![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
// Compute the magnitude of a vector
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif) inline float vectorMag(const Vector3 &a) {
return sqrt(a.x*a.x + a.y*a.y + a.z*a.z);
}
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
// Compute the cross product of two vectors
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif) inline Vector3 crossProduct(const Vector3 &a, const Vector3 &b) {
return Vector3(
a.y*b.z - a.z*b.y,
a.z*b.x - a.x*b.z,
a.x*b.y - a.y*b.x
);
}
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
// Scalar on the left multiplication, for symmetry
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif) inline Vector3 operator *(float k, const Vector3 &v) {
return Vector3(k*v.x, k*v.y, k*v.z);
}
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
// Compute the distance between two points
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif) inline float distance(const Vector3 &a, const Vector3 &b) {
float dx = a.x - b.x;
float dy = a.y - b.y;
float dz = a.z - b.z;
return sqrt(dx*dx + dy*dy + dz*dz);
}
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
// Compute the distance between two points, squared. Often useful
// when comparing distances, since the square root is slow
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif) inline float distanceSquared(const Vector3 &a, const Vector3 &b) {
float dx = a.x - b.x;
float dy = a.y - b.y;
float dz = a.z - b.z;
return dx*dx + dy*dy + dz*dz;
}
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif) /**////////////////////////////////////////////////////////////////////////////// //
// Global variables
//
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif) /**//////////////////////////////////////////////////////////////////////////////![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
// We provide a global zero vector constant
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
extern const Vector3 kZeroVector;
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif) /**////////////////////////////////////////////////////////////////////////////// #endif // #ifndef __VECTOR3_H_INCLUDED__
|