-
Notifications
You must be signed in to change notification settings - Fork 0
/
vector.cpp
127 lines (111 loc) · 2.26 KB
/
vector.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#include "vector.h"
double Vec::AngleX() {
double a = atan2(x[1],x[0]) / 3.141593;
if (a < 0.0)
a += 2.0;
return a;
}
double Vec::AngleY() {
Vec z(0.0,0.0,1.0);
double a = asin(fabs(dot(z))/(magnitude())) / 3.141593 + 0.5;
if (x[2] < 0.0)
a = 1.0 - a;
return a;
}
double& Vec::operator[] (int i) { return x[i]; }
Vec::Vec() {
x[0] = x[1] = x[2] = 0.0;
}
Vec::Vec(double a, double b, double c) {
x[0] = a;
x[1] = b;
x[2] = c;
}
void Vec::set(double a, double b, double c) {
x[0] = a;
x[1] = b;
x[2] = c;
}
double Vec::magnitude() {
return sqrt(x[0] * x[0] + x[1] * x[1] + x[2] * x[2]);
}
void Vec::normalize() {
double m = magnitude();
x[0] /= m;
x[1] /= m;
x[2] /= m;
}
Vec Vec::GetNormalized() {
Vec r = *this;
double m = r.magnitude();
r[0] /= m;
r[1] /= m;
r[2] /= m;
return r;
}
Vec Vec::cross(Vec v) {
Vec c;
c.x[0] = x[1] * v.x[2] - x[2] * v.x[1];
c.x[1] = x[2] * v.x[0] - x[0] * v.x[2];
c.x[2] = x[0] * v.x[1] - x[1] * v.x[0];
return c;
}
Vec Vec::blend(Vec v, double f) {
Vec c;
for (int i = 0; i < 3; i++) {
c.x[i] = pow(pow(x[i],f)+pow(v.x[i],f),(1.0/f));
}
return c;// / 2.0;
}
double Vec::dot(Vec v) {
double prod = x[0] * v.x[0] + x[1] * v.x[1] + x[2] * v.x[2];
return prod;
}
Vec::Vec(const Vec &v) {
x[0] = v.x[0];
x[1] = v.x[1];
x[2] = v.x[2];
}
Vec& Vec::operator=(const Vec &v) {
x[0] = v.x[0];
x[1] = v.x[1];
x[2] = v.x[2];
return *this;
}
Vec Vec::operator+(const Vec &v) {
Vec c;
c.x[0] = x[0] + v.x[0];
c.x[1] = x[1] + v.x[1];
c.x[2] = x[2] + v.x[2];
return c;
}
Vec Vec::operator-(const Vec &v) {
Vec c;
c.x[0] = x[0] - v.x[0];
c.x[1] = x[1] - v.x[1];
c.x[2] = x[2] - v.x[2];
return c;
}
Vec Vec::operator*(const double &f) {
Vec c;
c.x[0] = x[0] * f;
c.x[1] = x[1] * f;
c.x[2] = x[2] * f;
return c;
}
Vec Vec::operator/(const double &f) {
Vec c;
c.x[0] = x[0] / f;
c.x[1] = x[1] / f;
c.x[2] = x[2] / f;
return c;
}
Vec Vec::operator*=(const double &f) {
x[0] = x[0] * f;
x[1] = x[1] * f;
x[2] = x[2] * f;
return *this;
}
void Vec::print() {
printf("{%f,%f,%f}\n",x[0],x[1],x[2]);
}