smatskevich

GeometryTemplate

Jan 22nd, 2026 (edited)
213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.15 KB | None | 0 0
  1. const ld PI = atan2l(0., -1.);
  2.  
  3. struct Point { ld x; ld y; };
  4. struct Vector {
  5.   ld x; ld y;
  6.   ld Len() const { return sqrtl(x * x + y * y); }
  7.   ld Len2() const { return x * x + y * y; }
  8. };
  9. struct Line {
  10.   ld a; ld b; ld c;
  11.   Line(const Point& p1, const Point& p2) { a = p1.y - p2.y; b = p2.x - p1.x; c = -a * p1.x - b * p1.y; }
  12.   Line(const Point& p, const Vector& v) { a = v.y; b = -v.x; c = -a * p.x - b * p.y; }
  13. };
  14.  
  15. bool operator==(const Point& p1, const Point& p2) { return p1.x == p2.x && p1.y == p2.y; }
  16. Point operator-(const Point& p) { return {-p.x, -p.y}; }
  17. Point operator+(const Point& p, const Vector& v) { return {p.x + v.x, p.y + v.y}; }
  18. Point operator+(const Vector& v, const Point& p) { return {p.x + v.x, p.y + v.y}; }
  19. Point operator-(const Point& p, const Vector& v) { return {p.x - v.x, p.y - v.y}; }
  20. Vector operator-(const Point& p1, const Point& p2) { return {p1.x - p2.x, p1.y - p2.y}; }
  21. Vector operator+(const Vector& v1, const Vector& v2) { return {v1.x + v2.x, v1.y + v2.y};}
  22. Vector operator-(const Vector& v1, const Vector& v2) { return {v1.x - v2.x, v1.y - v2.y}; }
  23. ld DotProduct(const Vector& v1, const Vector& v2) { return v1.x * v2.x + v1.y * v2.y; }
  24. ld CrossProduct(const Vector& v1, const Vector& v2) { return v1.x * v2.y - v1.y * v2.x; }
  25. istream& operator>>(istream& in, Point& v) { return in >> v.x >> v.y; }
  26. istream& operator>>(istream& in, Vector& v) { return in >> v.x >> v.y; }
  27. ostream& operator<<(ostream& out, const Point& v) { return out << v.x << " " << v.y; }
  28. ostream& operator<<(ostream& out, const Vector& v) { return out << v.x << " " << v.y; }
  29. bool operator<(const Point& p1, const Point& p2) { return p1.x < p2.x || (p1.x == p2.x && p1.y < p2.y); }
  30. namespace std {
  31.   template <> struct hash<Point> {
  32.     size_t operator()(const Point& p) const { return hash<ll>()(p.x) ^ hash<ll>()(p.y); }
  33.   };
  34. }
  35. Point Center(const Point& p1, const Point& p2) { return {(p1.x + p2.x) / 2., (p1.y + p2.y) / 2.}; }
  36. Point Intersection(const Line& l1, const Line& l2) {
  37.   ld d = l1.a * l2.b - l1.b * l2.a;
  38.   if (fabsl(d) < eps) return {INFINITY, INFINITY};
  39.   return {(l2.c * l1.b - l1.c * l2.b) / d, (l1.c * l2.a - l2.c * l1.a) / d};
  40. }
  41.  
Advertisement
Add Comment
Please, Sign In to add comment