This repository has been archived by the owner on Dec 28, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
vector.cc
94 lines (86 loc) · 2.14 KB
/
vector.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include "vector.h"
#include <ostream>
#include <cmath>
#include <sstream>
#include <string>
/* -- vec3f -- */
/*Procedures*/
vec3f vec3f::operator+ (const vec3f& second) const{
vec3f temp;
temp[0] = second[0]+m_vec[0];
temp[1] = second[1]+m_vec[1];
temp[2] = second[2]+m_vec[2];
return temp;
}
vec3f vec3f::operator- (const vec3f& second) const{
vec3f temp;
temp[0] = m_vec[0]-second[0];
temp[1] = m_vec[1]-second[1];
temp[2] = m_vec[2]-second[2];
return temp;
}
vec3f vec3f::operator* (float m) const{
vec3f temp;
temp[0] = m_vec[0]*m;
temp[1] = m_vec[1]*m;
temp[2] = m_vec[2]*m;
return temp;
}
vec3f vec3f::operator* (vec3f v) const{
vec3f temp;
temp[0] = m_vec[0]*v[0];
temp[1] = m_vec[1]*v[1];
temp[2] = m_vec[2]*v[2];
return temp;
}
vec3f& vec3f::operator=(const vec3f& v){
m_vec[0] = v[0];
m_vec[1] = v[1];
m_vec[2] = v[2];
return *this;
}
vec3f vec3f::operator/(float m) const{
vec3f temp;
temp[0] = m_vec[0]/m;
temp[1] = m_vec[1]/m;
temp[2] = m_vec[2]/m;
return temp;
}
float vec3f::operator[](const int i) const{
return m_vec[i];
}
float& vec3f::operator[](const int i){
return m_vec[i];
}
vec3f vec3f::cross(const vec3f& v) const{
vec3f temp;
temp[0] = m_vec[1]*v[2]-m_vec[2]*v[1];
temp[1] = m_vec[2]*v[0]-m_vec[0]*v[2];
temp[2] = m_vec[0]*v[1]-m_vec[1]*v[0];
return temp;
}
float vec3f::dot(const vec3f& v) const{
return m_vec[0]*v[0] + m_vec[1]*v[1] + m_vec[2]*v[2];
}
float vec3f::distance(const vec3f& v) const{
return sqrt(pow(m_vec[0]-v[0], 2)+pow(m_vec[1]-v[1], 2)+pow(m_vec[2]-v[2], 2));
}
void vec3f::normalize(){
float f = this->distance(vec3f());
(*this) = (*this)*(1/f);
}
vec3f vec3f::reflect(const vec3f& v) const{
vec3f temp;
float tmp = 2.0 * (v.dot((*this)));
temp = (*this)*tmp - v;
return temp;
}
std::string vec3f::colorPrint(){
std::ostringstream o;
o << std::max(std::min(static_cast<int>(m_vec[0]*255), 255), 0) << " " << std::max(std::min(static_cast<int>(m_vec[1]*255), 255), 0) << " " << std::max(std::min(static_cast<int>(m_vec[2]*255), 255), 0);
return o.str();
}
std::ostream& operator<<(std::ostream& o, const vec3f &v){
o << "[ " << v[0] << ", " << v[1] << ", " << v[2] << "]";
return o;
}