summary refs log tree commit diff stats
path: root/src/vector.h
blob: bf38bb2e0615b01658c52a67aac6a63566a31c74 (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
#ifndef VECTOR_H_5458ED71
#define VECTOR_H_5458ED71

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

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

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

};

using vec2i = vec2<int>;

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