forked from KarthikRIyer/MDG-CG-lecture
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vector_math.h
34 lines (34 loc) · 1.13 KB
/
vector_math.h
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
class Vector2 { public: float x,y; };
class Vector3 {
public:
float x,y,z;
Vector3 operator + (Vector3 v) const
{ return Vector3(x + v.x, y + v.y, z + v.z); }
Vector3 operator - (Vector3 v) const
{ return Vector3(x - v.x, y - v.y, z - v.z); }
Vector3 operator * (float f) const
{ return Vector3(x*f,y*f,z*f); }
Vector3 operator - () const
{ return Vector3(-x,-y,-z); }
Vector3 operator / (float f) const
{ return Vector3(x/f,y/f,z/f); }
Vector3(float a, float b,float c):x(a),y(b),z(c){}
Vector3(){}
Vector3 direction(){
return Vector3(x/sqrt(x*x+y*y+z*z),y/sqrt(x*x+y*y+z*z),z/sqrt(x*x+y*y+z*z));
}
Vector3 cross(Vector3 v){
return Vector3(y * v.z - z * v.y, z * v.x - x * v.z, x * v.y - y * v.x);
}
float dot(Vector3 v){
return (x * v.x + y * v.y + z * v.z);
}
float length(){
return (sqrt(x*x+y*y+z*z));
}
static Vector3 unitY(){return Vector3(0.0f,1.0f,0.0f);}
static Vector3 unitX(){return Vector3(1.0f,0.0f,0.0f);}
static Vector3 unitZ(){return Vector3(0.0f,0.0f,1.0f);}
};
typedef Vector2 Point2;
typedef Vector3 Point3;