summary refs log tree commit diff stats
path: root/src/vector.h
blob: ed20936fca8ad1b0542db3125a6803c51cc498f9 (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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#ifndef COORDINATES_H_A45D34FB
#define COORDINATES_H_A45D34FB

#include <cstddef>

template <typename T>
class vec2 {
public:

  T coords[2];

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

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

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

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

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

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

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

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

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

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

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

  constexpr 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;
  }

  constexpr 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;
  }

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

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

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

    return *this;
  }

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

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

    return *this;
  }

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

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

  constexpr bool operator!=(const vec2& other) const
  {
    return !(*this == other);
  }

};

using vec2s = vec2<size_t>;
using vec2i = vec2<int>;

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