summary refs log tree commit diff stats
path: root/src/vector.h
blob: c7b58dcdfe53276b7e86dea46fa099ad47606ab0 (plain) (blame)
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#ifndef VECTOR_H_5458ED71
#define VECTOR_H_5458ED71

#include <tuple>

template <typename T>
class vec2 {
public:

  T coords[2];

  vec2() : coords{0, 0} {
  }

  vec2(T x, T y) : coords{x, y} {
  }

  inline T& x() {
    return coords[0];
  }

  inline const T& x() const {
    return coords[0];
  }

  inline T& w() {
    return coords[0];
  }

  inline const T& w() const {
    return coords[0];
  }

  inline T& y() {
    return coords[1];
  }

  inline const T& y() const {
    return coords[1];
  }

  inline T& h() {
    return coords[1];
  }

  inline const T& h() const {
    return coords[1];
  }

  template <typename R>
  operator vec2<R>() const {
    return vec2<R>(x(), y());
  }

  vec2 operator+(const vec2& other) const {
    return vec2(x() + other.x(), y() + other.y());
  }

  vec2& operator+=(const vec2& other) {
    x() += other.x();
    y() += other.y();

    return *this;
  }

  vec2 operator-(const vec2& other) const {
    return vec2(x() - other.x(), y() - other.y());
  }

  vec2 operator-=(const vec2& other) {
    x() -= other.x();
    y() -= other.y();

    return *this;
  }

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

  vec2 operator*(T s) const {
    return vec2(x() * s, y() * s);
  }

  vec2 operator/(T s) const {
    return vec2(x() / s, y() / s);
  }

  vec2& operator*=(T s) {
    x() *= s;
    y() *= s;

    return *this;
  }

  vec2 operator*(const vec2& other) const {
    return vec2(x() * other.x(), y() * other.y());
  }

  vec2 operator/(const vec2& other) const {
    return vec2(x() / other.x(), y() / other.y());
  }

  bool operator==(const vec2& other) const {
    return (x() == other.x()) && (y() == other.y());
  }

  bool operator!=(const vec2& other) const {
    return (x() != other.x()) || (y() != other.y());
  }

  bool operator<(const vec2& other) const {
    return std::tie(x(), y()) < std::tie(other.x(), other.y());
  }

};

using vec2i = vec2<int>;

#endif /* end of include guard: VECTOR_H_5458ED71 */