Advertisement
Leedwon

Untitled

Apr 21st, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.05 KB | None | 0 0
  1. // 11-1
  2.  
  3. #include "Vectorh.h"
  4. #include <cmath>
  5. using std::sqrt;
  6. using std::sin;
  7. using std::cos;
  8. using std::atan;
  9. using std::atan2;
  10. using std::cout;
  11.  
  12. namespace VECTOR
  13. {
  14.     const double Rad_to_deg = 45.0 / atan(1.0);
  15.  
  16.     void vector::set_len()
  17.     {
  18.         len = sqrt(x*x + y*y);
  19.     }
  20.     void vector::set_ang()
  21.     {
  22.         if (x == 0 && y == 0)
  23.             ang = 0;
  24.         else
  25.             ang = atan2(y, x);
  26.     }
  27.     void vector::set_x()
  28.     {
  29.         x = cos(ang) * len;
  30.     }
  31.     void vector::set_y()
  32.     {
  33.         y = sin(ang) * len;
  34.     }
  35.     vector::vector()
  36.     {
  37.         x = y = len = ang = 0;
  38.         mode = RECT;
  39.     }
  40.     vector::vector(double n1, double n2, Mode form)
  41.     {
  42.         mode = form;
  43.         if (mode == RECT)
  44.         {
  45.             x = n1;
  46.             y = n2;
  47.             set_ang();
  48.             set_len();
  49.         }
  50.         else if (mode == POL)
  51.         {
  52.             len = n1;
  53.             ang = n2 / Rad_to_deg;
  54.             set_x();
  55.             set_y();
  56.         }
  57.         else
  58.         {
  59.             cout << "bledny tryb, zeruje wektor";
  60.             vector();
  61.         }
  62.     }
  63.     void vector::reset(double n1, double n2, Mode form)
  64.     {
  65.         mode = form;
  66.         if (mode == RECT)
  67.         {
  68.             x = n1;
  69.             y = n2;
  70.             set_ang();
  71.             set_len();
  72.         }
  73.         else if (mode == POL)
  74.         {
  75.             len = n1;
  76.             ang = n2 / Rad_to_deg;
  77.             set_x();
  78.             set_y();
  79.         }
  80.         else
  81.         {
  82.             cout << "bledny tryb, zeruje wektor";
  83.             vector();
  84.         }
  85.     }
  86.     vector::~vector()
  87.     {
  88.     }
  89.     void vector::polar_mode()
  90.     {
  91.         mode = POL;
  92.     }
  93.     void vector::rect_mode()
  94.     {
  95.         mode = RECT;
  96.     }
  97.     vector vector::operator+(const vector & b) const
  98.     {
  99.         return vector(x + b.x, y + b.y);
  100.     }
  101.     vector vector::operator-(const vector & b) const
  102.     {
  103.         return vector(x - b.x, y - b.y);
  104.     }
  105.     vector vector::operator-() const
  106.     {
  107.         return vector(-x, -y);
  108.     }
  109.     vector vector::operator*(double n) const
  110.     {
  111.         return vector(x * n, y * n);
  112.     }
  113.     vector operator*(double n, const vector & a)
  114.     {
  115.         return a * n;
  116.     }
  117.     std::ostream & operator<<(std::ostream & os, const vector & v)
  118.     {
  119.         if (v.mode == vector::RECT)
  120.             os << "(x,y) = (" << v.x << "," << v.y << ")";
  121.         else if (v.mode == vector::POL)
  122.             os << "(len,ang) = (" << v.len << "," << v.ang * Rad_to_deg << ")";
  123.         else
  124.             os << "Niepoprawny tryb";
  125.         return os;
  126.     }
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement