-
Notifications
You must be signed in to change notification settings - Fork 2
/
vector.h
84 lines (74 loc) · 2.05 KB
/
vector.h
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
// $Revision: 1.1 $
// vector.h
// (c) 2004-2022 Matthew Arcus
// MIT License
#if !defined VECTOR_H
#define VECTOR_H
#include <iosfwd>
#include <math.h>
double radians(double deg) {
return deg * M_PI / 180;
}
double degrees(double rad) {
return rad * 180 / M_PI;
}
class spherical {
public:
double phi;
double lambda;
spherical(double phi_, double lambda_)
: phi(phi_),lambda(lambda_) {}
friend std::ostream &operator<<(std::ostream &s, const spherical &p) {
s << "[" << degrees(p.phi) << ", " << degrees(p.lambda) << "]";
return s;
}
};
class vector {
public:
double x;
double y;
double z;
vector(double x_, double y_, double z_) : x(x_),y(y_),z(z_) {};
vector(double phi, double lambda)
: x(cos(lambda)*cos(phi)),y(sin(lambda)*cos(phi)),z(sin(phi)) {}
friend double operator* (const vector &v1, const vector &v2) {
return v1.x * v2.x + v1.y * v2.y + v1.z * v2.z;
}
friend vector operator^ (const vector &v1, const vector &v2) {
return vector(v1.y * v2.z - v1.z * v2.y,
v1.z * v2.x - v1.x * v2.z,
v1.x * v2.y - v1.y * v2.x);
}
friend vector operator+ (const vector &v1, const vector &v2) {
return vector(v1.x + v2.x, v1.y + v2.y, v1.z + v2.z);
}
friend vector operator- (const vector &v1, const vector &v2) {
return vector(v1.x - v2.x, v1.y - v2.y, v1.z - v2.z);
}
friend vector operator *(double d, const vector &v) {
return vector(v.x * d, v.y *d, v.z * d);
}
friend vector operator *(const vector &v, double d) {
return vector(v.x * d, v.y *d, v.z * d);
}
friend vector operator /(const vector &v, double d) {
return vector(v.x/d, v.y/d, v.z/d);
}
friend vector operator -(const vector &v) {
return -1 * v;
}
friend std::ostream &operator<<(std::ostream &s, const vector &v) {
s << "[" << v.x << ", " << v.y << ", " << v.z << "]";
return s;
}
friend double length(const vector &v) {
return sqrt(v * v);
}
friend vector norm(const vector &v) {
return v/length(v);
}
operator spherical() {
return spherical(asin(z),atan2(y,x));
}
};
#endif