-
Notifications
You must be signed in to change notification settings - Fork 1
/
vec2.h
223 lines (165 loc) · 5.64 KB
/
vec2.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
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
#ifndef _VEC2_H_
#define _VEC2_H_
#include <cmath>
#include <iostream>
using namespace std;
#include "common.h"
#include "random.h"
namespace Renzoku {
class Vec2 {
public:
Vec2();
Vec2(Float v);
Vec2(Float e0, Float e1);
Vec2(const Vec2 &v) { *this = v; }
Float x() const { return e[0]; }
Float y() const { return e[1]; }
const Vec2& operator+() const;
Vec2 operator-() const;
Float operator[](int i) const { return e[i]; }
Float& operator[](int i) { return e[i]; }
Float length() const;
Float squared_length() const;
void make_unit_vector();
void set_x(Float _x) { e[0] = _x; }
void set_y(Float _y) { e[1] = _y; }
Float min_component() const;
Float max_component() const;
Float min_abs_component() const;
Float max_abs_component() const;
int arg_min_component() const;
int arg_max_component() const;
int arg_min_abs_component() const;
int arg_max_abs_component() const;
friend bool operator==(const Vec2 &v1, const Vec2 &v2);
friend bool operator!=(const Vec2 &v1, const Vec2 &v2);
friend istream& operator>>(istream &is, Vec2 &t);
friend ostream& operator<<(ostream &os, const Vec2 &t);
friend Vec2 operator+(const Vec2 &v1, const Vec2 &v2);
friend Vec2 operator-(const Vec2 &v1, const Vec2 &v2);
friend Vec2 operator*(const Vec2 &v1, const Vec2 &v2);
friend Vec2 operator/(const Vec2 &v1, const Vec2 &v2);
friend Vec2 operator-(const Vec2 &v, Float t);
friend Vec2 operator-(Float t, const Vec2 &v);
friend Vec2 operator/(const Vec2 &v, Float t);
friend Vec2 operator*(const Vec2 &v, Float t);
friend Vec2 operator*(Float scalar, const Vec2 &v);
Vec2& operator=(const Vec2 &v);
Vec2& operator+=(const Vec2 &v);
Vec2& operator-=(const Vec2 &v);
Vec2& operator*=(const Float t);
Vec2& operator/=(const Float t);
friend Vec2 unit_vector(const Vec2 &v);
friend Vec2 min(const Vec2 &v1, const Vec2 &v2);
friend Vec2 max(const Vec2 &v1, const Vec2 &v2);
friend Float dot(const Vec2 &v1, const Vec2 &v2);
void random(Random &rd) { e[0] = rd(); e[1] = rd(); }
public:
Float e[2];
};
inline Vec2::Vec2() {
e[0] = 0;
e[1] = 0;
}
inline Vec2::Vec2(Float v) {
e[0] = e[1] = v;
}
inline Vec2::Vec2(Float e0, Float e1) {
e[0] = e0; e[1] = e1; }
inline const Vec2& Vec2::operator+() const { return *this; }
inline Vec2 Vec2::operator-() const { return Vec2(-e[0], -e[1]); }
inline Float Vec2::length() const {
return sqrt(e[0]*e[0] + e[1]*e[1]); }
inline Float Vec2::squared_length() const {
return e[0]*e[0] + e[1]*e[1]; }
inline void Vec2::make_unit_vector() {
*this = *this / (*this).length(); }
inline Float Vec2::min_component() const {
Float tmp = e[0];
if (e[1] < tmp) tmp = e[1];
return tmp;
}
inline Float Vec2::max_component() const {
Float tmp = e[0];
if (e[1] > tmp) tmp = e[1];
return tmp;
}
inline Float Vec2::min_abs_component() const {
Float tmp = fabs(e[0]);
if (fabs(e[1]) < tmp) tmp = fabs(e[1]);
return tmp;
}
inline Float Vec2::max_abs_component() const {
Float tmp = fabs(e[0]);
if (fabs(e[1]) > tmp) tmp = fabs(e[1]);
return tmp;
}
inline int Vec2::arg_min_component() const {
return (e[0] < e[1]) ? 0 : 1;
}
inline int Vec2::arg_max_component() const {
return (e[0] > e[1]) ? 0 : 1;
}
inline int Vec2::arg_min_abs_component() const {
return (fabs(e[0]) < fabs(e[1])) ? 0 : 1;
}
inline int Vec2::arg_max_abs_component() const {
return (fabs(e[0]) > fabs(e[1])) ? 0 : 1;
}
inline bool operator==(const Vec2 &v1, const Vec2 &v2) {
if (v1.e[0] != v2.e[0]) return false;
if (v1.e[1] != v2.e[1]) return false;
return true;
}
inline bool operator!=(const Vec2 &v1, const Vec2 &v2) {
return !(v1 == v2); }
inline Vec2 operator+(const Vec2 &v1, const Vec2 &v2) {
return Vec2(v1.e[0] + v2.e[0], v1.e[1] + v2.e[1]);
}
inline Vec2 operator-(const Vec2 &v1, const Vec2 &v2) {
return Vec2(v1.e[0] - v2.e[0], v1.e[1] - v2.e[1]);
}
inline Vec2 operator*(const Vec2 &v1, const Vec2 &v2) {
return Vec2(v1.e[0] * v2.e[0], v1.e[1] * v2.e[1]);
}
inline Vec2 operator/(const Vec2 &v1, const Vec2 &v2) {
return Vec2(v1.e[0] / v2.e[0], v1.e[1] / v2.e[1]);
}
inline Vec2 operator-(const Vec2 &v, Float t) {
return Vec2(v.e[0] - t, v.e[1] - t); }
inline Vec2 operator-(Float t, const Vec2 &v) {
return Vec2(v.e[0] - t, v.e[1] - t); }
inline Vec2 operator/(const Vec2 &v, Float t) {
return Vec2(v.e[0]/t, v.e[1]/t); }
inline Vec2 operator*(const Vec2 &v, Float t) {
return Vec2(v.e[0]*t, v.e[1]*t); }
inline Vec2 operator*(Float t, const Vec2 &v) {
return Vec2(v.e[0]*t, v.e[1]*t); }
inline Vec2& Vec2::operator=(const Vec2 &v) {
e[0] = v.e[0]; e[1] = v.e[1]; return *this; }
inline Vec2& Vec2::operator+=(const Vec2 &v) {
*this = *this + v; return *this; }
inline Vec2& Vec2::operator-=(const Vec2 &v) {
*this = *this - v; return *this; }
inline Vec2& Vec2::operator*=(Float t) {
*this = *this * t; return *this; }
inline Vec2& Vec2::operator/=(Float t) {
*this = *this / t; return *this; }
inline Float dot(const Vec2 &v1, const Vec2 &v2) {
return v1.e[0]*v2.e[0] + v1.e[1]*v2.e[1]; }
inline Vec2 unit_vector(const Vec2 &v) {
return v / v.length(); }
inline Vec2 min(const Vec2 &v1, const Vec2 &v2) {
Vec2 tmp(v1);
if (v2.x() < v1.x()) tmp.set_x(v2.x());
if (v2.y() < v1.y()) tmp.set_y(v2.y());
return tmp;
}
inline Vec2 max(const Vec2 &v1, const Vec2 &v2) {
Vec2 tmp(v1);
if (v2.x() > v1.x()) tmp.set_x(v2.x());
if (v2.y() > v1.y()) tmp.set_y(v2.y());
return tmp;
}
} // end namespace Renzoku
#endif