Advertisement
Guest User

Untitled

a guest
Nov 17th, 2019
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.48 KB | None | 0 0
  1. #include <cmath>
  2. #include <iostream>
  3.  
  4. struct Point {
  5.   double x = 0.0, y = 0.0, z = 0.0;
  6.  
  7.   Point operator *(double a) const {
  8.     return {x * a, y * a, z * a};
  9.   }
  10.  
  11.   Point operator /(double a) const {
  12.     return {x / a, y / a, z / a};
  13.   }
  14.  
  15.   Point operator +(const Point& another) const {
  16.     return {another.x + this->x, another.y + this->y, another.z + this->z};
  17.   }
  18. };
  19.  
  20. struct Segment {
  21.   Point begin, end;
  22. };
  23.  
  24. double FindDistanceBetweenPoints(const Point& point_1, const Point& point_2) {
  25.   return std::sqrt(std::pow(point_1.x - point_2.x, 2) + std::pow(point_1.y - point_2.y, 2) + std::pow(point_1.z - point_2.z, 2));
  26. }
  27.  
  28. double FindDistanceBetweenSegmentAndPoint(const Segment& segment, const Point& point, double eps) {
  29.   Point right = segment.end;
  30.   Point left = segment.begin;
  31.   double dist_left = 0.0;
  32.   double dist_right = 0.0;
  33.   do {
  34.     Point left_candidate = (left * 2 + right) / 3;
  35.     Point right_candidate = (left + right * 2) / 3;
  36.     dist_left = FindDistanceBetweenPoints(point, left_candidate);
  37.     dist_right = FindDistanceBetweenPoints(point, right_candidate);
  38.     if (dist_left < dist_right) {
  39.       right = right_candidate;
  40.     } else {
  41.       left = left_candidate;
  42.     }
  43.   } while (FindDistanceBetweenPoints(left, right) > eps);
  44.  
  45.   return dist_left;
  46. }
  47.  
  48. double FindDistanceBetweenSegments(const Segment& first, const Segment& second, double eps) {
  49.   Point right = second.end;
  50.   Point left = second.begin;
  51.   double dist_left = 0.0;
  52.   double dist_right = 0.0;
  53.   do {
  54.     Point left_candidate = (left * 2 + right) / 3;
  55.     Point right_candidate = (left + right * 2) / 3;
  56.     dist_left = FindDistanceBetweenSegmentAndPoint(first, left_candidate, eps);
  57.     dist_right = FindDistanceBetweenSegmentAndPoint(first, right_candidate, eps);
  58.     if (dist_left < dist_right) {
  59.       right = right_candidate;
  60.     } else {
  61.       left = left_candidate;
  62.     }
  63.   } while (FindDistanceBetweenPoints(left, right) > eps);
  64.  
  65.   return dist_left;
  66. }
  67.  
  68. int main() {
  69.   double x, y, z;
  70.  
  71.   std::cin >> x >> y >> z;
  72.   Point segment_begin = {x, y, z};
  73.   std::cin >> x >> y >> z;
  74.   Point segment_end = {x, y, z};
  75.   Segment segment_1 = {segment_begin, segment_end};
  76.  
  77.   std::cin >> x >> y >> z;
  78.   segment_begin = {x, y, z};
  79.   std::cin >> x >> y >> z;
  80.   segment_end = {x, y, z};
  81.   Segment segment_2 = {segment_begin, segment_end};
  82.  
  83.   std::cout.precision(8);
  84.   std::cout << std::fixed << FindDistanceBetweenSegments(segment_1, segment_2, 0.00000001);
  85.   return 0;
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement