题意:平面上依次放置n个圆,后放的覆盖先放的,按顺序给出每个圆的半径和圆心坐标,问最后图形的可见圆弧长之和。
题解:因为是后放的覆盖先放的,所以逆序枚举,每个圆只考虑之前放过的圆和自己的交点,把所有交点排序后可以得到每两个相邻的交点之间的圆弧,找到圆弧中点,如果这个点在之前放过的圆内,说明这个圆弧不能要,否则加到答案里。注意弧度要规范到0~2π。
#include <cstdio>
#include <cstring>
#include <cmath>
#include <vector>
#include <algorithm>
using namespace std;
const double PI = acos(-1);
const double eps = 1e-11;
struct Point {
double x, y;
Point(double x = 0, double y = 0): x(x), y(y) {}
};
typedef Point Vector;
struct Circle {
Point c;
double r;
Circle() {}
Circle(Point c, double r = 0): c(c), r(r) {}
Point point(double a) {
return Point(c.x + cos(a) * r, c.y + sin(a) * c.y);
}
};
double dcmp(double x) {
if (fabs(x) < eps)
return 0;
return x < 0 ? -1 : 1;
}
Vector operator + (const Point& A, const Point& B) {
return Vector(A.x + B.x, A.y + B.y);
}
Vector operator - (const Point& A, const Point& B) {
return Vector(A.x - B.x, A.y - B.y);
}
Vector operator * (const Point& A, double a) {
return Vector(A.x * a, A.y * a);
}
Vector operator / (const Point& A, double a) {
return Vector(A.x / a, A.y / a);
}
double Cross(const Vector& A, const Vector& B) {
return A.x * B.y - A.y * B.x;
}
double Dot(const Vector& A, const Vector& B) {
return A.x * B.x + A.y * B.y;
}
double Length(const Vector& A) {
return sqrt(Dot(A, A));
}
double Angle(Vector A, Vector B) {
return acos(Dot(A, B) / Length(A) / Length(B));
}
bool operator < (const Point& A, const Point& B) {
return A.x < B.x || (A.x == B.x && A.y < B.y);
}
bool operator == (const Point& A, const Point& B) {
return A.x == B.x && A.y == B.y;
}
Point Rotate(Point A, double rad) {
return Point(A.x * cos(rad) - A.y * sin(rad), A.x * sin(rad) + A.y * cos(rad));
}
double torad(double deg) {
return deg / 180.0 * PI;
}
double normal(double rad) {
return rad - 2.0 * M_PI * floor(rad / 2.0 / M_PI);
}
void GetCircleIntersection(Circle a, Circle b, vector<double>& v) {
if (dcmp(Length(a.c - b.c)) == 0)
return;
if (dcmp(a.r + b.r - Length(a.c - b.c)) < 0)
return;
if (dcmp(fabs(a.r - b.r) - Length(a.c - b.c)) > 0)
return;
double rad = atan2(b.c.y - a.c.y, b.c.x - a.c.x);
double rad1 = acos((a.r * a.r + Length(a.c - b.c) * Length(a.c - b.c) - b.r * b.r) / (2.0 * a.r * Length(a.c - b.c)));
v.push_back(normal(rad - rad1));
v.push_back(normal(rad + rad1));
}
const int N = 105;
int n;
Circle cir[N];
bool judge(int cur, Point p) {
for (int i = n - 1; i > cur; i--)
if (dcmp(Length(cir[i].c - p) - cir[i].r) <= 0)
return false;
return true;
}
int main() {
int t;
scanf("%d", &t);
while (t--) {
scanf("%d", &n);
for (int i = 0; i < n; i++)
scanf("%lf%lf%lf", &cir[i].r, &cir[i].c.x, &cir[i].c.y);
double res = 0;
for (int i = n - 1; i >= 0; i--) {
vector<double> v;
v.push_back(0);
v.push_back(PI * 2);
for (int j = n - 1; j > i; j--)
GetCircleIntersection(cir[i], cir[j], v);
sort(v.begin(), v.end());
double temp = 0;
for (int j = 0; j < v.size() - 1; j++) {
double mid = (v[j] + v[j + 1]) / 2.0;
if (judge(i, Point(cir[i].c.x + cos(mid) * cir[i].r, cir[i].c.y + sin(mid) * cir[i].r)))
temp += v[j + 1] - v[j];
}
res += temp * cir[i].r;
}
printf("%.3lf\n", res);
}
return 0;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
时间: 2024-10-05 08:42:52