Advertisement
Guest User

hue

a guest
Oct 31st, 2014
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.49 KB | None | 0 0
  1. /*
  2. File: vector2D.cpp
  3. Created by: Vincent Le
  4. Creation Date: 10/31/2014
  5. Synopsis:
  6. */
  7.  
  8. #include <iostream>
  9. #include <iomanip>
  10. #include <cmath>
  11.  
  12. using namespace std;
  13.  
  14. const double EPSILON(1e-12);
  15.  
  16. // function prototypes
  17.  
  18. // ENTER FUNCTION PROTOTYPE FOR read_vector HERE.
  19. void read_vector(const string &, double & x, double & y);
  20.  
  21. // ENTER FUNCTION PROTOTYPE FOR vector_length HERE.
  22. double vector_length(int x, int y);
  23.  
  24. // ENTER FUNCTION PROTOTYPE FOR write_vector HERE.
  25. void write_vector(const string &, double x, double y);
  26.  
  27. // ENTER FUNCTION PROTOTYPE FOR vector_add HERE.
  28. void vector_add(double x1, double y1, double x2, double y2, double & x3, double & y3);
  29.  
  30. // ENTER FUNCTION PROTOTYPE FOR vector_subtract HERE.
  31. void vector_subtract(double x1, double y1, double x2, double y2, double & x3, double & y3);
  32.  
  33. // ENTER FUNCTION PROTOTYPE FOR scalar_mult HERE.
  34. void scalar_mult(double x1, double y1, double s, double & x2, double & y2);
  35.  
  36. // ENTER FUNCTION PROTOTYPE FOR normalize HERE.
  37. void normalize(double & x, double & y);
  38.  
  39. // ENTER FUNCTION PROTOTYPE FOR perpendicular HERE.
  40. void perpendicular(double x1, double y1, double x2, double y2);
  41.  
  42. // *** DO NOT CHANGE ANY CODE IN THE MAIN FUNCTION.
  43. int main()
  44. {
  45. double u1, v1; // coordinates of first vector
  46. double u2, v2; // coordinates of second vector
  47. double u3, v3;
  48. double scalar;
  49.  
  50. read_vector("Enter first vector (2 floats): ", u1, v1);
  51. read_vector("Enter second vector (2 floats): ", u2, v2);
  52.  
  53. cout << "Enter scalar multiplier: ";
  54. cin >> scalar;
  55. cout << endl;
  56.  
  57. write_vector("First vector: ", u1, v1);
  58. write_vector("Second vector: ", u2, v2);
  59.  
  60. cout << endl;
  61.  
  62. vector_add(u1, v1, u2, v2, u3, v3);
  63. write_vector("Vector add: ", u3, v3);
  64.  
  65. vector_subtract(u1, v1, u2, v2, u3, v3);
  66. write_vector("Vector subtract: ", u3, v3);
  67.  
  68. scalar_mult(u1, v1, scalar, u3, v3);
  69. write_vector("Scalar multiplier: ", u3, v3);
  70.  
  71. cout << endl;
  72.  
  73. write_vector("First vector: ", u1, v1);
  74. write_vector("Second vector: ", u2, v2);
  75. perpendicular(u1, v1, u2, v2);
  76.  
  77. return(0);
  78. }
  79.  
  80. // DEFINE FUNCTION read_vector HERE.
  81. void read_vector(const string &, double & x, double & y)
  82. {
  83. int a, b;
  84. cout << "Enter vector (2 floats): ";
  85. cin >> a;
  86. cin >> b;
  87. }
  88.  
  89. // DEFINE FUNCTION vector_length HERE.
  90. double vector_length(int x, int y)
  91. {
  92. int r;
  93. r = sqrt((pow(x,2))+(pow(y,2)));
  94. return r;
  95. }
  96.  
  97.  
  98. // DEFINE FUNCTION write_vector HERE.
  99. void write_vector(const string &, double x, double y)
  100. {
  101. cout << "(" << x << ", " << y << ") has length " << vector_length(x,y);
  102. }
  103.  
  104. // DEFINE FUNCTION vector_add HERE.
  105. void vector_add(double x1, double y1, double x2, double y2, double & x3, double & y3)
  106. {
  107. x3 = x1+x2;
  108. y3 = y1+y2;
  109. }
  110.  
  111.  
  112. // DEFINE FUNCTION vector_subtract HERE.
  113. void vector_subtract(double x1, double y1, double x2, double y2, double & x3, double & y3)
  114. {
  115. x3 = x1-x2;
  116. y3 = y1-y2;
  117. }
  118.  
  119. // DEFINE FUNCTION scalar_mult HERE.
  120. void scalar_mult(double x1, double y1, double s, double & x2, double & y2)
  121. {
  122. x2 = s*x1;
  123. y2 = s*y1;
  124. }
  125.  
  126.  
  127. // DEFINE FUNCTION normalize HERE.
  128. void normalize(double & x, double & y)
  129. {
  130. vector_length(x,y);
  131. int b;
  132. b = 0;
  133. if (vector_length != EPSILON)
  134. {
  135. x = ((x)/(sqrt((pow(x,2))+(pow(y,2)))));
  136. y = ((y)/(sqrt((pow(x,2))+(pow(y,2)))));
  137. }
  138. if (vector_length == EPSILON)
  139. {
  140. x = b;
  141. y = b;
  142. }
  143. }
  144.  
  145. // DEFINE FUNCTION perpendicular HERE.
  146. void perpendicular(double x1, double y1, double x2, double y2)
  147. {
  148. normalize(x1, y1);
  149. normalize(x2, y2);
  150. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement