Skip to content

Instantly share code, notes, and snippets.

@tkymx
Created January 25, 2013 04:42
Show Gist options
  • Save tkymx/4bc3989a768b573580f1 to your computer and use it in GitHub Desktop.
Save tkymx/4bc3989a768b573580f1 to your computer and use it in GitHub Desktop.
ベクトル表現のために使用
#pragma once
#include<math.h>
#define D3DX_PI ((double) 3.141592654f)
#define D3DX_1BYPI ((double) 0.318309886f)
#define D3DXToRadian( degree ) ((degree) * (D3DX_PI / 180.0f))
#define D3DXToDegree( radian ) ((radian) * (180.0f / D3DX_PI))
class Vector
{
public:
double x,y,z;
Vector(){x=y=z=0;};
Vector(double _x,double _y,double _z){x=_x;y=_y;z=_z;};
void operator =(const Vector &v) {x=v.x;y=v.y;z=v.z;}
bool operator == (const Vector &v)const {if(x==v.x&&y==v.y&&z==v.z)return true;else return false;}
bool operator > (const Vector &v)const {return !(*this>v) && !(*this==v) ;}
bool operator < (const Vector &v)const { return x != v.x ? x < v.x : y != v.y ? y < v.y : z < v.z; }
bool operator != (const Vector &v)const {if(x!=v.x||y!=v.y||z!=v.z)return true;else return false;}
Vector operator +(const Vector &v)const {return Vector(x+v.x,y+v.y,z+v.z);}
void operator += (const Vector &v) {x+=v.x;y+=v.y;z+=v.z;}
Vector operator - (const Vector &v)const {return Vector(x-v.x,y-v.y,z-v.z);}
void operator -= (const Vector &v) {x-=v.x;y-=v.y;z-=v.z;}
Vector operator * (const Vector &v)const {return Vector(y*v.z-z*v.y , z*v.x-x*v.z , x*v.y-y*v.x);}
void operator *= (const Vector &v) {Vector vv =Vector(y*v.z-z*v.y , z*v.x-x*v.z , x*v.y-y*v.x);
x=vv.x;y=vv.y;z=vv.z;}
Vector operator * (const double i)const {return Vector((double)(x*i),(double)(y*i),(double)(z*i));}
void operator *= (const double i) {x=(double)(x*i);y=(double)(y*i);z=(double)(z*i);}
Vector operator / (const double i)const {return Vector((double)(x/i),(double)(y/i),(double)(z/i));}
void operator /= (const double i) {x=(double)(x/i);y=(double)(y/i);z=(double)(z/i);}
Vector operator / (const Vector &v)const {return Vector( x/v.x , y/v.y , z/v.z );}
void operator /= (const Vector &v) {x/=v.x; y/=v.y; z/=v.z;}
double operator ^(const Vector &v)const {return x*v.x+y*v.y+z*v.z;}
double dot(const Vector &v)const {return x*v.x+y*v.y+z*v.z;}
double GetRad()const {return atan2((double)y,(double)x);};
Vector GetRadVec()const {return Vector(atan2((double)y,(double)z),
atan2((double)z,(double)x),atan2((double)y,(double)x));};
double GetRad(Vector &v)const {return (double)( acos( ((*this).Normal()^v.Normal()) ) );};
double GetLarge()const{return sqrt( (double)(x*x+y*y+z*z) );};
Vector Normal()const {double size = GetLarge();if(size==0)return Vector();return Vector( x/size, y/size , z/size );};
Vector Rotate(Vector vv, double rx,double ry,double rz) {
Vector v;
Vector tmp = *this-vv;
Vector cosv = Vector( cos(D3DXToRadian(rx)),cos(D3DXToRadian(ry)),cos(D3DXToRadian(rz)) );
Vector sinv = Vector( sin(D3DXToRadian(rx)),sin(D3DXToRadian(ry)),sin(D3DXToRadian(rz)) );
//X回n転]
v.x = tmp.x;
v.y = tmp.y*cosv.x-tmp.z*sinv.x;
v.z = tmp.y*sinv.x+tmp.z*cosv.x;
tmp = v;
//Y回n転]
v.x = tmp.x*cosv.y + tmp.z*sinv.y;
v.y = tmp.y;
v.z = -tmp.x*sinv.y + tmp.z*cosv.y;
tmp=v;
//Z回n転]
v.x = tmp.x*cosv.z - tmp.y*sinv.z;
v.y = tmp.x*sinv.z + tmp.y*cosv.z;
v.z = tmp.z;
return v;
}
bool Move(Vector &v,Vector s){
Vector vv =Vector(v.x-x,v.y-y,v.z-z);
if( abs(vv.GetLarge()) < s.GetLarge() )
{
*this = v;
return true;
}
else
{
*this+=(vv.Normal()*s.GetLarge());
}
return false;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment