aboutsummaryrefslogtreecommitdiff
path: root/point.h
blob: 489dcf8cd01e1e7a9e0ddfdab746ffca87dfcb75 (plain)
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
#include <cmath>
#include <iostream>

template <typename PT = int, typename P2T = long long> struct PointT {
  PointT &operator+=(const PointT &o) {
    x += o.x;
    y += o.y;
    return *this;
  }

  PointT operator+(const PointT &o) const {
    PointT copy = *this;
    return copy += o;
  }

  PointT &operator-=(const PointT &o) {
    x -= o.x;
    y -= o.y;
    return *this;
  }

  PointT operator-() const { return {-x, -y}; }

  PointT operator-(const PointT &o) const {
    PointT copy = *this;
    return copy -= o;
  }

  PointT to(const PointT &o) const { return o - *this; }

  P2T dot(const PointT &o) const {
    return static_cast<P2T>(x) * o.x + static_cast<P2T>(y) * o.y;
  }

  P2T det(const PointT &o) const {
    return static_cast<P2T>(x) * o.y - static_cast<P2T>(y) * o.x;
  }

  double norm() const { return std::hypot<double>(y, x); }

  P2T norm2() const { return dot(*this); }

  PT x, y;
};

template <typename PT, typename P2T>
std::ostream &operator<<(std::ostream &out, const PointT<PT, P2T> &p) {
  return out << "(" << p.x << ", " << p.y << ")";
}