Advertisement
ivnikkk

Untitled

Jan 31st, 2022
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.14 KB | None | 0 0
  1. typedef long double ld;
  2.  
  3. const ld EPS = 1e-9;
  4.  
  5. #define Vec Point
  6.  
  7. int sign(ld x) {
  8.     if (x > EPS) return 1;
  9.     if (x < EPS) return -1;
  10.     return 0;
  11. }
  12.  
  13. ld sq(ld x) {
  14.     return x * x;
  15. }
  16.  
  17. struct Point {
  18.     ld x, y;
  19.     Point() : x(0), y(0) {}
  20.     Point(ld _x, ld _y) : x(_x), y(_y) {}
  21.     Point operator-(const Point& other) const{
  22.         return Point(x - other.x, y - other.y);
  23.     }
  24.     Point operator+(const Point& other) const {
  25.         return Point(x + other.x, y + other.y);
  26.     }
  27.     Point operator*(const ld& other) const {
  28.         return Point(x * other, y * other);
  29.     }
  30.     // векторное произведение sin
  31.     ld operator^(const Point& other) const {
  32.         return x * other.y - y * other.x;
  33.     }
  34.     // скалярное произведение cos
  35.     ld operator*(const Point& other) const {
  36.         return x * other.x + y * other.y;
  37.     }
  38.     ld len2() const{
  39.         return sq(x) + sq(y);
  40.     }
  41.     ld len() const{
  42.         return sqrtl(len2());
  43.     }
  44.     Point norm() const {
  45.         ld d = len();
  46.         return Point(x / d,y / d);
  47.     }
  48.     bool operator<(const Point& other) const {
  49.         if (sign(x - other.x) != 0) {
  50.             return x < other.x;
  51.         }
  52.         else if (sign(y - other.y) != 0) {
  53.             return y < other.y;
  54.         }
  55.         return false;
  56.     }
  57.     bool operator==(const Point& other) const {
  58.         return sign(x - other.x) == 0 && sign(y - other.y) == 0;
  59.     }
  60.     Point ort() {
  61.         return Point(-y, x);
  62.     }
  63.     void deb() const {
  64.         std::cerr << "(" << x << ", " << y << ")" << std::endl;
  65.     }
  66. };
  67. //расстояние от точки до точки
  68. ld dist_points(Point& a, Point& b) {
  69.     return sqrtl(sq(a.x - b.x) + sq(a.y - b.y));
  70. }
  71.  
  72. // угол между двумя векторами в радианах
  73. ld get_angle(const Vec& a, const Vec& b) {
  74.     return atan2(a ^ b, a * b);
  75. }
  76.  
  77. // проверка принадлежности точки отрезку
  78. bool is_point_on_seg(const Point& a, const Point& b, const Point& x) {
  79.     // проверка в тупую
  80.     //return sign((a - b).len() - (a - x).len() - (b - x).len()) == 0;
  81.     if (a == b) {
  82.         return a == x;
  83.     }
  84.     else {
  85.         return sign((x - a) ^ (b - a)) == 0 &&
  86.                sign((x - a) * (b - a)) == 1 &&
  87.                sign((x - b) * (a - b)) == 1;
  88.     }
  89. }
  90.  
  91. struct Line {
  92.    
  93.  
  94. };
  95.  
  96.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement