Vladislav_Bezruk

DISTANCE BETWEEN POINT AND RAY IN 3D

Jul 9th, 2022 (edited)
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.53 KB | None | 0 0
  1. // ## task DISTANCE BETWEEN POINT AND RAY IN 3D ###
  2.  
  3. /* ### Description ###
  4.  
  5. There are a point and a ray in 3D space. Find the distance between them.
  6.  
  7. Input:
  8. Nine reals, the 3D coordinates of:
  9.  
  10. 1. The given point.
  11.  
  12. 2. The initial point of the ray.
  13.  
  14. 3. The direction vector of the ray.
  15.  
  16. The length of the direction vector is greater than 1e-8.
  17.  
  18. Output:
  19. A real, the distance between the given point and the ray.
  20.  
  21. */
  22.  
  23. #include <iostream>
  24.  
  25. #include <iomanip>
  26.  
  27. #include <cmath>
  28.  
  29. #include <vector>
  30.  
  31. using namespace std;
  32.  
  33. //vector input
  34. template <typename T> void read_vector(vector<T>& vec)
  35. {
  36.     T in;
  37.  
  38.     for (int i = 0; i < 3; i++)
  39.     {
  40.         cin >> in;
  41.  
  42.         vec.push_back(in);
  43.     }
  44.  
  45. }
  46.  
  47. //difference of two vectors
  48. template <typename T> vector<T> vector_diff(const vector<T>& vec_a, const vector<T>& vec_b)
  49. {
  50.     vector<T> result;
  51.    
  52.     for (int i = 0; i < 3; i++)
  53.     {
  54.         result.push_back(vec_a[i] - vec_b[i]);
  55.     }
  56.    
  57.     return result;
  58. }
  59.  
  60. //sum of two vectors
  61. template <typename T> vector<T> vector_sum(const vector<T>& vec_a, const vector<T>& vec_b)
  62. {
  63.     vector<T> result;
  64.    
  65.     for (int i = 0; i < 3; i++)
  66.     {
  67.         result.push_back(vec_a[i] + vec_b[i]);
  68.     }
  69.    
  70.     return result;
  71. }
  72.  
  73. //mult of number and vector
  74. template <typename T> vector<T> vector_mult(const T a, const vector<T>& vec_b)
  75. {
  76.     vector<T> result;
  77.    
  78.     for (int i = 0; i < 3; i++)
  79.     {
  80.         result.push_back(a * vec_b[i]);
  81.     }
  82.    
  83.     return result;
  84. }
  85.  
  86. //scalar product of two vectors
  87. template <typename T> T scalar_product(const vector<T>& vec_a, const vector<T>& vec_b)
  88. {
  89.     T res = 0.0;
  90.  
  91.     for (int i = 0; i < 3; i++)
  92.     {
  93.         res += vec_a[i] * vec_b[i];
  94.     }
  95.  
  96.     return res;
  97. }
  98.  
  99. //module or length of vector
  100. template <typename T> double vector_module(const vector<T>& vec)
  101. {
  102.     double square_sum = 0.0;
  103.  
  104.     for (int i = 0; i < 3; i++)
  105.     {
  106.         square_sum += pow(vec[i], 2);
  107.     }
  108.  
  109.     double res = sqrt(square_sum);
  110.  
  111.     return res;
  112. }
  113.  
  114. int main()
  115. {
  116.     //point, point on vector and vector
  117.     vector<double> p, vec_p, vec;
  118.  
  119.     //reading point
  120.     read_vector<double>(p);
  121.  
  122.     //reading point on vector
  123.     read_vector<double>(vec_p);
  124.  
  125.     //reading vector
  126.     read_vector<double>(vec);
  127.    
  128.     //calculations
  129.     double t0 = scalar_product<double>(vec, vector_diff<double>(p, vec_p)) / scalar_product<double>(vec, vec);
  130.  
  131.     double d;
  132.    
  133.     if (t0 <= 0)
  134.     {
  135.         d = vector_module<double>(vector_diff<double>(p, vec_p));
  136.     }
  137.     else
  138.     {
  139.         d = vector_module<double>(vector_diff<double>(p, vector_sum<double>(vec_p, vector_mult<double>(t0, vec))));
  140.     }
  141.    
  142.     cout << fixed << setprecision(9) << d;
  143.  
  144.     return 0;
  145. }
Add Comment
Please, Sign In to add comment