-
Notifications
You must be signed in to change notification settings - Fork 2
/
vector3d.cc
76 lines (62 loc) · 1.49 KB
/
vector3d.cc
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
#include "vector3d.h"
vector3d vector3d::normalize(double r)
{
return vector3d(x / length() * r, y / length() * r, z / length() * r);
}
double vector3d::length()
{
return sqrt(x*x + y*y + z*z);
}
//CROSS PRODUCT
vector3d operator%(const vector3d& a, const vector3d& b)
{
vector3d vec (a.y*b.z - a.z*b.y, a.z*b.x - a.x*b.z, a.x*b.y - a.y*b.x);
return vec;
}
//Scalar Product
double operator*(const vector3d& a, const vector3d& b)
{
return (a.x * b.x) + (a.y * b.y) + (a.z * b.z);
}
ostream& operator<<(ostream& os, const vector3d& vec)
{
os << "(" << to_string(vec.x) << ", " << to_string(vec.y) << ", " << to_string(vec.z) << ")";
return os;
}
vector3d operator*(const double& a, const vector3d& b)
{
return b*a;
}
vector3d operator*(const vector3d& a, const double& b)
{
vector3d vec (double(a.x * b), double(a.y * b), double(a.z * b));
return vec;
}
vector3d operator-(const vector3d& a, const vector3d& b)
{
return a + (b * (-1));
}
vector3d operator+(const vector3d& a, const vector3d& b)
{
vector3d c(a.x + b.x, a.y + b.y, a.z + b.z);
return c;
}
double vector3d::operator[](int i)
{
assert(!((i > 2) || (i < 0)));
if(i == 0)
return x;
else if(i == 1)
return y;
else if(i == 2)
return z;
}
bool operator==(const vector3d& lhs, const vector3d& rhs)
{
double epsilon = 0.00000001;
return (abs(lhs.x - rhs.x) <= epsilon) && (abs(lhs.y - rhs.y) <= epsilon) && (abs(lhs.z - rhs.z) <= epsilon);
}
bool operator!=(const vector3d& lhs, const vector3d& rhs)
{
return !(lhs==rhs);
}