Skip to content

Commit a28cbf7

Browse files
author
atcoder-live
committed
geometry - Vector
1 parent 42ebc02 commit a28cbf7

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

geom.cpp

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// geometry
2+
// https://youtu.be/UWbGRhF3Ozw?t=9564
3+
const double eps = 1e-9;
4+
struct V {
5+
double x, y;
6+
V(double x=0, double y=0): x(x), y(y) {}
7+
V& operator+=(const V& v) { x += v.x; y += v.y; return *this;}
8+
V operator+(const V& v) const { return V(*this) += v;}
9+
V& operator-=(const V& v) { x -= v.x; y -= v.y; return *this;}
10+
V operator-(const V& v) const { return V(*this) -= v;}
11+
V& operator*=(double s) { x *= s; y *= s; return *this;}
12+
V operator*(double s) const { return V(*this) *= s;}
13+
V& operator/=(double s) { x /= s; y /= s; return *this;}
14+
V operator/(double s) const { return V(*this) /= s;}
15+
double dot(const V& v) const { return x*v.x + y*v.y;}
16+
double cross(const V& v) const { return x*v.y - v.x*y;}
17+
double norm2() const { return x*x + y*y;}
18+
double norm() const { return sqrt(norm2());}
19+
20+
int ort() const { // orthant
21+
if (abs(x) < eps && abs(y) < eps) return 0;
22+
if (y > 0) return x>0 ? 1 : 2;
23+
else return x>0 ? 4 : 3;
24+
}
25+
bool operator<(const V& v) const {
26+
int o = ort(), vo = v.ort();
27+
if (o != vo) return o < vo;
28+
return cross(v) > 0;
29+
}
30+
};
31+
istream& operator>>(istream& is, V& v) {
32+
is >> v.x >> v.y; return is;
33+
}
34+
ostream& operator<<(ostream& os, const V& v) {
35+
os<<"("<<v.x<<","<<v.y<<")"; return os;
36+
}

0 commit comments

Comments
 (0)