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

template <typename T>
class vec2 {
public:

  T coords[2];

  vec2() = default;

  vec2(double x, double 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*(double s) const
  {
    return vec2(x() * s, y() * s);
  }

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

    return *this;
  }

};

using vec2d = vec2<double>;
using vec2i = vec2<int>;

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