-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathvec.hh
More file actions
61 lines (55 loc) · 1.36 KB
/
vec.hh
File metadata and controls
61 lines (55 loc) · 1.36 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
#ifndef VEC_HH
#define VEC_HH
#include <cmath>
struct vec {
public:
double x,y;
vec() {}
vec(double x_) : x(x_),y(x_) {}
vec(double x_,double y_) : x(x_),y(y_) {}
inline vec operator+ (vec p) {
return vec(x+p.x,y+p.y);
}
inline vec operator- (vec p) {
return vec(x-p.x,y-p.y);
}
inline vec operator* (double a) {
return vec(x*a,y*a);
}
inline vec operator/ (double a) {
return vec(x/a,y/a);
}
inline void operator+= (vec p) {
x+=p.x;y+=p.y;
}
inline void operator-= (vec p) {
x-=p.x;y-=p.y;
}
inline void operator*= (double a) {
x*=a;y*=a;
}
inline void operator/= (double a) {
x/=a;y/=a;
}
inline operator double() {
return double(x*x+y*y);
}
inline double magnitude() {
return sqrt(x*x+y*y);
}
inline void normalize() {
double mag=x*x+y*y;
if(mag>0) {
mag=1./sqrt(mag);
x*=mag;y*=mag;
}
}
};
inline vec operator*(const double e,vec f) {
return vec(e*f.x,e*f.y);
}
inline vec operator-(vec f) {
return vec(-f.x,-f.y);
}
inline double mod_sq(vec a) {return a.x*a.x+a.y*a.y;}
#endif