-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathvector.cpp
executable file
·36 lines (36 loc) · 1.33 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
// Vector
// https://youtu.be/UWbGRhF3Ozw?t=9564
struct V {
double x, y;
V(double x=0, double y=0): x(x), y(y) {}
V& operator+=(const V& v) { x += v.x; y += v.y; return *this;}
V operator+(const V& v) const { return V(*this) += v;}
V& operator-=(const V& v) { x -= v.x; y -= v.y; return *this;}
V operator-(const V& v) const { return V(*this) -= v;}
V& operator*=(double s) { x *= s; y *= s; return *this;}
V operator*(double s) const { return V(*this) *= s;}
V& operator/=(double s) { x /= s; y /= s; return *this;}
V operator/(double s) const { return V(*this) /= s;}
double dot(const V& v) const { return x*v.x + y*v.y;}
double cross(const V& v) const { return x*v.y - v.x*y;}
double norm2() const { return x*x + y*y;}
double norm() const { return sqrt(norm2());}
V normalize() const { return *this/norm();}
V rotate90() const { return V(y, -x);}
int ort() const { // orthant
if (abs(x) < eps && abs(y) < eps) return 0;
if (y > 0) return x>0 ? 1 : 2;
else return x>0 ? 4 : 3;
}
bool operator<(const V& v) const {
int o = ort(), vo = v.ort();
if (o != vo) return o < vo;
return cross(v) > 0;
}
};
istream& operator>>(istream& is, V& v) {
is >> v.x >> v.y; return is;
}
ostream& operator<<(ostream& os, const V& v) {
os<<"("<<v.x<<","<<v.y<<")"; return os;
}