Advertisement
Mlxa

2D геома на точках.

Aug 3rd, 2018
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.29 KB | None | 0 0
  1. #define all(x) begin(x), end(x)
  2. #define equal           std_equal
  3. #define not_equal       std_not_equal
  4. #define less            std_less
  5. #define less_equal      std_less_equal
  6. #define greater         std_greater
  7. #define greater_equal   std_greater_equal
  8. #define map             std_map
  9. #define vector          std_vector
  10. #include <bits/stdc++.h>
  11. #undef equal
  12. #undef not_equal
  13. #undef less
  14. #undef less_equal
  15. #undef greater
  16. #undef greater_equal
  17. #undef map
  18. #undef vector
  19. using namespace std;
  20.  
  21. template<class A>
  22. void addlog(A a) {
  23.     cerr << a << endl;
  24. }
  25. template<class A, class... B>
  26. void addlog(A a, B... b) {
  27.     cerr << a << ' ';
  28.     addlog(b...);
  29. }
  30.  
  31. typedef long double float80_t;
  32. static_assert(sizeof(float80_t) >= 10, "it's not float80_t");
  33. const float80_t eps = { 1e-9 };
  34. inline bool equal(float80_t a, float80_t b) {
  35.     return fabs(a - b) <= eps;
  36. }
  37. inline bool not_equal(float80_t a, float80_t b) {
  38.     return fabs(a - b) > eps;
  39. }
  40. inline bool less(float80_t a, float80_t b) {
  41.     return a < b - eps;
  42. }
  43. inline bool greater(float80_t a, float80_t b) {
  44.     return a > b + eps;
  45. }
  46. inline bool less_equal(float80_t a, float80_t b) {
  47.     return a <= b + eps;
  48. }
  49. inline bool greater_equal(float80_t a, float80_t b) {
  50.     return a >= b - eps;
  51. }
  52. void accurate_output() {
  53.     cout.precision(19);
  54.     cerr.precision(19);
  55.     cout << fixed;
  56.     cerr << fixed;
  57. }
  58.  
  59. float80_t to_radians(float80_t x)
  60. {   return x * M_PI / 180;      }
  61. float80_t to_degrees(float80_t x)
  62. {   return x * 180 / M_PI;      }
  63. int64_t to_int(float80_t x) {
  64.     float80_t r = round(x + 0.5);
  65.     assert(equal(r, x));
  66.     return (int64_t)(r);
  67. }
  68.  
  69. struct point {
  70.     float80_t x, y;
  71.     point() : x(0), y(0) {}
  72.     point(float80_t x_, float80_t y_) : x(x_), y(y_) {}
  73.     friend ostream &operator<<(ostream &out, point p)
  74.     {   return out << p.x << ' ' << p.y;    }
  75.     friend istream &operator>>(istream &in, point &p)
  76.     {   return in >> p.x >> p.y;    }
  77. };
  78. struct vector : public point {
  79.     vector() : point() {}
  80.     vector(float80_t x_, float80_t y_) : point(x_, y_) {}
  81.     vector(point a, point b) : point(b.x - a.x, b.y - a.y) {}
  82.     float80_t len_square() const
  83.     {   return x * x + y + y;   }
  84.     float80_t len() const
  85.     {   return sqrt((float80_t)(len_square())); }
  86.     float80_t ang() const
  87.     {   return atan2(y, x); }
  88.     friend float80_t ang(vector a, vector b)
  89.     {   return atan2(cross_product(a, b), dot_product(a, b));   }
  90.     vector operator-()
  91.     {   return vector(-x, -y);      }
  92.     vector operator*(float80_t c)
  93.     {   return vector(x * c, y * c);    }
  94.     vector operator/(float80_t c)
  95.     {   return vector(x / c, y / c);    }
  96.     friend point operator+(point a, vector b)
  97.     {   return point(a.x + b.x, a.y + b.y); }
  98.     friend point operator-(point a, vector b)
  99.     {   return a + (-b);    }
  100.     friend point &operator+=(point &a, vector b)
  101.     {   return a = a + b;   }
  102.     friend point &operator-=(point &a, vector b)
  103.     {   return a = a - b;   }
  104.     friend float80_t dot_product(vector a, vector b)
  105.     {   return a.x * b.x + a.y * b.y;   }
  106.     friend float80_t cross_product(vector a, vector b)
  107.     {   return a.x * b.y - b.x * a.y;   }
  108.     friend vector operator+(vector a, vector b)
  109.     {   return vector(a.x + b.x, a.y + b.y);    }
  110.     friend vector operator-(vector a, vector b)
  111.     {   return a + (-b);    }
  112.     vector &operator+=(vector other)
  113.     {   return (*this) = (*this) + other;   }
  114.     vector &operator-=(vector other)
  115.     {   return (*this) = (*this) - other;   }
  116.     vector normalized(float80_t len_need=1)
  117.     {   return vector(x * len_need / len(), y * len_need / len());      }
  118.     vector rotated(float80_t cosa, float80_t sina)
  119.     {   return vector(x * cosa - y * sina, x * sina + y * cosa);        }
  120.     vector rotated(float80_t angle)
  121.     {   return rotated(cos(angle), sin(angle));     }
  122.     static vector from_polar(float80_t angle, float80_t len_need=1)
  123.     {   return vector(len_need * cos(angle), len_need * sin(angle));    }
  124. };
  125.  
  126. typedef int32_t cross_t;
  127. const cross_t collapse = 1e9;
  128. bool equal(point a, point b) {
  129.     return equal(a.x, b.x) && equal(a.y, b.y);
  130. }
  131. bool not_equal(point a, point b) {
  132.     return not_equal(a.x, b.x) || not_equal(a.y, b.y);
  133. }
  134. bool on_line(point o, point a, point b) {
  135.     assert(not_equal(a, b));
  136.     return equal(cross_product(vector(o, a), vector(o, b)), 0);
  137. }
  138. bool on_ray(point o, point a, point b) {
  139.     assert(not_equal(a, b));
  140.     return  on_line(o, a, b)
  141.     &&      greater_equal(dot_product(vector(a, o), vector(a, b)), 0);
  142. }
  143. bool on_segment(point o, point a, point b) {
  144.     if (equal(a, b)) {
  145.         assert(false);
  146.         return equal(a, o);
  147.     } else {
  148.         return  on_line(o, a, b)
  149.         &&      less(dot_product(vector(o, a), vector(o, b)), 0);
  150.     }
  151. }
  152.  
  153. float80_t signed_triangle_square(point a, point b, point c)
  154. {   return cross_product(vector(a, b), vector(a, c)) / 2;       }
  155. float80_t triangle_square(point a, point b, point c)
  156. {   return fabs(signed_triangle_square(a, b, c));   }
  157.  
  158. cross_t cross_lines(point a, point b, point c, point d) {
  159.     assert(not_equal(a, b));
  160.     assert(not_equal(c, d));
  161.     float80_t s_acd = signed_triangle_square(a, c, d);
  162.     float80_t s_bcd = -signed_triangle_square(b, c, d);
  163.     if (equal(s_acd, -s_bcd)) {
  164.         return equal(s_acd, 0) ? collapse : 0;
  165.     } else {
  166.         return 1;
  167.     }
  168. }
  169.  
  170. point line_intersection(point a, point b, point c, point d) {
  171.     assert(cross_lines(a, b, c, d) == 1);
  172.     float80_t s_acd = signed_triangle_square(a, c, d);
  173.     float80_t s_bcd = -signed_triangle_square(b, c, d);
  174.     return a + (vector(a, b) * s_acd / (s_acd + s_bcd));
  175. }
  176.  
  177. void run();
  178. int main() {
  179. #ifdef LOCAL
  180.     while (true)
  181. #endif
  182.         run();
  183.     return 0;
  184. }
  185.  
  186.  
  187. void run() {
  188.     ;
  189. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement