vect.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. // vect.cpp -- methods for the Vector class
  2. #include <cmath>
  3. #include "vect.h" // includes <iostream>
  4. using std::sqrt;
  5. using std::sin;
  6. using std::cos;
  7. using std::atan;
  8. using std::atan2;
  9. using std::cout;
  10. namespace VECTOR
  11. {
  12. // compute degrees in one radian
  13. const double Rad_to_deg = 45.0 / atan(1.0);
  14. // should be about 57.2957795130823
  15. // private methods
  16. // calculates magnitude from x and y
  17. void Vector::set_mag()
  18. {
  19. mag = sqrt(x * x + y * y);
  20. }
  21. void Vector::set_ang()
  22. {
  23. if (x == 0.0 && y == 0.0)
  24. ang = 0.0;
  25. else
  26. ang = atan2(y, x);
  27. }
  28. // set x from polar coordinate
  29. void Vector::set_x()
  30. {
  31. x = mag * cos(ang);
  32. }
  33. // set y from polar coordinate
  34. void Vector::set_y()
  35. {
  36. y = mag * sin(ang);
  37. }
  38. // public methods
  39. Vector::Vector() // default constructor
  40. {
  41. x = y = mag = ang = 0.0;
  42. mode = RECT;
  43. }
  44. // construct vector from rectangular coordinates if form is r
  45. // (the default) or else from polar coordinates if form is p
  46. Vector::Vector(double n1, double n2, Mode form)
  47. {
  48. mode = form;
  49. if (form == RECT)
  50. {
  51. x = n1;
  52. y = n2;
  53. set_mag();
  54. set_ang();
  55. }
  56. else if (form == POL)
  57. {
  58. mag = n1;
  59. ang = n2 / Rad_to_deg;
  60. set_x();
  61. set_y();
  62. }
  63. else
  64. {
  65. cout << "Incorrect 3rd argument to Vector() -- ";
  66. cout << "vector set to 0\n";
  67. x = y = mag = ang = 0.0;
  68. mode = RECT;
  69. }
  70. }
  71. // reset vector from rectangular coordinates if form is
  72. // RECT (the default) or else from polar coordinates if
  73. // form is POL
  74. void Vector:: reset(double n1, double n2, Mode form)
  75. {
  76. mode = form;
  77. if (form == RECT)
  78. {
  79. x = n1;
  80. y = n2;
  81. set_mag();
  82. set_ang();
  83. }
  84. else if (form == POL)
  85. {
  86. mag = n1;
  87. ang = n2 / Rad_to_deg;
  88. set_x();
  89. set_y();
  90. }
  91. else
  92. {
  93. cout << "Incorrect 3rd argument to Vector() -- ";
  94. cout << "vector set to 0\n";
  95. x = y = mag = ang = 0.0;
  96. mode = RECT;
  97. }
  98. }
  99. Vector::~Vector() // destructor
  100. {
  101. }
  102. void Vector::polar_mode() // set to polar mode
  103. {
  104. mode = POL;
  105. }
  106. void Vector::rect_mode() // set to rectangular mode
  107. {
  108. mode = RECT;
  109. }
  110. // operator overloading
  111. // add two Vectors
  112. Vector Vector::operator+(const Vector & b) const
  113. {
  114. return Vector(x + b.x, y + b.y);
  115. }
  116. // subtract Vector b from a
  117. Vector Vector::operator-(const Vector & b) const
  118. {
  119. return Vector(x - b.x, y - b.y);
  120. }
  121. // reverse sign of Vector
  122. Vector Vector::operator-() const
  123. {
  124. return Vector(-x, -y);
  125. }
  126. // multiply vector by n
  127. Vector Vector::operator*(double n) const
  128. {
  129. return Vector(n * x, n * y);
  130. }
  131. // friend methods
  132. // multiply n by Vector a
  133. Vector operator*(double n, const Vector & a)
  134. {
  135. return a * n;
  136. }
  137. // display rectangular coordinates if mode is RECT,
  138. // else display polar coordinates if mode is POL
  139. std::ostream & operator<<(std::ostream & os, const Vector & v)
  140. {
  141. if (v.mode == Vector::RECT)
  142. os << "(x,y) = (" << v.x << ", " << v.y << ")";
  143. else if (v.mode == Vector::POL)
  144. {
  145. os << "(m,a) = (" << v.mag << ", "
  146. << v.ang * Rad_to_deg << ")";
  147. }
  148. else
  149. os << "Vector object mode is invalid";
  150. return os;
  151. }
  152. } // end namespace VECTOR