Khangai Robot Play - Naive  01
THe naive play of the Khangai Robot
vec2.h
Go to the documentation of this file.
1 /*
2  * vec2.h
3  *
4  * Created : 1/9/2018
5  * Author : n-is
6  * email : 073bex422.nischal@pcampus.edu.np
7  */
8 
9 #ifndef _VEC2_H_
10 #define _VEC2_H_
11 
12 #include "vec3.h"
13 #include <math.h>
14 
15 template <class T>
16 class Vec2
17 {
18 public:
19  Vec2() { set_Values(0,0); }
20  Vec2(T x, T y) { set_Values(x,y); }
21 
22  Vec2(Vec3<T> v) { set_Values(v.getX(), v.getY()) ;}
23 
24  Vec2(Vec2 &&) = default;
25  Vec2 &operator=(Vec2 &&) = default;
26  ~Vec2() { }
27 
28  T getX() const { return x_; }
29  T getY() const { return y_; }
30  void setX(T x) { x_ = x; }
31  void setY(T y) { y_ = y; }
32 
33  void set_Values(T x, T y) {
34  setX(x);
35  setY(y);
36  }
37 
38 
39  Vec2 mult_EW(float num) {
40  Vec2 temp(*this);
41  temp.x_ *= num;
42  temp.y_ *= num;
43 
44  return temp;
45  }
46 
47  template <typename T1>
49  Vec2 temp(v);
50  temp.x_ *= x_;
51  temp.y_ *= y_;
52 
53  return temp;
54  }
55 
56 
57  Vec2 add_EW(float num) {
58  Vec2 temp(*this);
59  temp.x_ += num;
60  temp.y_ += num;
61 
62  return temp;
63  }
64 
65  template <typename T1>
67  Vec2 temp(v);
68  temp.x_ += x_;
69  temp.y_ += y_;
70 
71  return temp;
72  }
73 
74 
75  Vec2 sub_EW(float num) {
76  Vec2 temp(*this);
77  temp.x_ -= num;
78  temp.y_ -= num;
79 
80  return temp;
81  }
82 
83  template <typename T1>
85  Vec2 temp(v);
86  temp.x_ -= x_;
87  temp.y_ -= y_;
88 
89  return temp;
90  }
91 
92 
93  Vec2 div_EW(float num) {
94  Vec2 temp(*this);
95  temp.x_ /= (float)num;
96  temp.y_ /= (float)num;
97 
98  return temp;
99  }
100 
101  template <typename T1>
103  Vec2 temp(*this);
104  temp.x_ /= (float)v.x_;
105  temp.y_ /= (float)v.y_;
106 
107  return temp;
108  }
109 
110 
111  Vec2(const Vec2 &v) {
112  x_ = v.x_;
113  y_ = v.y_;
114  }
115 
116  Vec2 &operator=(const Vec2 &v) {
117  Vec2 temp(v);
118  swap(*this, temp);
119 
120  return *this;
121  }
122 
123  Vec2 &operator+=(const Vec2 &rhs) {
124  x_ += rhs.x_;
125  y_ += rhs.y_;
126 
127  return *this;
128  }
129 
130  Vec2 &operator-=(const Vec2 &rhs) {
131  x_ -= rhs.x_;
132  y_ -= rhs.y_;
133 
134  return *this;
135  }
136 
137  friend Vec2 operator+(Vec2 lhs, const Vec2 &rhs) {
138  lhs += rhs;
139  return lhs;
140  }
141 
142  friend Vec2 operator-(Vec2 lhs, const Vec2 &rhs) {
143  lhs -= rhs;
144  return lhs;
145  }
146 
148  float r = sqrtf(x_*x_ + y_*y_);
149  float theta = atan2f(y_, x_);
150  Vec2<float> pol(r, theta);
151  return pol;
152  }
153 
154  void print() const {
155  printf("%ld, %ld", (int32_t)x_, (int32_t)y_);
156  }
157 
158 private:
159  T x_,y_;
160 
161  friend void swap(Vec2 &first, Vec2 &second) {
162  swap_Element(first.x_, second.x_);
163  swap_Element(first.y_, second.y_);
164  }
165 };
166 
167 #endif // !_VEC2_H_
Vec2 & operator+=(const Vec2 &rhs)
Definition: vec2.h:123
Definition: vec3.h:24
Vec2 mult_EW(Vec2< T1 > v)
Definition: vec2.h:48
T getX() const
Definition: vec3.h:34
friend void swap(Vec2 &first, Vec2 &second)
Definition: vec2.h:161
Vec2(T x, T y)
Definition: vec2.h:20
Vec2 polar()
Definition: vec2.h:147
Vec2 add_EW(Vec2< T1 > v)
Definition: vec2.h:66
Vec2 & operator-=(const Vec2 &rhs)
Definition: vec2.h:130
void swap_Element(T &first, T &second)
Definition: vec3.h:16
Vec2 add_EW(float num)
Definition: vec2.h:57
void set_Values(T x, T y)
Definition: vec2.h:33
Vec2()
Definition: vec2.h:19
~Vec2()
Definition: vec2.h:26
T x_
Definition: vec2.h:159
T getY() const
Definition: vec3.h:35
void setY(T y)
Definition: vec2.h:31
void print() const
Definition: vec2.h:154
void setX(T x)
Definition: vec2.h:30
Vec2 div_EW(Vec2< T1 > v)
Definition: vec2.h:102
Vec2 div_EW(float num)
Definition: vec2.h:93
T getY() const
Definition: vec2.h:29
friend Vec2 operator-(Vec2 lhs, const Vec2 &rhs)
Definition: vec2.h:142
Vec2(const Vec2 &v)
Definition: vec2.h:111
Vec2 sub_EW(float num)
Definition: vec2.h:75
Vec2 & operator=(Vec2 &&)=default
Definition: vec2.h:16
Vec2 mult_EW(float num)
Definition: vec2.h:39
T y_
Definition: vec2.h:159
Vec2(Vec3< T > v)
Definition: vec2.h:22
Vec2 sub_EW(Vec2< T1 > v)
Definition: vec2.h:84
T getX() const
Definition: vec2.h:28
Vec2 & operator=(const Vec2 &v)
Definition: vec2.h:116
friend Vec2 operator+(Vec2 lhs, const Vec2 &rhs)
Definition: vec2.h:137