P7651 多边形与圆的面积交

· · 题解

参考了该题解的方式,但是提供一种通过向量计算圆与三角形面积交的方法。

先给出最简单的求出正方形四个顶点的方式:将 A,D 分别绕对角线中点旋转 \frac{\pi}{2} 即可。

为了方便处理,将圆心移到坐标系原点。

以下图这种情况为例:

只要求出 \overrightarrow{OD} 即可,这样就将问题转化为 \overrightarrow{OD} 与 \overrightarrow{OB} 的夹角(扇形面积)以及 \overrightarrow{OA} 与 \overrightarrow{OD} 围成的三角形面积。

都是容易处理的,注意面积的正负。

其中 $\overrightarrow{AM}$ 是 $\overrightarrow{AO}$ 在 $\overrightarrow{AB}$ 上的投影向量;$\overrightarrow{MD}$ 的模长容易算(在直角三角形 $OMD$ 中),方向就是 $\overrightarrow{AB}$ 向量的方向,于是就都能简单地算出来。 这种方法在钝角情况同样适用,原因是向量投影下来是有方向的。 ![](https://s21.ax1x.com/2025/03/21/pE0MeOS.png) 其他情况都类似,计算夹角使用 asin,并要注意特判钝角。 [AC record](https://www.luogu.com.cn/record/209150736) ```cpp namespace Geometry { const double eps = 1e-9, pi = acos(-1), inf = 2e18; struct Vector { double x, y; Vector() {x = y = 0;} Vector(const double &x, const double &y) : x(x), y(y) {} Vector(const Vector &t) : x(t.x), y(t.y) {} friend Vector operator - (const Vector &x, const Vector &y) { return Vector(x.x - y.x, x.y - y.y); } Vector &operator -= (const Vector &t) { x -= t.x, y -= t.y; return *this; } friend Vector operator + (const Vector &x, const Vector &y) { return Vector(x.x + y.x, x.y + y.y); } Vector &operator += (const Vector &t) { x += t.x; y += t.y; return *this; } friend Vector operator / (const Vector &x, const double &y) { return Vector(x.x / y, x.y / y); } Vector &operator /= (const double t) { x /= t; y /= t; return *this; } friend Vector operator * (const Vector &x, const double &y) { return Vector(x.x * y, x.y * y); } friend Vector operator * (const double &y, const Vector &x) { return Vector(x.x * y, x.y * y); } Vector &operator *= (const double t) { x *= t; y *= t; return *this; } friend double operator * (Vector a, Vector b) { return a.x * b.x + a.y * b.y; } // dot Product friend double operator ^ (Vector a, Vector b) { return a.x * b.y - a.y * b.x; } // cross Product friend bool operator == (Vector a, Vector b) { return abs(a.x - b.x) < eps && abs(a.y - b.y) < eps; } friend bool operator < (Vector a, Vector b) { return abs(a.x - b.x) >= eps ? a.x < b.x : b.y - a.y >= eps; } il double mod() { return hypot(x, y); } il double mod2() { return x * x + y * y; } il void input() { scanf("%lf%lf", &x, &y); } il void print(char ch = '\n') { printf("%.3lf %.3lf%c", x, y, ch); } }; typedef Vector Point; il double dot(Vector a, Vector b) { return a * b; } il double cross(Vector a, Vector b) { return a ^ b; } il double cross(Vector a, Vector b, Vector c) { return (b - a) ^ (c - a);} // it's a line segment when seg = true struct Line { Point A, B; double angle; Line() { } Line(const Point &A, const Point &B) : A(A), B(B), angle(atan2((B - A).y, (B - A).x)) {} il bool left(const Point &x) const { return cross(B - A, x - A) > 0; } il bool right(const Point &x) const { return cross(B - A, x - A) < 0; } // friend bool operator == (Line a, Line b) {return a.A == b.A && a.B == b.B; } friend bool operator < (const Line &x, const Line &y) { if (abs(x.angle - y.angle) < eps) return y.left(x.A); return x.angle < y.angle; } il void print() { A.print(' '); ptc(' '); B.print('\n'); } }; Point intersection(const Line &x, const Line &y) { // ensure they aren't parallel Vector AB = y.A - x.A, v1 = x.B - x.A, v2 = y.B - y.A; return x.A + (((AB ^ v2) / (v1 ^ v2)) * v1); } il double dis(const Point &p, const Line &l) { Vector pA = l.A - p, pB = l.B - p, AB = l.B - l.A; return fabs(pA ^ pB) / AB.mod(); } il double entri(Point A, Point B) { return (A ^ B) / 2; } il double enfan(Point A, Point B) { if (A * B < 0) { if ((A ^ B) < 0) return -pi - asin((A ^ B) / A.mod() / B.mod()); return pi + asin((A ^ B) / A.mod() / B.mod()); } return asin((A ^ B) / A.mod() / B.mod()); } il double triangle(double R, Point A, Point B) { auto AB = Line(A, B); const double dA = A.mod(), dB = B.mod(), S = R * R / 2, ds = dis({0, 0}, AB); bool flg1 = dA - R > eps, flg2 = dB - R > eps; Vector ba = A - B, _B = -1 * B; Vector ab = B - A, _A = -1 * A; if (dB < eps || dA < eps) return 0; if (!flg1 && !flg2) return entri(A, B); else if (!flg1) { Vector x1 = ab / ab.mod() * (_A * ab / ab.mod()); double flen = sqrt(R * R - ds * ds); x1 += ab / ab.mod() * flen; Point D = A + x1; return entri(A, D) + enfan(D, B) * S; } else if (!flg2) { Vector x1 = ba / ba.mod() * (_B * ba / ba.mod()); double flen = sqrt(R * R - ds * ds); x1 += ba / ba.mod() * flen; Point D = B + x1; return enfan(A, D) * S + entri(D, B); } else { if (ds - R > eps || dot(ab, -1 * A) < -eps || dot(ba, -1 * B) < -eps) return enfan(A, B) * S; else { Vector x1 = ba / ba.mod() * (_B * ba / ba.mod()); double flen = sqrt(B.mod2() - ds * ds) + sqrt(R * R - ds * ds); x1 = x1 / x1.mod() * flen; Point D1 = B + x1; Vector x2 = ab / ab.mod() * (_A * ab / ab.mod()); double flen2 = sqrt(A.mod2() - ds * ds) + sqrt(R * R - ds * ds); x2 = x2 / x2.mod() * flen2; Point D2 = A + x2; return enfan(A, D1) * S + entri(D1, D2) + enfan(D2, B) * S; } } } } using namespace Geometry; double x[4], y[4]; double x5, y5, r, opt; int main() { cin >> x[0] >> y[0] >> x[2] >> y[2] >> x5 >> y5 >> r; x[0] -= x5; y[0] -= y5; x[2] -= x5; y[2] -= y5; x5 = y5 = 0; double mx = (x[0] + x[2]) / 2, my = (y[0] + y[2]) / 2; x[1] = (x[0] - mx) * cos(pi / 2) - (y[0] - my) * sin(pi / 2) + mx, y[1] = (y[0] - my) * cos(pi / 2) + (x[0] - mx) * sin(pi / 2) + my; x[3] = (x[2] - mx) * cos(pi / 2) - (y[2] - my) * sin(pi / 2) + mx, y[3] = (y[2] - my) * cos(pi / 2) + (x[2] - mx) * sin(pi / 2) + my; rep1(i, 0, 3) opt += triangle(r, Point(x[i], y[i]), Point(x[(i + 1) & 3], y[(i + 1) & 3])); printf("%.2lf", abs(opt)); return 0; } ```