Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cmath>
- #include <iostream>
- struct Point {
- double x = 0.0, y = 0.0, z = 0.0;
- Point operator *(double a) const {
- return {x * a, y * a, z * a};
- }
- Point operator /(double a) const {
- return {x / a, y / a, z / a};
- }
- Point operator +(const Point& another) const {
- return {another.x + this->x, another.y + this->y, another.z + this->z};
- }
- };
- struct Segment {
- Point begin, end;
- };
- double FindDistanceBetweenPoints(const Point& point_1, const Point& point_2) {
- 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));
- }
- double FindDistanceBetweenSegmentAndPoint(const Segment& segment, const Point& point, double eps) {
- Point right = segment.end;
- Point left = segment.begin;
- double dist_left = 0.0;
- double dist_right = 0.0;
- do {
- Point left_candidate = (left * 2 + right) / 3;
- Point right_candidate = (left + right * 2) / 3;
- dist_left = FindDistanceBetweenPoints(point, left_candidate);
- dist_right = FindDistanceBetweenPoints(point, right_candidate);
- if (dist_left < dist_right) {
- right = right_candidate;
- } else {
- left = left_candidate;
- }
- } while (FindDistanceBetweenPoints(left, right) > eps);
- return dist_left;
- }
- double FindDistanceBetweenSegments(const Segment& first, const Segment& second, double eps) {
- Point right = second.end;
- Point left = second.begin;
- double dist_left = 0.0;
- double dist_right = 0.0;
- do {
- Point left_candidate = (left * 2 + right) / 3;
- Point right_candidate = (left + right * 2) / 3;
- dist_left = FindDistanceBetweenSegmentAndPoint(first, left_candidate, eps);
- dist_right = FindDistanceBetweenSegmentAndPoint(first, right_candidate, eps);
- if (dist_left < dist_right) {
- right = right_candidate;
- } else {
- left = left_candidate;
- }
- } while (FindDistanceBetweenPoints(left, right) > eps);
- return dist_left;
- }
- int main() {
- double x, y, z;
- std::cin >> x >> y >> z;
- Point segment_begin = {x, y, z};
- std::cin >> x >> y >> z;
- Point segment_end = {x, y, z};
- Segment segment_1 = {segment_begin, segment_end};
- std::cin >> x >> y >> z;
- segment_begin = {x, y, z};
- std::cin >> x >> y >> z;
- segment_end = {x, y, z};
- Segment segment_2 = {segment_begin, segment_end};
- std::cout.precision(8);
- std::cout << std::fixed << FindDistanceBetweenSegments(segment_1, segment_2, 0.00000001);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement