-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpoint.hpp
40 lines (34 loc) · 1.65 KB
/
point.hpp
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
struct Point {
long long x, y;
Point() = default;
constexpr Point(long long x_, long long y_) : x(x_), y(y_) {}
constexpr bool operator==(const Point &p) const { return x == p.x && y == p.y; }
constexpr bool operator<(const Point &p) const {
if (x != p.x) return x < p.x;
return y < p.y;
}
constexpr Point operator+() const { return *this; }
constexpr Point operator-() const { return {-x, -y}; }
constexpr Point operator+(const Point &p) const { return {x + p.x, y + p.y}; }
constexpr Point operator-(const Point &p) const { return {x - p.x, y - p.y}; }
constexpr Point operator*(long long s) const { return {x * s, y * s}; }
constexpr Point &operator+=(const Point &p) {
x += p.x;
y += p.y;
return *this;
}
constexpr Point &operator-=(const Point &p) {
x -= p.x;
y -= p.y;
return *this;
}
constexpr long long dot(const Point &p) const { return x * p.x + y * p.y; }
constexpr long long cross(const Point &p) const { return x * p.y - y * p.x; }
constexpr long long lengthSq() const { return x * x + y * y; }
double length() const { return hypot(x, y); }
constexpr long long distanceSq(const Point &p) const { return (*this - p).lengthSq(); }
double distance(const Point &p) const { return (*this - p).length(); }
friend constexpr Point operator*(long long s, const Point &p) { return p * s; }
friend istream &operator>>(istream &is, Point &p) { return is >> p.x >> p.y; }
friend ostream &operator<<(ostream &os, const Point &p) { return os << "(" << p.x << "," << p.y << ")"; }
};