HDU 5120 Intersection (计算几何)
2015-10-11 22:52
393 查看
题意:
求两个一模一样,但位置不一定相同的圆环的环相交面积
分析:
其实就是圆交面积++−−
ans=大圆交−2∗一大一小圆交+小圆交
写圆交函数的时候,注意有三种情况,相含,相交,相离
把重合当成相含了, 一直没考虑相含卡到死。。。
代码:
求两个一模一样,但位置不一定相同的圆环的环相交面积
分析:
其实就是圆交面积++−−
ans=大圆交−2∗一大一小圆交+小圆交
写圆交函数的时候,注意有三种情况,相含,相交,相离
把重合当成相含了, 一直没考虑相含卡到死。。。
代码:
// // Created by TaoSama on 2015-10-11 // Copyright (c) 2015 TaoSama. All rights reserved. // //#pragma comment(linker, "/STACK:1024000000,1024000000") #include <algorithm> #include <cctype> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <string> #include <set> #include <vector> using namespace std; #define pr(x) cout << #x << " = " << x << " " #define prln(x) cout << #x << " = " << x << endl const int N = 1e5 + 10, INF = 0x3f3f3f3f, MOD = 1e9 + 7; const double PI = acos(-1), EPS = 1e-10; int sgn(double a) { return a < -EPS ? -1 : a < EPS ? 0 : 1; } struct Point { double x, y; }; double getDis(const Point& p1, const Point& p2) { return sqrt((p1.x - p2.x) * (p1.x - p2.x) + (p1.y - p2.y) * (p1.y - p2.y)); } double getAngle(double a, double b, double c) { return acos((a * a + b * b - c * c) / 2 / a / b); } struct Circle { double r; Point p; Circle() {} Circle(double r, double x, double y) { this->r = r; p = (Point) {x, y}; } }; double getIntersectionArea(const Circle& c1, const Circle& c2) { double dis = getDis(c1.p, c2.p); if(sgn(dis - (c1.r + c2.r)) > 0) return 0; if(sgn(dis - abs(c1.r - c2.r)) <= 0) { double minr = min(c1.r, c2.r); return PI * minr * minr; } double alpha = getAngle(c1.r, dis, c2.r) * 2; double beta = getAngle(c2.r, dis, c1.r) * 2; double area = 0.5 * alpha * c1.r * c1.r + 0.5 * beta * c2.r * c2.r; area -= 0.5 * c1.r * c1.r * sin(alpha) + 0.5 * c2.r * c2.r * sin(beta); return area; } int main() { #ifdef LOCAL freopen("C:\\Users\\TaoSama\\Desktop\\in.txt", "r", stdin); // freopen("C:\\Users\\TaoSama\\Desktop\\out.txt","w",stdout); #endif ios_base::sync_with_stdio(0); int t; scanf("%d", &t); int kase = 0; while(t--) { double r, R; double x1, y1, x2, y2; scanf("%lf%lf%lf%lf%lf%lf", &r, &R, &x1, &y1, &x2, &y2); Circle a1(R, x1, y1), a2(R, x2, y2); Circle b1(r, x1, y1), b2(r, x2, y2); double ans = getIntersectionArea(a1, a2) - getIntersectionArea(a1, b2) - getIntersectionArea(a2, b1) + getIntersectionArea(b1, b2); printf("Case #%d: %.6f\n", ++kase, ans); } return 0; }
相关文章推荐
- 【Google Code Jam 2009 round2 problem D】Watering Plants (两圆交点求法详解)
- 计算几何模板
- 计算几何小模板
- BZOJ2829信用卡凸包
- HDU 4922 Hello, Your Package! (计算几何+DP)(WA)
- poj 1514&zoj 1185 Metal Cutting(半平面交)
- UVA 10969 Sweet Dream(圆的相交)
- uva 11177 Fighting Against a Polygonal Monster(凸包与圆的面积交)
- POJ1279 && LA2512 Art Gallery(求多边形的核)
- poj 2540 && uva 10084 Hotter Colder(半平面交)
- 【计算几何】POJ 2318 & POJ 2398
- 【计算几何】POJ 2653
- 【计算几何】POJ 1113
- HDU 5128 The E-pang Palace
- POJ 2318 TOYS(叉积+二分or暴力)
- POJ 2398 Toy Storage(叉积+二分)
- POJ 1228 Grandpa's Estate 计算凸包+判断点在线段上
- POJ 1873 The Fortified Forest 计算凸包
- POJ 2007 Scrambled Polygon 极角排序
- POJ 2074 Line of Sight 直线相交+线段覆盖