Advertisement
Gosunov

Basic geometry

Dec 14th, 2021
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.95 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. #define all(a) (a).begin(), (a).end()
  6.  
  7. const double pi = 3.14159265;
  8.  
  9. struct Point {
  10.     int x, y;
  11.  
  12.     Point() {}
  13.  
  14.     Point(int _x, int _y) {
  15.         x = _x;
  16.         y = _y;
  17.     }
  18.  
  19.     Point operator-(Point &p) const {
  20.         return Point(x - p.x, y - p.y);
  21.     }
  22. };
  23.  
  24. struct Vec {
  25.     int x, y;
  26.  
  27.     Vec() {}
  28.  
  29.     Vec(int _x, int _y) {
  30.         x = _x;
  31.         y = _y;
  32.     }
  33.  
  34.     Vec(Point end) {
  35.         x = end.x;
  36.         y = end.y;
  37.     }
  38.  
  39.     Vec(Point beg, Point end) {
  40.         x = end.x - beg.x;
  41.         y = end.y - beg.y;
  42.     }
  43.  
  44.     // scalar
  45.     double operator^(const Vec &v) const {
  46.         return x * v.x + y * v.y;
  47.     }
  48.  
  49.     // vector
  50.     double operator*(const Vec &v) const {
  51.         return x * v.y - v.x * y;
  52.     }
  53.  
  54.     double phi2() {
  55.         double a = atan2(y, x);
  56.         if (a < 0)
  57.             a += 2 * pi;
  58.         return a;
  59.     }
  60.  
  61.     double phi() {
  62.         return atan2(y, x);
  63.     }
  64.  
  65.     double len() {
  66.         return sqrt(x * x + y * y);
  67.     }
  68. };
  69.  
  70. struct Segment {
  71.     Point a, b;
  72.  
  73.     Segment() {}
  74.  
  75.     Segment(Point _a, Point _b) {
  76.         a = _a;
  77.         b = _b;
  78.     }
  79. };
  80.  
  81. double dist(Segment &s, Point &c) {
  82.     Vec ab(s.a, s.b);
  83.     Vec ac(s.a, c);
  84.     Vec bc(s.b, c);
  85.     if ((ac ^ ab) > 0 && (bc ^ ab) < 0)
  86.         return abs((ab * ac) / ab.len());
  87.  
  88.     return min(ac.len(), bc.len());
  89. }
  90.  
  91. istream& operator>>(istream& in, Point &p) {
  92.     return in >> p.x >> p.y;
  93. }
  94.  
  95. istream& operator>>(istream& in, Vec &v) {
  96.     return in >> v.x >> v.y;
  97. }
  98.  
  99. istream& operator>>(istream& in, Segment &s) {
  100.     return in >> s.a >> s.b;
  101. }
  102.  
  103. void solve() {
  104.     Point a;
  105.     cin >> a;
  106.     Segment s;
  107.     cin >> s;
  108.     cout << dist(s, a) << '\n';
  109. }
  110.  
  111. signed main() {
  112.     ios_base::sync_with_stdio(0);
  113.     cin.tie(0);
  114.     cout.setf(ios_base::boolalpha);
  115.     int t = 1;
  116.     // cin >> t;
  117.     while (t--)
  118.         solve();
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement