-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvector3f.cpp
More file actions
64 lines (51 loc) · 1.38 KB
/
vector3f.cpp
File metadata and controls
64 lines (51 loc) · 1.38 KB
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
#include <cmath>
#include "vector3f.h"
Vector3f::Vector3f(float x_, float y_, float z_) {
x = x_;
y = y_;
z = z_;
}
Vector3f & Vector3f::operator+=(Vector3f v) {
return *this = Vector3f(x+v.x, y+v.y, z+v.z);
}
Vector3f & Vector3f::operator-=(Vector3f v) {
return *this += (-1.)*v;
}
Vector3f & Vector3f::operator*=(float f) {
return *this = Vector3f(x*f, y*f, z*f);
}
Vector3f & Vector3f::operator/=(float f) {
if (f<1e-5) {
throw std::runtime_error("Division by zero");
}
return *this *= 1./f;
}
float norm (const Vector3f &v) {
return sqrt(pow(v.x,2)+pow(v.y,2)+pow(v.z,2));
}
float dot_product (const Vector3f &v1, const Vector3f &v2) {
return v1.x*v2.x + v1.y*v2.y + v1.z*v2.z;
}
Vector3f cross_product (const Vector3f &v1, const Vector3f &v2) {
Vector3f v3;
v3.x = v1.y * v2.z - v1.z * v2.y;
v3.y = v1.z * v2.x - v1.x * v2.z;
v3.z = v1.x * v2.y - v1.y * v2.x;
return v3;
}
Vector3f operator+ (const Vector3f &v1, const Vector3f &v2) {
return Vector3f(v1)+=v2;
}
Vector3f operator- (const Vector3f &v1, const Vector3f &v2) {
return Vector3f(v1)-=v2;
}
Vector3f operator* (const float &f, const Vector3f &v) {
return Vector3f(v)*=f;
}
Vector3f operator/ (const Vector3f &v, const float &f) {
return Vector3f(v)/=f;
}
std::ostream & operator<< (std::ostream &st, const Vector3f &v) {
st << "x: " << v.x << ", y: " << v.y << ", z: " << v.z;
return st;
}