Matrix_code

geo - poligon cut

Nov 9th, 2017 (edited)
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.48 KB | None | 0 0
  1. typedef vector<point> Polygon;
  2. struct Line {
  3.     point A,B;
  4. };
  5. // Cut a polygon with a line. Returns one half.
  6. // To return the other half, reverse the direction of Line l (by negating l.a, l.b)
  7. // The line must be formed using 2 points
  8. int ccw(point a,point b,point c)
  9. {
  10.     return dcmp(cross(b-a,c-a));
  11. }
  12. Polygon polygon_cut(const Polygon& P, Line l) {
  13.     Polygon Q;
  14.     for(int i = 0; i < P.size(); ++i) {
  15.         point A = P[i], B = (i == P.size()-1) ? P[0] : P[i+1];
  16.         if (ccw(l.A, l.B, A) != -1) Q.push_back(A);
  17.         if (ccw(l.A, l.B, A)*ccw(l.A, l.B, B) < 0) {
  18.             point p = LLIN(A,B,l.A, l.B); //areIntersect(Line(A, B), l, p);
  19.             Q.push_back(p);
  20.         }
  21.     }
  22.     return Q;
  23. }
  24. point Reflect(Line l, point p)
  25. {
  26.     point q=  PPL(l.A, l.B, p);
  27.     return p + (q - p) * 2;
  28. }
  29.  
  30. point MakeCircle(point a,point b, point c)
  31. {
  32.     if(dcmp(cross(b-a,c-a)) == 0) {
  33.         point p[3];
  34.         p[0] = a, p[1] = b, p[2] = c;
  35.         sort(p ,p + 3);
  36.         return (p[0] + p[2])/2;
  37.     }
  38.     return CCC(a,b,c);
  39. }
  40. void SmallEnclosingCircle( Polygon p, point &cen, double &r)
  41. {
  42.     random_shuffle(p.begin(), p.end());
  43.     cen = p[0], r = 0;
  44.     int n = p.size();
  45.     for(int i= 1;i < n;i ++){
  46.         if(dist(cen,p[i]) <= r + EPS) continue;
  47.         cen = p[i], r = 0;
  48.         for(int j = 0; j < i; j ++) {
  49.             if(dist(cen, p[j]) <= r + EPS) continue;
  50.             cen = (p[i] + p[j])/2;
  51.             r = dist(cen, p[j]);
  52.             for(int k = 0; k < j ;k ++) {
  53.                 if(dist(cen, p[k]) <= r + EPS) continue;
  54.                 cen = MakeCircle(p[i],p[j],p[k]);
  55.                 r = dist(cen, p[k]);
  56.             }
  57.         }
  58.     }
  59. }
  60.  
  61. struct Circle{
  62.     point p;
  63.     double r;
  64. };
  65. // helper functions for commonCircleArea
  66. double cir_area_solve(double a, double b, double c) {
  67.     return acos((a*a + b*b - c*c) / 2 / a / b);
  68. }
  69. double cir_area_cut(double a, double r) {
  70.     double s1 = a * r * r / 2;
  71.     double s2 = sin(a) * r * r / 2;
  72.     return s1 - s2;
  73. }
  74. // Tested: http://codeforces.com/contest/600/problem/D
  75. double commonCircleArea(Circle c1, Circle c2) { //return the common area of two circle
  76.     if (c1.r < c2.r) swap(c1, c2);
  77.     double d = mag(c1.p - c2.p);
  78.     if (d + c2.r <= c1.r + EPS) return c2.r*c2.r*PI;
  79.     if (d >= c1.r + c2.r - EPS) return 0.0;
  80.     double a1 = cir_area_solve(d, c1.r, c2.r);
  81.     double a2 = cir_area_solve(d, c2.r, c1.r);
  82.     return cir_area_cut(a1*2, c1.r) + cir_area_cut(a2*2, c2.r);
  83. }
Add Comment
Please, Sign In to add comment