Advertisement
danielperezunlpam

vec2d

Apr 22nd, 2021 (edited)
693
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.91 KB | None | 0 0
  1. #ifndef EDYA_P3_VECTOR2D_H
  2. #define EDYA_P3_VECTOR2D_H
  3.  
  4. #include <iostream>
  5. #include <cmath>
  6.  
  7. /**
  8.  * FLOAT | DOUBLE
  9.  * @tparam T
  10.  */
  11. template<class T>
  12. class vec2d {
  13.  
  14. public:
  15.     vec2d(T x = 0, T y = 0) {
  16.         this->x = x;
  17.         this->y = y;
  18.     }
  19.  
  20.     virtual ~vec2d() {
  21.         std::cout << "\n~vec2d" << *this;
  22.     }
  23.  
  24.     T getX() { return this->x; }
  25.  
  26.     T getY() { return this->y; }
  27.  
  28.     void setX(T x) { this->x = x; }
  29.  
  30.     void setY(T y) { this->y = y; }
  31.  
  32.     T mag() {
  33.         return sqrt(x * x + y * y);
  34.     }
  35.  
  36.     T dot(const vec2d &v) {
  37.         return (this->x) * (v.x) + (this->y) * (v.y);
  38.     }
  39.  
  40.     T cross(const vec2d &v) {
  41.         return (this->x) * (v.y) - (this->y) * (v.x);
  42.     }
  43.  
  44.     vec2d operator+(const vec2d &v1) {
  45.         return vec2d(v1.x + x, v1.y + y);
  46.     }
  47.  
  48.     vec2d operator-(const vec2d &v1) {
  49.         return vec2d(x - v1.x, y - v1.y);
  50.     }
  51.  
  52.  
  53.     vec2d &operator+=(const vec2d &vec_sum) {
  54.  
  55.         this->x += vec_sum.x;
  56.         this->y += vec_sum.y;
  57.         return *this;
  58.  
  59.     }
  60.  
  61.     vec2d &operator-=(const vec2d &vec_res) {
  62.         this->x -= vec_res.x;
  63.         this->y -= vec_res.y;
  64.         return *this;
  65.     }
  66.  
  67.     vec2d &operator/=(T i) {
  68.         i = i == 0 ? 1 : i;
  69.         this->x /= i;
  70.         this->y /= i;
  71.         return *this;
  72.     }
  73.  
  74.     vec2d operator/(T i) {
  75.         vec2d rta;
  76.         rta.x = this->x / i;
  77.         rta.y = this->y / i;
  78.         return rta;
  79.     }
  80.  
  81.     vec2d operator*(T i) {
  82.         vec2d rta;
  83.         rta.x = this->x * i;
  84.         rta.y = this->y * i;
  85.         return rta;
  86.     }
  87.  
  88.     vec2d &operator*=(const T &v) {
  89.         this->x *= v;
  90.         this->y *= v;
  91.         return *this;
  92.     }
  93.  
  94.     friend std::ostream &operator<<(std::ostream &out, const vec2d &vec) {
  95.         return out << "(" << vec.x << ", " << vec.y << ")";
  96.     }
  97.  
  98.     bool operator==(const vec2d &rhs) {
  99.         return x == rhs.x && y == rhs.y;
  100.     }
  101.  
  102.     bool operator!=(const vec2d &rhs) {
  103.         return !(*this == rhs);
  104.     }
  105.  
  106. private:
  107.     T x, y;
  108.  
  109. };
  110.  
  111. typedef vec2d<float> vf2d;
  112. typedef vec2d<double> vd2d;
  113. typedef vec2d<int> vi2d;
  114.  
  115. void test_vector() {
  116.  
  117.     vf2d w(1.0, 4.0);
  118.     vd2d p(1.0, 4.0);
  119.  
  120.     vec2d<double> i(1.0, 4.0);
  121.     vec2d<double> j(2.0, 3.0);
  122.     vec2d<double> k = i + j - i;
  123.     std::cout << std::endl << i;
  124.     std::cout << std::endl << j;
  125.     std::cout << std::endl << k;
  126.     k = k * 4.0;
  127.     std::cout << std::endl << k;
  128.     k = k / 2;
  129.     std::cout << std::endl << k;
  130.  
  131.     k *= 4.0;
  132.     std::cout << std::endl << k;
  133.     k /= 2;
  134.     std::cout << std::endl << k;
  135.  
  136.     std::cout << std::endl << vec2d<double>(1, 1).mag();
  137.     std::cout << std::endl << vec2d<double>(3, 4).mag();
  138.     std::cout << std::endl << i << " x " << j << " = " << i.cross(j);
  139.     std::cout << std::endl << i << " . " << j << " = " << i.dot(j);
  140.     std::cout << std::endl;
  141. }
  142.  
  143. #endif //EDYA_P3_VECTOR2D_H
  144.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement