Advertisement
kolychestiy

geometry pattern

Jul 4th, 2023
684
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.99 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. const double eps = 1e-7;
  5. double pi = acos(-1);
  6.  
  7. struct Point{
  8.     double x = 0, y = 0;
  9.     double angle;
  10.     double len_2;
  11.     double len;
  12.     Point(double x, double y) : x(x), y(y) {
  13.         angle = atan2(y, x);
  14.         len_2 = x*x + y*y;
  15.         len = sqrt(len_2);
  16.     }
  17.     Point() {}
  18.    
  19.     Point rationing() const {
  20.         return *this / len;
  21.     }
  22.  
  23.     Point operator / (double k) const {
  24.         return Point(x / k, y / k);
  25.     }
  26.  
  27.     Point rotate(double da){
  28.         da += angle;
  29.         return Point(len * cos(da), len * sin(da));
  30.     }
  31. };
  32.  
  33. bool operator < (const Point& p, const Point& q){
  34.     return p.x < q.x - eps || p.x < q.x + eps && p.y < q.y;
  35. }
  36.  
  37. Point operator * (double k, const Point& p){
  38.     return Point(p.x * k, p.y * k);
  39. }
  40.  
  41. Point operator - (const Point& p, const Point& q){
  42.     return Point(p.x - q.x, p.y - q.y);
  43. }
  44.  
  45. Point operator + (const Point& p, const Point& q){
  46.     return Point(p.x + q.x, p.y + q.y);
  47. }
  48.  
  49. istream& operator >> (istream& in, Point& p){
  50.     double x, y;
  51.     cin >> x >> y;
  52.     p = Point(x, y);
  53.     return in;
  54. }
  55.  
  56. ostream& operator << (ostream& out, const Point& p){
  57.     out << p.x << ' ' << p.y;
  58.     return out;
  59. }
  60.  
  61. double angle_between(const Point& p, const Point& q){
  62.     double a = abs(p.angle - q.angle);
  63.     return min(a, 2*pi - a);
  64. }
  65.  
  66. double vect(const Point& p, const Point& q){
  67.     return p.x*q.y - p.y*q.x;
  68. }
  69.  
  70. double scal(const Point& p, const Point& q){
  71.     return p.x*q.x + p.y*q.y;
  72. }
  73.  
  74. double dst_2(const Point& p, const Point& q){
  75.     double x = p.x - q.x;
  76.     double y = p.y - q.y;
  77.     return x*x + y*y;
  78. }
  79.  
  80. double area2(vector<Point>& a){
  81.     Point p;
  82.     Point last = a.back();
  83.     double s = 0;
  84.     for (auto cur : a){
  85.         s += vect(cur - p, last - p);
  86.         last = cur;
  87.     }
  88.     return abs(s);
  89. }
  90.  
  91. struct Line{
  92.     double a, b, c;
  93.     Point v;
  94.     Line (double A, double B, double C) : a(A), b(B), c(C) {
  95.         double len = Point(a, b).len;
  96.         a /= len; b /= len; c /= len;
  97.  
  98.         if (a < 0 || a == 0 && b < 0){
  99.             a *= -1; b *= -1; c *= -1;
  100.         }
  101.  
  102.         v = Point(-b, a);
  103.     }
  104.  
  105.     Line (const Point& p, const Point& q){
  106.         a = p.y - q.y;
  107.         b = q.x - p.x;
  108.         c = -(p.x*a + p.y*b);
  109.         *this = Line(a, b, c);
  110.     }
  111.  
  112.     Line (){}
  113.  
  114.     double distance(const Point& p) const {
  115.         return abs(a*p.x + b*p.y + c);
  116.     }
  117.  
  118.     double vect(const Point& p) const {
  119.         return a*p.x + b*p.y + c;
  120.     }
  121. };
  122.  
  123. istream& operator >> (istream& in, Line& l){
  124.     double a, b, c;
  125.     cin >> a >> b >> c;
  126.     l = Line(a, b, c);
  127.     return in;
  128. }
  129.  
  130. ostream& operator << (ostream& out, const Line& l){
  131.     out << l.a << ' ' << l.b << ' ' << l.c << endl;
  132.     return out;
  133. }
  134.  
  135. pair<int, Point> intersect(const Line& l, const Line& m){
  136.     double z = l.a * m.b - l.b * m.a;
  137.     if (z == 0){
  138.         if (l.c == m.c){
  139.             return {1, Point()};
  140.         }
  141.         return {-1, Point()};
  142.     }
  143.  
  144.     double x = -(l.c * m.b - l.b * m.c) / z;
  145.     double y = -(l.a * m.c - l.c * m.a) / z;
  146.     return {0, Point(x, y)};
  147. }
  148.  
  149. struct Beam {
  150.     Line l;
  151.     Point v;
  152.     Point b;
  153.     Beam (const Point& p, const Point& q) : b(p), v(q), l(p, q){}
  154.     Beam (){}
  155.  
  156.     double distance(const Point& p) const {
  157.         if (scal(v - b, p - b) < 0){
  158.             return sqrt(dst_2(b, p));
  159.         }
  160.         return l.distance(p);
  161.     }
  162.  
  163.     bool in_sector(const Point& p) const {
  164.         return scal(v - b, p - b) > -eps;
  165.     }
  166. };
  167.  
  168. double distance(const Beam& a, const Beam& b){
  169.     const auto& res = intersect(a.l, b.l);
  170.     if (res.first == 0){
  171.         const Point& p = res.second;
  172.         // cout << p << endl;
  173.         if (a.in_sector(p) && b.in_sector(p)){
  174.             return 0;
  175.         }
  176.     }
  177.  
  178.     return min(a.distance(b.b), b.distance(a.b));
  179. }
  180.  
  181. struct Segment {
  182.     Point p, q;
  183.     Beam b1, b2;
  184.     Line l;
  185.     Segment (Point p, Point q) : p(min(p, q)), q(max(p, q)), b1(p, q), b2(q, p), l(p, q) {}
  186.     Segment () {}
  187.    
  188.     double distance(const Point& p) const {
  189.         return max(b1.distance(p), b2.distance(p));
  190.     }
  191.  
  192.     bool in_sector(const Point& p) const {
  193.         return b1.in_sector(p) && b2.in_sector(p);
  194.     }
  195. };
  196.  
  197. istream& operator >> (istream& in, Segment& s){
  198.     Point p, q;
  199.     cin >> p >> q;
  200.     s = Segment(p, q);
  201.     return in;
  202. }
  203.  
  204. double distance(const Segment& s, const Segment& t){
  205.     const auto& res = intersect(s.l, t.l);
  206.     if (res.first == 0){
  207.         const Point& p = res.second;
  208.         if (s.in_sector(p) && t.in_sector(p)){
  209.             return 0;
  210.         }
  211.     }
  212.  
  213.     return min(
  214.         min(s.distance(t.p), s.distance(t.q)),
  215.         min(t.distance(s.p), t.distance(s.q))
  216.     );
  217. }
  218.  
  219. void solve(){
  220.  
  221.  
  222.  
  223. }
  224.  
  225. main(){
  226.  
  227.     ios_base::sync_with_stdio(0);
  228.     cin.tie(0);
  229.     cout.tie(0);
  230.  
  231.     cout << fixed << setprecision(10);
  232.  
  233.     freopen("raydist.in", "r", stdin);
  234.     freopen("raydist.out", "w", stdout);
  235.  
  236.     solve();
  237.  
  238. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement