Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.idea
49 changes: 49 additions & 0 deletions geometry.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#include "geometry.h"
#include <math.h>
#include "ternary_search.h"

Point::Point(double a, double b, double c) : x (a), y (b), z (c) {}

Point::Point() : x (0), y (0), z (0) {}

Segment::Segment (const Point& a, const Point& b) {
one = a;
two = b;
}


// расстояние между точками
const double getDistance(const Point& one, const Point& two) {
return sqrt(pow((one.x - two.x), 2) + pow((one.y - two.y), 2) + pow((one.z - two.z), 2));
}

Point operator+(const Point& one, const Point& two) {
return Point(one.x + two.x, one.y + two.y, one.z + two.z);
}

Point operator*(const Point& p, double mult) {
return Point(p.x * mult, p.y * mult, p.z * mult);
}

Point operator/(const Point& p, double mult) {
return Point(p.x / mult, p.y / mult, p.z / mult);
}

double getDistancePointToSegment(const Point& p, const Segment& seg, double eps) {
auto func = [&p](const Point& p_to) {
return getDistance(p_to, p);
};
// 1ый аргумент - точка, до которой кратчайшее растояние от p, 2ой - p
Point least_p_in_seg = ternarySearchMin(seg.one, seg.two, func, getDistance, eps);
return getDistance(least_p_in_seg, p);
}


double getDistanceSegmentToSegment(const Segment& seg1, const Segment& seg2, double eps) {
auto func = [&seg2](const Point& p_to) {
return getDistancePointToSegment(p_to, seg2);
};
// 1ый аргумент - точка, до которой кратчайшее растояние от p, 2ой - p
Point x = ternarySearchMin(seg1.one, seg1.two, func, getDistance, eps);
return getDistancePointToSegment(x, seg2);
}
34 changes: 34 additions & 0 deletions geometry.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#ifndef INC_3TERM_CONTESTS_GEOMETRY_H
#define INC_3TERM_CONTESTS_GEOMETRY_H


struct Point {
double x, y, z;

Point (double a, double b, double c);

Point ();
};

struct Segment {
Point one, two;

Segment (const Point& a, const Point& b);
};


// расстояние между точками
const double getDistance(const Point& one, const Point& two);

Point operator+(const Point& one, const Point& two);

Point operator*(const Point& p, double mult);

Point operator/(const Point& p, double mult);

double getDistancePointToSegment(const Point& p, const Segment& seg, double eps = 0.0000001);

double getDistanceSegmentToSegment(const Segment& seg1, const Segment& seg2, double eps = 0.0000001);


#endif //INC_3TERM_CONTESTS_GEOMETRY_H
31 changes: 31 additions & 0 deletions mod3_a.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Даны два отрезка в пространстве (x1, y1, z1) - (x2, y2, z2) и (x3, y3, z3) - (x4, y4, z4).
* Найдите расстояние между отрезками.
*
* */


#include <iostream>
#include <math.h>
#include <stdio.h>
#include "geometry.h"


int main() {
int x, y, z;

std::cin >> x >> y >> z;
Point a (x, y, z);
std::cin >> x >> y >> z;
Point b (x, y, z);
std::cin >> x >> y >> z;
Point c (x, y, z);
std::cin >> x >> y >> z;
Point d (x, y, z);

Segment seg1 (c, d);
Segment seg2 (a, b);

printf("%.9lf", getDistanceSegmentToSegment(seg1, seg2));
return 0;
}
20 changes: 20 additions & 0 deletions ternary_search.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#ifndef INC_3TERM_CONTESTS_TERNARY_SEARCH_H
#define INC_3TERM_CONTESTS_TERNARY_SEARCH_H


template <typename T, class Func>
T ternarySearchMin(T left, T right, Func f, const double (*getDistance)(const T&, const T&), double eps = 0.0000001) {
while (getDistance(left, right) > eps) {
T a = (left * 2 + right) / 3;
T b = (left + right * 2) / 3;
if (f(a) < f(b)) {
right = b;
} else {
left = a;
}
}
return (left + right) / 2;
}


#endif //INC_3TERM_CONTESTS_TERNARY_SEARCH_H