-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpoint.cpp
68 lines (66 loc) · 3.35 KB
/
point.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
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
constexpr double EPS = 1e-9;
template <typename T> bool eq(const T a, const T b) {
if constexpr (is_floating_point_v<T>) return fabs(a - b) <= EPS;
else return a == b;
}
template<typename T=long double> struct Point {
T x, y;
Point() : x(0), y(0) {}
Point(T x_, T y_) : x(x_), y(y_) {}
template <typename U> operator Point<U>() { return {(U)x, (U)y}; }
Point operator+(const Point &p) const { return Point(x + p.x, y + p.y); }
Point operator-(const Point &p) const { return Point(x - p.x, y - p.y); }
Point operator+(const T &k) const { return Point(x + k, y + k); }
Point operator-(const T &k) const { return Point(x - k, y - k); }
Point operator*(const T &k) const { return Point(x * k, y * k); }
Point operator/(const T &k) const { return Point(x / k, y / k); }
Point& operator+=(const Point &p) { x += p.x, y += p.y; return *this; }
Point& operator-=(const Point &p) { x -= p.x, y -= p.y; return *this; }
Point& operator+=(const T &k) { x += k, y += k; return *this; }
Point& operator-=(const T &k) { x -= k, y -= k; return *this; }
Point& operator*=(const T &k) { x *= k, y *= k; return *this; }
Point& operator/=(const T &k) { x /= k, y /= k; return *this; }
bool operator==(const Point &p) const { return eq(x, p.x) and eq(y, p.y); }
bool operator<(const Point &p) const { return eq(x, p.x) ? y < p.y : x < p.x; }
bool operator>(const Point &p) const { return eq(x, p.x) ? y > p.y : x > p.x; }
bool operator<=(const Point &p) const { return *this == p or *this < p; }
bool operator>=(const Point &p) const { return *this == p or *this > p; }
friend ostream& operator<<(ostream &os, const Point &p) { return os << p.x << ' ' << p.y; }
friend istream& operator>>(istream &is, Point &p) { return is >> p.x >> p.y; }
template<typename U> void rotate(U rad) {
tie(x, y) = make_pair(x * cos(rad) - y * sin(rad), x * sin(rad) + y * cos(rad));
}
template<typename U> Point<U> rotated(U rad) const {
return Point<U>(x * cos(rad) - y * sin(rad), x * sin(rad) + y * cos(rad));
}
T dot(const Point &p) const { return x * p.x + y * p.y; }
T cross(const Point &p) const { return x * p.y - y * p.x; }
T cross(const Point &a, const Point &b) const { return (a - *this).cross(b - *this); }
T dist2() const { return x * x + y * y; }
T dist2(const Point &p) const { return to(p).dist2(); }
long double dist() const { return hypot(x, y); }
long double dist(const Point &p) const { return to(p).dist(); }
long double angle() const { return atan2(y, x); }
long double angle(const Point &p) const { return atan2(cross(p), dot(p)); }
long double norm() const { return sqrt(dot(*this)); }
Point rot90() const { return Point(-y, x); }
Point to(const Point &p) const { return p - *this; }
};
template <typename T> bool polar_less(const Point<T> &a, const Point<T> &b) {
constexpr auto top = [](const Point<T> &p) { return p.y < 0 or (!p.y and p.x < 0); };
auto ta = top(a);
auto tb = top(b);
if (ta != tb) return ta;
return a.cross(b) > 0;
}
// ang(a1,b1) <= ang(a2,b2)
template <typename T>
bool angle_less(const Point<T> &a1, const Point<T> &b1, const Point<T> &a2, const Point<T> &b2) {
Point<T> p1(a1.dot(b1), abs(a1.cross(b1)));
Point<T> p2(a2.dot(b2), abs(a2.cross(b2)));
return p1.cross(p2) <= 0;
}
template <typename T>
T projection_len(const Point<T> &p, const Point<T> &a, const Point<T> &b) {
return (p-a).dot(b-a) / (b-a).norm();
}