Advertisement
Guest User

Untitled

a guest
Aug 17th, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.08 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3. #include <stdio.h>
  4. #include <vector>
  5. #include <stack>
  6. #include <algorithm>
  7. #include <iomanip>
  8.  
  9. const long double EPS = 0.000001;
  10. const int SZ = 10000;
  11.  
  12. using namespace std;
  13.  
  14. struct point
  15. {
  16.     long double x, y;
  17. };
  18.  
  19. struct vec
  20. {
  21.     long double x, y;
  22.     vec(point a, point b)
  23.     {
  24.         this->x = b.x - a.x;
  25.         this->y = b.y - a.y;
  26.     }
  27.     vec(point a)
  28.     {
  29.         this->x = a.x;
  30.         this->y = a.y;
  31.     }
  32.     vec()
  33.     {
  34.         this->x = 0;
  35.         this->y = 0;
  36.     }
  37.     vec(double x, double y)
  38.     {
  39.         this->x = x;
  40.         this->y = y;
  41.     }
  42.     double l()
  43.     {
  44.         return sqrt(x * x + y * y);
  45.     }
  46. };
  47.  
  48. double scal_pr(vec a, vec b)
  49. {
  50.     return a.x * b.x + a.y * b.y;
  51. }
  52.  
  53. vec operator*(vec a, long double b)
  54. {
  55.     a.x *= b;
  56.     a.y *= b;
  57.     return a;
  58. }
  59.  
  60. void operator*=(vec& a, long double b)
  61. {
  62.     a.x *= b;
  63.     a.y *= b;
  64. }
  65.  
  66. vec operator/(vec a, long double b)
  67. {
  68.     a.x /= b;
  69.     a.y /= b;
  70.     return a;
  71. }
  72.  
  73. void operator/=(vec& a, long double b)
  74. {
  75.     a.x /= b;
  76.     a.y /= b;
  77. }
  78.  
  79. template<class T>
  80. T operator+(T a, vec b)
  81. {
  82.     a.x += b.x;
  83.     a.y += b.y;
  84.     return a;
  85. }
  86.  
  87. struct line
  88. {
  89.     long double a, b, c;
  90.     line(long double a = 0, long double b = 1, long double c = 0)
  91.     {
  92.         this->a = a;
  93.         this->b = b;
  94.         this->c = c;
  95.     }
  96.     line(point s, long double a, long double b)
  97.     {
  98.         this->a = a;
  99.         this->b = b;
  100.         this->c = -(a * s.x + b * s.y);
  101.     }
  102. };
  103.  
  104. struct segment
  105. {
  106.     point p1, p2;
  107.     line get_line()
  108.     {
  109.         double a = p2.y - p1.y, b = p1.x - p2.x;
  110.         return line(a, b, -(a * p1.x + b * p1.y));
  111.     }
  112. };
  113.  
  114. segment operator+(segment a, vec v)
  115. {
  116.     a.p1 = a.p1 + v;
  117.     a.p2 = a.p2 + v;
  118.     return a;
  119. }
  120.  
  121. segment s1, s2;
  122. vec v1, v2;
  123.  
  124. long double dist(point a, point b)
  125. {
  126.     return sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y));
  127. }
  128.  
  129. double dist(line l, point p)
  130. {
  131.     //cout << l.a << " " << l.b << " " << l.c <<  "\n";
  132.     return (l.a * p.x + l.b * p.y + l.c) / sqrt(l.a * l.a + l.b * l.b);
  133. }
  134.  
  135. long double dist(segment a, point p)
  136. {
  137.     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)
  138.         return min(dist(p, a.p1), dist(p, a.p2));
  139.     else
  140.         return fabs(dist(a.get_line(), p));
  141. }
  142.  
  143. bool cros(segment a, segment b)
  144. {
  145.     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)
  146.         return 1;
  147.     else
  148.         return 0;
  149. }
  150.  
  151. long double dist(segment a, segment b)
  152. {
  153.     if (cros(a, b))
  154.         return 0;
  155.     //cout << min(min(dist(a, b.p1), dist(a, b.p2)), min(dist(b, a.p1), dist(b, a.p2))) << "@\n";
  156.     return min(min(dist(a, b.p1), dist(a, b.p2)), min(dist(b, a.p1), dist(b, a.p2)));
  157. }
  158.  
  159. long double dist(long double t)
  160. {
  161.     /*if ((s1 + (v1 * t)).p1.x < 100)
  162.     {
  163.         cout << "wewewe";
  164.     }*/
  165.     //cout << t << " " << dist(s1 + (v1 * t), s2 + (v2 * t)) << "\n";
  166.     return dist(s1 + (v1 * t), s2 + (v2 * t));
  167. }
  168.  
  169. //#define IS_DEBUG
  170.  
  171. int main()
  172. {
  173.     #if !defined IS_DEBUG
  174.         freopen("love.in", "r", stdin);
  175.         freopen("love.out", "w", stdout);
  176.     #endif
  177.     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;
  178.     long double l = 0, c1, c2, r = 2000000;
  179.     while (r - l > EPS)
  180.     {
  181.         //cout << l << " " << r << "\n";
  182.         c1 = (2 * l + r) / 3;
  183.         c2 = (2 * r + l) / 3;
  184.         //cout << c1 << " " << c2 << "\n";
  185.         if (dist(c1) > dist(c2))
  186.         {
  187.             l = c1;
  188.             //cout << "l\n";
  189.         }
  190.         else
  191.         {
  192.             r = c2;
  193.             //cout << "r\n";
  194.         }
  195.  
  196.     }
  197.     if (fabs(dist(r)) > EPS)
  198.         cout << -1;
  199.     else
  200.         cout << setprecision(5) << r;
  201.     #if !defined IS_DEBUG
  202.         fclose(stdin);
  203.         fclose(stdout);
  204.     #endif
  205.     return 0;
  206. }
  207. /*
  208. 0 0 -1 3
  209. 4 4 7 7
  210. 3 0
  211. 0 -1
  212.  
  213. 0 0 -1 3
  214. 4 4 7 7
  215. 1 0
  216. 0 -3
  217.  
  218. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement