Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // 11-1
- #include "Vectorh.h"
- #include <cmath>
- using std::sqrt;
- using std::sin;
- using std::cos;
- using std::atan;
- using std::atan2;
- using std::cout;
- namespace VECTOR
- {
- const double Rad_to_deg = 45.0 / atan(1.0);
- void vector::set_len()
- {
- len = sqrt(x*x + y*y);
- }
- void vector::set_ang()
- {
- if (x == 0 && y == 0)
- ang = 0;
- else
- ang = atan2(y, x);
- }
- void vector::set_x()
- {
- x = cos(ang) * len;
- }
- void vector::set_y()
- {
- y = sin(ang) * len;
- }
- vector::vector()
- {
- x = y = len = ang = 0;
- mode = RECT;
- }
- vector::vector(double n1, double n2, Mode form)
- {
- mode = form;
- if (mode == RECT)
- {
- x = n1;
- y = n2;
- set_ang();
- set_len();
- }
- else if (mode == POL)
- {
- len = n1;
- ang = n2 / Rad_to_deg;
- set_x();
- set_y();
- }
- else
- {
- cout << "bledny tryb, zeruje wektor";
- vector();
- }
- }
- void vector::reset(double n1, double n2, Mode form)
- {
- mode = form;
- if (mode == RECT)
- {
- x = n1;
- y = n2;
- set_ang();
- set_len();
- }
- else if (mode == POL)
- {
- len = n1;
- ang = n2 / Rad_to_deg;
- set_x();
- set_y();
- }
- else
- {
- cout << "bledny tryb, zeruje wektor";
- vector();
- }
- }
- vector::~vector()
- {
- }
- void vector::polar_mode()
- {
- mode = POL;
- }
- void vector::rect_mode()
- {
- mode = RECT;
- }
- vector vector::operator+(const vector & b) const
- {
- return vector(x + b.x, y + b.y);
- }
- vector vector::operator-(const vector & b) const
- {
- return vector(x - b.x, y - b.y);
- }
- vector vector::operator-() const
- {
- return vector(-x, -y);
- }
- vector vector::operator*(double n) const
- {
- return vector(x * n, y * n);
- }
- vector operator*(double n, const vector & a)
- {
- return a * n;
- }
- std::ostream & operator<<(std::ostream & os, const vector & v)
- {
- if (v.mode == vector::RECT)
- os << "(x,y) = (" << v.x << "," << v.y << ")";
- else if (v.mode == vector::POL)
- os << "(len,ang) = (" << v.len << "," << v.ang * Rad_to_deg << ")";
- else
- os << "Niepoprawny tryb";
- return os;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement