Advertisement
Guest User

B

a guest
Sep 16th, 2019
2,520
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.49 KB | None | 0 0
  1. #include <algorithm>
  2. #include <cstdio>
  3. #include <vector>
  4. #include <cmath>
  5.  
  6. using namespace std;
  7.  
  8. struct point {
  9.     long double x;
  10.     long double y;
  11.  
  12.     long double len() {
  13.         return sqrt(x * x + y * y);
  14.     }
  15. };
  16.  
  17. point operator-(point a, point b) {
  18.     point res;
  19.  
  20.     res.x = a.x - b.x;
  21.     res.y = a.y - b.y;
  22.  
  23.     return res;
  24. }
  25.  
  26. long double height(point a, point b, point c) {
  27.     point base = a - c;
  28.     long double s = (b.x - a.x) * (c.y - a.y) - (b.y - a.y) * (c.x - a.x);
  29.  
  30.     if (base.len() == 0) {
  31.         return 0;
  32.     }
  33.  
  34.     return (abs(s) / base.len());
  35. }
  36.  
  37. int main() {
  38.  
  39.     point p, a, b;
  40.     long double x, y, z;
  41.  
  42.     scanf("%Lf %Lf %Lf %Lf %Lf %Lf", &p.x, &p.y, &a.x, &a.y, &b.x, &b.y);
  43.  
  44.     if (p.x == a.x && p.x == b.x) {
  45.         if ((a.y < p.y && b.y > p.y) || (b.y < p.y && a.y > p.y)) {
  46.             printf("0");
  47.         } else {
  48.             printf("%.5Lf", min(abs(p.y - a.y), abs(p.y - b.y)));
  49.         }
  50.  
  51.         return 0;
  52.     }
  53.  
  54.     if (p.y == a.y && p.y == b.y) {
  55.         if ((a.x < p.x && b.x > p.x) || (b.x < p.x && a.x > p.x)) {
  56.             printf("0");
  57.         } else {
  58.             printf("%.5Lf", min(abs(p.x - a.x), abs(p.x - b.x)));
  59.         }
  60.  
  61.         return 0;
  62.     }
  63.  
  64.     x = (p - b).len() * (p - b).len();
  65.     y = (p - a).len() * (p - a).len();
  66.     z = (a - b).len() * (a - b).len();
  67.  
  68.     if (x <= y + z && y <= x + z) {
  69.         printf("%.5Lf", height(a, p, b));
  70.     } else {
  71.         printf("%.5Lf", sqrt(min(x, y)));
  72.     }
  73.  
  74.  
  75.     return 0;
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement