Advertisement
didedoshka

itll be fun

Jun 10th, 2020
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.91 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4. #include <queue>
  5. #include <cmath>
  6. #include <set>
  7. #include <stack>
  8. #include <bitset>
  9. #include <map>
  10.  
  11. #ifndef M_PI
  12. #define M_PI 3.141592653589
  13. #endif
  14. //#define int long long
  15. #define double long double
  16. #define f(variable, start, end) for (int variable = start; variable < end; ++variable)
  17. #define be(a) a.begin(), a.end()
  18. #define no cout << "NO\n"
  19. #define yes cout << "YES\n"
  20. #define nor cout << "NO\n"; return
  21. #define yesr cout << "YES\n"; return
  22. #define vi vector<int>
  23. #define vvi vector<vector<int>>
  24.  
  25.  
  26. using namespace std;
  27.  
  28.  
  29. template<typename T, typename E>
  30. vector<T> &operator>>(vector<T> &v, E a) {
  31.     v.push_back(a);
  32.     return v;
  33. }
  34.  
  35. //abs for any type
  36. template<typename T>
  37. T abs(T a) {
  38.     if (0 < a) {
  39.         return a;
  40.     } else {
  41.         return -a;
  42.     }
  43. }
  44.  
  45. //pair output
  46. template<typename E>
  47. ostream &operator<<(ostream &os, pair<E, E> &t) {
  48.     os << t.first << ' ' << t.second << '\n';
  49.     return os;
  50. }
  51.  
  52. //"map" pair output
  53. template<typename E>
  54. ostream &operator<<(ostream &os, pair<const E, E> &t) {
  55.     os << t.first << ' ' << t.second << '\n';
  56.     return os;
  57. }
  58.  
  59. //vector output
  60. template<typename T>
  61. ostream &operator<<(ostream &os, vector<T> &vec) {
  62.     for (T i : vec) {
  63.         os << i << '\n';
  64.     }
  65.     cout << '\n';
  66.     return os;
  67. }
  68.  
  69. //2 dimensional vector output
  70. template<typename T>
  71. ostream &operator<<(ostream &os, vector<vector<T> > &vec) {
  72.     for (vector<T> i : vec) {
  73.         os << i;
  74.     }
  75.     return os;
  76. }
  77.  
  78. struct Point {
  79.     double x, y;
  80.  
  81.     Point() {
  82.         x = 0;
  83.         y = 0;
  84.     }
  85.  
  86.     double dist(Point a) const {
  87.         return sqrt((a.x - x) * (a.x - x) + (a.y - y) * (a.y - y));
  88.     }
  89.  
  90. };
  91.  
  92. bool eq(double a, double b) {
  93.     return fabs(a - b) <= 1e-7;
  94. }
  95.  
  96. bool ge(double a, double b) {
  97.     return a > b && !eq(a, b);
  98. }
  99.  
  100. struct Vector {
  101.     double x, y;
  102.  
  103.     Vector(Point A, Point B) {
  104.         x = B.x - A.x;
  105.         y = B.y - A.y;
  106.     }
  107.  
  108.     double len() const {
  109.         return sqrt(x * x + y * y);
  110.     }
  111.  
  112.     double dot_product (Vector a) const {
  113.         return x * a.x + y * a.y;
  114.     }
  115.  
  116.     double cross_product (Vector a) const {
  117.         return x * a.y - y * a.x;
  118.     }
  119. };
  120.  
  121. istream &operator>>(istream &in, Point &P) {
  122.     in >> P.x >> P.y;
  123.     return in;
  124. }
  125.  
  126. ostream &operator<<(ostream &out, Point &P) {
  127.     out << P.x << " " << P.y;
  128.     return out;
  129. }
  130.  
  131.  
  132. int32_t main() {
  133.     cin.tie(NULL);
  134.     cout.tie(NULL);
  135.     cout.setf(ios::fixed);
  136.     cout.precision(10);
  137.     ios_base::sync_with_stdio(false);
  138.  
  139.     Point P, A, B;
  140.     cin >> P >> A >> B;
  141.     Vector AB(A, B);
  142.     Vector BA(B, A);
  143.     Vector AP(A, P);
  144.     Vector BP(B, P);
  145.     if (ge(AB.dot_product(AP), 0) && ge(BA.dot_product(BP), 0))
  146.         cout << fabs(AB.cross_product(AP)) / AB.len() << endl;
  147.     else
  148.         cout << min(P.dist(A), P.dist(B)) << endl;
  149.  
  150.     return 0;
  151. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement