-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvec3.cpp
87 lines (69 loc) · 1.59 KB
/
vec3.cpp
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
#include "vec3.h"
#include <math.h>
vec3::vec3()
:x(0), y(0), z(0)
{}
vec3::vec3(double value)
:x(value), y(value), z(value)
{}
vec3::vec3(double x_, double y_, double z_)
:x(x_), y(y_), z(z_)
{}
vec3::vec3(const vec3& other)
{
x = other.x; y = other.y; z = other.z;
}
vec3 vec3::operator+(const vec3& other)
{
return vec3(x + other.x, y + other.y, z + other.z);
}
vec3 vec3::operator-(const vec3& other)
{
return vec3(x - other.x, y - other.y, z - other.z);
}
vec3 vec3::operator*(double t)
{
return vec3(x * t, y * t, z * t);
}
vec3 vec3::operator/(double t)
{
return vec3(x / t, y / t, z / t);
}
double vec3::dot(const vec3& v1, const vec3& v2)
{
return v1.x * v2.x + v1.y * v2.y + v1.z * v2.z;
}
double vec3::squared_length() {
return x * x + y * y + z * z;
}
double vec3::length()
{
return sqrt(x * x + y * y + z * z);
}
vec3 vec3::normalize()
{
double temp = 1.0 / sqrt(x * x + y * y + z * z);
return vec3(x * temp, y * temp, z * temp);
}
vec3 vec3::cross(const vec3& v1, const vec3& v2)
{
return vec3((v1.y * v2.z - v1.z * v2.y),
(v1.z * v2.x - v1.x * v2.z),
(v1.x * v2.y - v1.y * v2.x));
}
vec3 operator+(const vec3& v1, const vec3& v2)
{
return vec3(v1.x + v2.x,
v1.y + v2.y,
v1.z + v2.z);
}
vec3 operator*(const vec3& v1, const vec3& v2)
{
return vec3(v1.x * v2.x,
v1.y * v2.y,
v1.z * v2.z);
}
std::ostream& operator<<(std::ostream& stream, const vec3& v)
{
return stream << "x: " << v.x << " y: " << v.y << " z: " << v.z;
}