Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define _CRT_SECURE_NO_WARNINGS
- #include <bits/stdc++.h>
- using namespace std;
- #define all(x) x.begin(), x.end()
- #define endl '\n'
- #define sz(x) (int)(x).size()
- #define mp(x, y) make_pair(x, y)
- #define pb push_back
- #define pii pair<int, int>
- #define rep(i, f, t) for (auto i = (f); i < (t); ++i)
- #define ui unsigned int
- #define ll long long
- #define double long double
- #define ull unsigned long long
- const int inf = (int)(2e9);
- const ll INF = (ll)(1e18);
- const int MOD = (int)(1e9 + 7);
- const double eps = (double)(1e-9);
- const double pi = acos(-1);
- const int maxn = (int)(1e5 + 10);
- const int maxm = (int)(2e5 + 10);
- void solve();
- template <typename A> inline void print(A x) { cout << x << endl; }
- template <typename A, typename B> inline void print(A x, B y) { cout << x << ' ' << y << endl; }
- template <typename A, typename B, typename C> inline void print(A x, B y, C z) { cout << x << ' ' << y << ' ' << z << endl; }
- template <typename A> inline void in(A &x) { cin >> x; }
- template <typename A, typename B> inline void in(A &x, B &y) { cin >> x >> y; }
- template <typename A, typename B, typename C> inline void in(A &x, B &y, C &z) { cin >> x >> y >> z; }
- template <typename A, typename B, typename C, typename D> inline void in(A &x, B &y, C &z, D &p) { cin >> x >> y >> z >> p; }
- template <typename A> inline void read(A begin, A end) { while (begin != end) cin >> *(begin++); }
- template <typename A> inline void write(A begin, A end) { while (begin != end) { cout << *(begin++) << ' '; } cout << endl; }
- signed main()
- {
- std::ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
- srand((ui)(time(0)));
- cout << fixed << setprecision(20);
- solve();
- return 0;
- }
- struct Point {
- double x;
- double y;
- Point() {}
- Point(const double& _x, const double& _y) {
- x = _x;
- y = _y;
- }
- };
- bool operator==(const Point& a, const Point& b) {
- return a.x == b.x && a.y == b.y;
- }
- double polar_angle(const Point& p) {
- double alpha = atan2(p.y, p.x);
- if (alpha < 0) alpha += 2 * pi;
- return alpha;
- }
- istream& operator>>(istream& in, Point& p)
- {
- in >> p.x >> p.y;
- return in;
- }
- ostream& operator<<(ostream& out, Point& p) {
- out << p.x << ' ' << p.y;
- return out;
- }
- struct Vector {
- double x;
- double y;
- Vector() {}
- Vector(const double& _x, const double& _y) {
- x = _x;
- y = _y;
- }
- Vector(const Point& a, const Point& b) {
- x = b.x - a.x;
- y = b.y - a.y;
- }
- };
- double len(const Vector& a) {
- return sqrt(a.x * a.x + a.y * a.y);
- }
- double scalar_product(const Vector& a, const Vector& b)
- {
- return a.x * b.x + a.y * b.y;
- }
- double cross_product(const Vector& a, const Vector& b)
- {
- return a.x * b.y - b.x * a.y;
- }
- double alpha(const Vector& a, const Vector& b) {
- return acos(scalar_product(a, b) / (len(a) * len(b)));
- }
- Vector norm(const Vector& a) {
- Vector res;
- double l = len(a);
- res.x = a.x / l;
- res.y = a.y / l;
- return res;
- }
- struct Line {
- double a;
- double b;
- double c;
- Line() {}
- Line(const double& _A, const double& _B, const double& _C) {
- a = _A;
- b = _B;
- c = _C;
- }
- Line(const Point& first_point, const Point& second_point) {
- a = second_point.y - first_point.y;
- b = first_point.x - second_point.x;
- c = second_point.x * first_point.y - first_point.x * second_point.y;
- }
- };
- istream& operator>>(istream& in, Line& line) {
- in >> line.a >> line.b >> line.c;
- return in;
- }
- bool point_in_line(const Point& p, const Line& line) {
- return abs(line.a * p.x + line.b * p.y + line.c) < eps;
- }
- double dist_to_line_unsigned(const Point& p, const Line& line) {
- return abs(line.a * p.x + line.b * p.y + line.c) / sqrt(line.a * line.a + line.b * line.b);
- }
- double dist_to_line_signed(const Point& p, const Line& line) {
- return -(line.a * p.x + line.b * p.y + line.c) / sqrt(line.a * line.a + line.b * line.b);
- }
- Point lines_intersection(const Line& first_line, const Line& second_line) {
- Point result;
- double matrix_determinant = (first_line.a * second_line.b - second_line.a * first_line.b);
- result.x = (first_line.b * second_line.c - second_line.b * first_line.c) / matrix_determinant;
- result.y = (first_line.c * second_line.a - second_line.c * first_line.a) / matrix_determinant;
- return result;
- }
- struct Segment {
- Point a;
- Point b;
- Segment() {}
- Segment(const Point& _a, const Point& _b) {
- a = _a;
- b = _b;
- }
- };
- istream& operator>>(istream& in, Segment& s) {
- in >> s.a >> s.b;
- return in;
- }
- double dist_to_segment(const Point& p, const Segment& seg) {
- Vector ab(seg.a, seg.b), ac(seg.a, p), bc(seg.b, p), ba(seg.b, seg.a);
- double result = min(len(ac), len(bc));
- Line line(seg.a, seg.b);
- if ((pi / 2.0) - alpha(ab, ac) > eps && (pi / 2.0) - alpha(bc, ba) > eps)
- result = min(result, dist_to_line_signed(p, line));
- return result;
- }
- bool point_in_segment(const Point& p, const Segment& seg) {
- double length = len(Vector(seg.a, seg.b));
- double len1 = len(Vector(seg.a, p)), len2 = len(Vector(seg.b, p));
- return (abs(length - (len1 + len2)) < eps);
- }
- Point a, b, c, d;
- Segment ab, cd;
- double v1_x, v2_x, v1_y, v2_y;
- double f(double t)
- {
- Point qa(a.x + v1_x * t, a.y + v1_y * t), qb(b.x + v1_x * t, b.y + v1_y * t), qc(c.x + v2_x * t, c.y + v2_y * t), qd(d.x + v2_x * t, d.y + v2_y * t);
- ab = Segment(qa, qb);
- cd = Segment(qc, qd);
- double ans = dist_to_segment(qa, cd);
- if (ans < 0) ans = 1000000.0;
- ans = min(ans, dist_to_segment(qb, cd));
- if (ans < 0) ans = 1000000.0;
- ans = min(ans, dist_to_segment(qc, ab));
- if (ans < 0) ans = 1000000.0;
- ans = min(ans, dist_to_segment(qd, ab));
- if (ans < 0) ans = 1000000.0;
- return ans;
- }
- double ternary_search()
- {
- double l = 0.0, r = 1e5;
- while (r - l > eps) {
- double m1 = l + (r - l) / 3;
- double m2 = r - (r - l) / 3;
- cout << f(m1) << ' ' << f(m2) << endl;
- if (f(m1) > f(m2))
- l = m1;
- else
- r = m2;
- }
- cout << f(l) << endl;
- return l;
- }
- void solve()
- {
- cin >> a >> b >> c >> d;
- cin >> v1_x >> v1_y >> v2_x >> v2_y;
- cout << ternary_search() << endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement