Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cmath>
- #include <stdio.h>
- #include <vector>
- #include <stack>
- #include <algorithm>
- #include <iomanip>
- const long double EPS = 0.000001;
- const int SZ = 10000;
- using namespace std;
- struct point
- {
- long double x, y;
- };
- struct vec
- {
- long double x, y;
- vec(point a, point b)
- {
- this->x = b.x - a.x;
- this->y = b.y - a.y;
- }
- vec(point a)
- {
- this->x = a.x;
- this->y = a.y;
- }
- vec()
- {
- this->x = 0;
- this->y = 0;
- }
- vec(double x, double y)
- {
- this->x = x;
- this->y = y;
- }
- double l()
- {
- return sqrt(x * x + y * y);
- }
- };
- double scal_pr(vec a, vec b)
- {
- return a.x * b.x + a.y * b.y;
- }
- vec operator*(vec a, long double b)
- {
- a.x *= b;
- a.y *= b;
- return a;
- }
- void operator*=(vec& a, long double b)
- {
- a.x *= b;
- a.y *= b;
- }
- vec operator/(vec a, long double b)
- {
- a.x /= b;
- a.y /= b;
- return a;
- }
- void operator/=(vec& a, long double b)
- {
- a.x /= b;
- a.y /= b;
- }
- template<class T>
- T operator+(T a, vec b)
- {
- a.x += b.x;
- a.y += b.y;
- return a;
- }
- struct line
- {
- long double a, b, c;
- line(long double a = 0, long double b = 1, long double c = 0)
- {
- this->a = a;
- this->b = b;
- this->c = c;
- }
- line(point s, long double a, long double b)
- {
- this->a = a;
- this->b = b;
- this->c = -(a * s.x + b * s.y);
- }
- };
- struct segment
- {
- point p1, p2;
- line get_line()
- {
- double a = p2.y - p1.y, b = p1.x - p2.x;
- return line(a, b, -(a * p1.x + b * p1.y));
- }
- };
- segment operator+(segment a, vec v)
- {
- a.p1 = a.p1 + v;
- a.p2 = a.p2 + v;
- return a;
- }
- segment s1, s2;
- vec v1, v2;
- long double dist(point a, point b)
- {
- return sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y));
- }
- double dist(line l, point p)
- {
- //cout << l.a << " " << l.b << " " << l.c << "\n";
- return (l.a * p.x + l.b * p.y + l.c) / sqrt(l.a * l.a + l.b * l.b);
- }
- long double dist(segment a, point p)
- {
- if (scal_pr(vec(a.p1, a.p2), vec(a.p1, p)) < 0 || scal_pr(vec(a.p2, a.p1), vec(a.p2, p)) < 0)
- return min(dist(p, a.p1), dist(p, a.p2));
- else
- return fabs(dist(a.get_line(), p));
- }
- bool cros(segment a, segment b)
- {
- if (dist(a.get_line(), b.p1) * dist(a.get_line(), b.p2) < 0 && dist(b.get_line(), a.p1) * dist(b.get_line(), a.p2) < 0)
- return 1;
- else
- return 0;
- }
- long double dist(segment a, segment b)
- {
- if (cros(a, b))
- return 0;
- //cout << min(min(dist(a, b.p1), dist(a, b.p2)), min(dist(b, a.p1), dist(b, a.p2))) << "@\n";
- return min(min(dist(a, b.p1), dist(a, b.p2)), min(dist(b, a.p1), dist(b, a.p2)));
- }
- long double dist(long double t)
- {
- /*if ((s1 + (v1 * t)).p1.x < 100)
- {
- cout << "wewewe";
- }*/
- //cout << t << " " << dist(s1 + (v1 * t), s2 + (v2 * t)) << "\n";
- return dist(s1 + (v1 * t), s2 + (v2 * t));
- }
- //#define IS_DEBUG
- int main()
- {
- #if !defined IS_DEBUG
- freopen("love.in", "r", stdin);
- freopen("love.out", "w", stdout);
- #endif
- cin >> s1.p1.x >> s1.p1.y >> s1.p2.x >> s1.p2.y >> s2.p1.x >> s2.p1.y >> s2.p2.x >> s2.p2.y >> v1.x >> v1.y >> v2.x >> v2.y;
- long double l = 0, c1, c2, r = 2000000;
- while (r - l > EPS)
- {
- //cout << l << " " << r << "\n";
- c1 = (2 * l + r) / 3;
- c2 = (2 * r + l) / 3;
- //cout << c1 << " " << c2 << "\n";
- if (dist(c1) > dist(c2))
- {
- l = c1;
- //cout << "l\n";
- }
- else
- {
- r = c2;
- //cout << "r\n";
- }
- }
- if (fabs(dist(r)) > EPS)
- cout << -1;
- else
- cout << setprecision(5) << r;
- #if !defined IS_DEBUG
- fclose(stdin);
- fclose(stdout);
- #endif
- return 0;
- }
- /*
- 0 0 -1 3
- 4 4 7 7
- 3 0
- 0 -1
- 0 0 -1 3
- 4 4 7 7
- 1 0
- 0 -3
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement