From 2100b97d4273c2e45533cf9ac93b3adb60daffb6 Mon Sep 17 00:00:00 2001 From: subcrip Date: Sat, 9 Nov 2024 18:58:13 +0800 Subject: [PATCH] Add geometry/point.cc Signed-off-by: subcrip --- geometry/point.cc | 50 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 geometry/point.cc diff --git a/geometry/point.cc b/geometry/point.cc new file mode 100644 index 0000000..2032cd0 --- /dev/null +++ b/geometry/point.cc @@ -0,0 +1,50 @@ +template struct point { + T x, y; + point() : x(), y() {} + point(const pair& a) : x(a.first), y(a.second) {} + point(const T& x, const T& y) : x(x), y(y) {} + + inline T square() const { return x * x + y * y; } + inline ld norm() const { return sqrt(ld(square())); } + + inline point operator+(const point& rhs) const { return point(x + rhs.x, y + rhs.y); } + inline point operator-(const point& rhs) const { return point(x - rhs.x, y - rhs.y); } + inline point operator+() const { return *this; } + inline point operator-() const { return point(-x, -y); } + inline point operator*(const T& a) const { return point(x * a, y * a); } + inline T operator*(const point& rhs) const { return x * rhs.y - y * rhs.x; } + inline point operator/(const T& a) const { return point(x / a, y / a); } + inline point operator+=(const point& rhs) { x += rhs.x, y += rhs.y; return *this; } + inline point operator-=(const point& rhs) { x -= rhs.x, y -= rhs.y; return *this; } + inline point operator*=(const T& a) { x *= a, y *= a; return *this; } + inline point operator/=(const T& a) { x /= a, y /= a; return *this; } + + inline bool operator==(const point& rhs) const { return x == rhs.x and y == rhs.y; } + inline bool operator!=(const point& rhs) const { return not (*this == rhs); } + inline bool operator<(const point& rhs) const { return pair(x, y) < pair(rhs.x, rhs.y); } + inline bool operator<=(const point& rhs) const { return *this < rhs or *this == rhs; } + inline bool operator>(const point& rhs) const { return not (*this <= rhs); } + inline bool operator>=(const point& rhs) const { return not (*this < rhs); } + + static inline ld slope(const point& a, const point& b) { + if (a.x == b.x) return INFLL; + return ld(a.y - b.y) / (a.x - b.x); + } + + // distance from point `a` to line `l--r` + static inline ld dist(const point& a, const point& l, const point& r) { + return area(a, l, r) * 2 / (l - r).norm(); + } + + static inline ld area(const point& a, const point& b, const point& c) { + return (b - a) * (c - a) / ld(2); + } + + friend inline istream& operator>>(istream& in, point& a) { + return in >> a.x >> a.y; + } + + friend inline ostream& operator<<(ostream& out, const point& a) { + return out << a.x << ' ' << a.y; + } +};