Round and rectangular area intersection
or triangulation to do it.
Code:
#include <stdio.h> #include <string.h> #include <stdlib.h> #include <math.h> #include < Algorithm>using namespace Std;const Double eps = 1e-8;const double pi = ACOs ( -1.0); int dcmp (double x) {if (x > EPs ) return 1; return x <-eps? -1:0;} struct point{double x, y; Point () {x = y = 0;} Point (Double A, double b) {x = a, y = b;} inline void Read () {scanf ("%lf%lf", &x, &y);} Inline point operator-(const-point-&b) const {return point (x-b.x, y-b.y);} Inline point operator+ (const-point-&b) const {return point (x + b.x, y + b.y);} Inline point operator* (const double &b) const {return point (x * b, y * b);} inline double dot (const point &b) Const {return x * b.x + y * B.Y;} Inline Double Cross (const point &b, const point &c) const {return (b.x-x) * (c.y-y)-(c.x-x) * (B.Y-Y); } inline double Dis (const point &b) const {return sqrt ((*this-b). dot (*this-b));} Inline bOol InLine (const point &b, const point &c) const//three points collinear {return!dcmp (cross (b, C));} The inline bool onseg (const point &b, const dot &c) const//points on the segment, including the endpoint {return InLine (b, c) && (*THIS-C). Dot (*this-b) < EPS;}}; Inline double min (double A, double b) {return a < b a:b;} Inline double max (double A, double b) {return a > b a:b;} Inline double Sqr (double x) {return x * x;} Inline double Sqr (const point &p) {return P.dot (p);} Point Linecross (const-point &a, const-point &b, const-point-&c, const-point-&d) {Double u = a.cross (b, c), V = B.cross (A, D); Return Point ((c.x * v + d.x * u)/(U + V), (C.Y * v + d.y * u)/(U + V));} Double linecrosscircle (const-point &a, const-point &b, const-point-&r, Double R, point &p1, Poi NT &P2) {point fp = Linecross (R, point (R.x + a.y-b.y, R.y + b.x-a.x), A, b); Double rtol = R.dis (FP); Double RTOs = fp. Onseg (A, b)? Rtol:min (R.dis (a), R.dis (b));Double Atob = A.dis (b); Double fptoe = sqrt (R * r-rtol * rtol)/Atob; if (RTOs > R-eps) return RTOs; P1 = fp + (A-B) * FPTOE; P2 = fp + (b-a) * FPTOE; return RTOs;} Double Sectorarea (const point &r, const point &a, const point &b, Double R)//not greater than 180 degrees sector area, R->a->b counterclockwise { Double A2 = SQR (r-a), B2 = SQR (r-b), C2 = SQR (A-B); Return R * R * ACOs ((A2 + b2-c2) * 0.5/SQRT (A2)/sqrt (B2)) * 0.5;} Double Tacia (const point &r, const point &a, const point &b, Double R)//triangleandcircleintersectarea, counterclockwise, R for Center {Double Adis = R.dis (a), Bdis = R.dis (b); if (Adis < R + EPS && Bdis < r + EPS) return R.cross (A, b) * 0.5; Point TA, TB; if (R.inline (A, B)) return 0.0; Double RTOs = Linecrosscircle (A, B, R, R, TA, TB); if (RTOs > R-eps) return Sectorarea (R, a, B, r); if (Adis < R + EPS) return R.cross (A, TB) * 0.5 + Sectorarea (R, TB, B, R); if (Bdis < R + eps) return R.cross (TA, b) * 0.5 + SectorareA (R, a, TA, R); Return R.cross (TA, TB) * 0.5 + Sectorarea (R, A, TA, R) + Sectorarea (R, TB, B, r);} const int N = 505; Point P[n], o;double SPICA (int N, point R, Double r)//simplepolygonintersectcirclearea{int i; Double res = 0, if_clock_t; for (i = 0; i < n; + + i) {if_clock_t = dcmp (R.cross (P[i], p[(i + 1)% n])); if (if_clock_t < 0) Res-= Tacia (R, p[(i + 1)% n], P[i], R); else res + = Tacia (R, P[i], p[(i + 1)% n], R); } return Fabs (res);} Double R;int Main () {int bo = 0; while (~SCANF ("%lf%lf%lf", &o.x, &o.y, &r)) {if (bo) printf ("\ n"); else Bo = 1; Double x1, y1, x2, y2; scanf ("%lf%lf%lf%lf", &x1, &y1, &x2, &y2); if (x1 > x2) Swap (x1, x2); if (y1 > y2) swap (y1, y2); P[0] = point (x1, y1); P[1] = point (x1, y2); P[2] = point (x2, y2); P[3] = point (x2, y1); printf ("%.10f\n", SPICA (4, O, R)); } return 0;}
ZOJ 2675 Little Mammoth (computational geometry)