-
Notifications
You must be signed in to change notification settings - Fork 0
/
Vec.hpp
102 lines (82 loc) · 4.74 KB
/
Vec.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#ifndef VEC_H
#define VEC_H
#include<cmath>
template<class T> struct Vec3{
T x, y, z;
Vec3():x(0), y(0), z(0){}
Vec3(T _x, T _y, T _z):x(_x), y(_y), z(_z){}
Vec3(const Vec3<T> &v){x=v.x; y=v.y; z=v.z;}
void clear(){x=0;y=0;z=0;}
void normalize(){
T temp = distance();
if(temp==0) return;
x/=temp;
y/=temp;
z/=temp;
}
inline double distance()const{return distance(Vec3<T>());}
inline double angle()const{return angle(*this, Vec3<T>());}
template<class G> inline double distance(const Vec3<G> v)const{return sqrt((x-v.x)*(x-v.x)+(y-v.y)*(y-v.y)+(z-v.z)*(z-v.z));}
template<class G> inline double angle(const Vec3<G> v)const{return angle(*this, v);}
template<class G> Vec3<T> operator+(const Vec3<G> v)const{return Vec3<T>(x+v.x, y+v.y, z+v.z);}
template<class G> Vec3<T> operator-(const Vec3<G> v)const{return Vec3<T>(x-v.x, y-v.y, z-v.z);}
template<class G> Vec3<T> operator*(const G d)const{return Vec3<T>(x*d, y*d, z*d);}
template<class G> Vec3<T> operator/(const G d)const{return Vec3<T>(x/d, y/d, z/d);}
template<class G> T operator*(const Vec3<G> v)const{return x*v.x + y*v.y + z*v.z;}
template<class G> Vec3<T> operator^(const Vec3<G> v)const{return Vec3<T>(y*v.z - z*v.y, x*v.z - z*v.x, x*v.y - y*v.x);}
template<class G> Vec3<T> operator=(const Vec3<G> v){x=v.x; y=v.y; z=v.z; return *this;}
template<class G> Vec3<T> operator+=(const Vec3<G> v){x+=v.x; y+=v.y; z+=v.z; return *this;}
template<class G> Vec3<T> operator-=(const Vec3<G> v){x-=v.x; y-=v.y; z-=v.z; return *this;}
template<class G> Vec3<T> operator*=(const G d){x*=d; y*=d; z*=d; return *this;}
template<class G> Vec3<T> operator/=(const G d){x/=d; y/=d; z/=d; return *this;}
template<class G> Vec3<T> operator^=(const Vec3<G> v){return *this=*this^v;}
template<class G> bool operator==(const Vec3<G> v){return x==v.x && y==v.y && z==v.z;}
template<class G> bool operator!=(const Vec3<G> v){return x!=v.x || y!=v.y || z!=v.z;}
template<class G> operator Vec3<G>(){return Vec3<G>(x,y);}
};
template<class T> struct Vec2{
T x, y;
Vec2():x(0), y(0){}
Vec2(T _x, T _y):x(_x), y(_y){}
Vec2(const Vec2 &v){x=v.x; y=v.y;}
void clear(){x=0;y=0;}
void normalize(){
T temp = distance();
if(temp==0) return;
x/=temp;
y/=temp;
}
inline double distance()const{return distance(Vec2<T>());}
inline double angle()const{return angle(*this, Vec2<T>());}
template<class G> T distance(const Vec2<G> v)const{return sqrt((x-v.x)*(x-v.x)+(y-v.y)*(y-v.y));}
template<class G> T angle(const Vec2<G> v){return angle(*this, v);}
template<class G> Vec2<T> operator+(const Vec2<G> v)const{return Vec2(x+v.x, y+v.y);}
template<class G> Vec2<T> operator-(const Vec2<G> v)const{return Vec2(x-v.x, y-v.y);}
template<class G> Vec2<T> operator*(const G d)const{return Vec2(x*d, y*d);}
template<class G> Vec2<T> operator/(const G d)const{return Vec2(x/d, y/d);}
template<class G> T operator*(const Vec2<G> v)const{return x*v.x + y*v.y;}
template<class G> Vec2<T> operator=(const Vec2<G> v){x=v.x; y=v.y; return *this;}
template<class G> Vec2<T> operator+=(const Vec2<G> v){x+=v.x; y+=v.y; return *this;}
template<class G> Vec2<T> operator-=(const Vec2<G> v){x-=v.x; y-=v.y; return *this;}
template<class G> Vec2<T> operator*=(const G d){x*=d; y*=d; return *this;}
template<class G> Vec2<T> operator/=(const G d){x/=d; y/=d; return *this;}
template<class G> bool operator==(const Vec2<G> v){return x==v.x && y==v.y;}
template<class G> bool operator!=(const Vec2<G> v){return x!=v.x || y!=v.y;}
template<class G> operator Vec2<G>(){return Vec2<G>(x,y);}
};
template<class G> Vec3<G> operator-(Vec3<G> v){return v*(-1);}
template<class G> Vec2<G> operator-(Vec2<G> v){return v*(-1);}
template<class G, class N, class M> inline double mixedProduct(const Vec3<G> v, const Vec3<N> u, const Vec3<M> w){return v*(u^w);}
template<class G, class N> inline double distance(const Vec3<G> v, const Vec3<N> u=Vec3<N>()){return sqrt((v.x-u.x)*(v.x-u.x)+(v.y-u.y)*(v.y-u.y)+(v.z-u.z)*(v.z-u.z));}
template<class G, class N> inline double angle(const Vec3<G> v, const Vec3<N> u){return acos((v*u)/(v.distance()*u.distance()));}
template<class G, class N> static double distance(const Vec2<G> v, const Vec2<N> u=Vec2<N>()){return sqrt((v.x-u.x)*(v.x-u.x)+(v.y-u.y)*(v.y-u.y));}
template<class G, class N> static double angle(const Vec2<G> v, const Vec2<N> u){return acos((v*u)/(v.distance()*u.distance()));}
typedef Vec3<float> Vec3f;
typedef Vec3<double> Vec3d;
typedef Vec3<int> Vec3i;
typedef Vec3<long long> Vec3l;
typedef Vec2<float> Vec2f;
typedef Vec2<double> Vec2d;
typedef Vec2<int> Vec2i;
typedef Vec2<long long> Vec2l;
#endif // VEC_H