Advertisement
MinhNGUYEN2k4

Segments

Jan 17th, 2022
1,157
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. struct Point{
  2.     double x, y;
  3.  
  4.     Point(double a = 0, double b = 0){
  5.         x = a;
  6.         y = b;
  7.     }
  8.  
  9.     Point(const Point &v){
  10.         x = v.x;
  11.         y = v.y;
  12.     }
  13.  
  14.     friend istream &operator >> (istream &stream, Point &v){
  15.         stream >> v.x >> v.y;
  16.         return stream;
  17.     }
  18.  
  19.     friend Point operator - (const Point &u, const Point &v){
  20.         return Point(u.x - v.x, u.y - v.y);
  21.     }
  22.  
  23.     friend double Cross(const Point &u, const Point &v){
  24.         return u.x*v.y - u.y*v.x;
  25.     }
  26.  
  27.     friend bool operator == (const Point &u, const Point &v){
  28.         return abs(u.x-v.x) <= ESP && abs(u.y-v.y) <= ESP;
  29.     }
  30.  
  31.     friend bool operator != (const Point &u, const Point &v){
  32.         return abs(u.x-v.x) > ESP || abs(u.y-v.y) > ESP;
  33.     }
  34.  
  35.     friend bool operator < (const Point &u, const Point &v){
  36.         return u != v && mp(u.x, u.y) < mp(v.x, v.y);
  37.     }
  38.  
  39.     friend ostream &operator << (ostream &stream, const Point &v){
  40.         stream << "(" << v.x << ", " << v.y << ")";
  41.         return stream;
  42.     }
  43. };
  44.  
  45. struct Line{
  46.     Point x, y;
  47.  
  48.     Line(Point u = Point(0, 0), Point v = Point(0, 0)){
  49.         x = u;
  50.         y = v;
  51.     }
  52.  
  53.     friend bool Intersect(const Line &u, const Line &v){
  54.         return Cross(u.x-u.y, v.x-u.y) * Cross(u.x-u.y, v.y-u.y) <= 0
  55.         && Cross(v.x-v.y, u.x-v.y) * Cross(v.x-v.y, u.y-v.y) <= 0;
  56.     }
  57.  
  58.     friend Point GiaoDiem(const Line &u, const Line &v){
  59.         Point nu = u.x - u.y;
  60.         Point nv = v.x - v.y;
  61.         //cout << nu.x << ' ' << nu.y << endl;
  62.         //cout << nv.x << ' ' << nv.y << endl;
  63.         double a1 = nu.y, b1 = -nu.x, c1 = u.x.x*nu.y - u.x.y*nu.x;
  64.         //cout << a1 << ' ' << b1 << ' ' << c1 << endl;
  65.         double a2 = nv.y, b2 = -nv.x, c2 = v.x.x*nv.y - v.x.y*nv.x;
  66.         //cout << a2 << ' ' << b2 << ' ' << c2 << endl;
  67.         if (a1 == 0){
  68.             swap(a1, a2);
  69.             swap(b1, b2);
  70.             swap(c1, c2);
  71.         }
  72.         //a1*x+b1*y=c1
  73.         //a2*x+b2*y=c2
  74.         double y = (c2-(a2*c1)/a1)/(b2-a2*b1/a1);
  75.         double x = (c1-b1*y)/a1;
  76.         return Point(x, y);
  77.     }
  78.  
  79. };
Advertisement
Advertisement
Advertisement
RAW Paste Data Copied
Advertisement