-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvec.hpp
More file actions
160 lines (139 loc) · 4.57 KB
/
vec.hpp
File metadata and controls
160 lines (139 loc) · 4.57 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
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#ifndef VEC_H
#define VEC_H
#include <cmath>
struct Vec2 {
public:
double x;
double y;
Vec2(): x(0), y(0) {};
Vec2(double a, double b): x(a), y(b) {};
inline int xi() const { return static_cast<int>(x); }
inline int yi() const { return static_cast<int>(y); }
Vec2& operator+=(const Vec2& v) {
x += v.x;
y += v.y;
return *this;
};
Vec2& operator+=(double t) {
x += t;
y += t;
return *this;
};
Vec2& operator-=(const Vec2& v) {
x -= v.x;
y -= v.y;
return *this;
};
Vec2& operator*=(double t) {
x *= t;
y *= t;
return *this;
}
double len() const { return std::sqrt((x * x) + (y * y)); }
// void rotate(double angle) {
// x = x * cos(angle) - y * sin(angle);
// y = x * sin(angle) + y * cos(angle);
// }
};
inline Vec2 operator+(const Vec2& a, const Vec2& b) { return { a.x + b.x, a.y + b.y }; }
inline Vec2 operator-(const Vec2& a, const Vec2& b) { return { a.x - b.x, a.y - b.y }; }
inline Vec2 operator*(const Vec2& a, double b) { return { a.x * b, a.y * b }; };
inline bool operator==(const Vec2&a, const Vec2& b) {
return a.x == b.x && a.y == b.y;
}
inline Vec2 normalized(const Vec2& v) { return v * (1/v.len()); }
inline double dot(const Vec2& a, const Vec2& b) { return (a.x * b.x) + (a.y * b.y); }
inline double cross(const Vec2& a, const Vec2& b) { return (a.x * b.y) - (a.y * b.x); }
inline Vec2 perpendicular(const Vec2& v) { return { v.y, -v.x }; }
inline Vec2 rotate(const Vec2& v, double angle) {
return {
v.x * cos(angle) - v.y * sin(angle),
v.x * sin(angle) + v.y * cos(angle)
};
}
struct Vec3 {
public:
double x;
double y;
double z;
Vec3(): x(0), y(0), z(0) {};
Vec3(double a, double b, double c): x(a), y(b), z(c) {};
inline int xi() const { return static_cast<int>(x); }
inline int yi() const { return static_cast<int>(y); }
inline int zi() const { return static_cast<int>(z); }
Vec3& operator+=(const Vec3& v) {
x += v.x;
y += v.y;
z += v.z;
return *this;
};
Vec3& operator+=(double t) {
x += t;
y += t;
z += t;
return *this;
};
Vec3& operator-=(const Vec3& v) {
x -= v.x;
y -= v.y;
z -= v.z;
return *this;
};
Vec3& operator*=(double t) {
x *= t;
y *= t;
z *= t;
return *this;
}
double len() const { return std::sqrt((x * x) + (y * y) + (z * z)); }
// none of these functions work properly...
// void rotate_axis_x(double angle) {
// y = y * cos(angle) - z * sin(angle);
// z = y * sin(angle) + z * cos(angle);
// };
// void rotate_axis_y(double angle) {
// x = x * cos(angle) - z * sin(angle);
// z = x * sin(angle) + z * cos(angle);
// }
// void rotate_axis_z(double angle) {
// x = x * cos(angle) - y * sin(angle),
// y = x * sin(angle) + y * cos(angle),
// }
};
inline Vec3 operator+(const Vec3& a, const Vec3& b) { return { a.x + b.x, a.y + b.y, a.z + b.z }; }
inline Vec3 operator-(const Vec3& a, const Vec3& b) { return { a.x - b.x, a.y - b.y, a.z - b.z }; }
inline Vec3 operator*(const Vec3& a, double b) { return { a.x * b, a.y * b, a.z * b }; };
inline bool operator==(const Vec3&a, const Vec3& b) {
return a.x == b.x && a.y == b.y && a.z == b.z;
}
inline Vec3 normalized(const Vec3& v) { return v * (1/v.len()); }
inline double dot(const Vec3& a, const Vec3& b) { return (a.x * b.x) + (a.y * b.y) + (a.z * b.z); }
inline Vec3 cross(const Vec3& a, const Vec3& b) {
return {
(a.y*b.z) - (a.z*b.y),
(a.z*b.x) - (a.x*b.z),
(a.x*b.y) - (a.y*b.x)
};
}
inline Vec3 rotate_axis_x(const Vec3& v, double angle) {
return {
v.x,
v.y * cos(angle) - v.z * sin(angle),
v.y * sin(angle) + v.z * cos(angle),
};
}
inline Vec3 rotate_axis_y(const Vec3& v, double angle) {
return {
v.x * cos(angle) - v.z * sin(angle),
v.y,
v.x * sin(angle) + v.z * cos(angle),
};
}
inline Vec3 rotate_axis_z(const Vec3& v, double angle) {
return {
v.x * cos(angle) - v.y * sin(angle),
v.x * sin(angle) + v.y * cos(angle),
v.z
};
}
#endif