计算几何的一些板

两圆面积交:

 1 #include <cmath>
 2 #include <cstdio>
 3 #include <cstdlib>
 4 #include <iostream>
 5 #define eps 1e-8
 6 #define db double
 7 #define Pi 3.1415926535
 8 using namespace std;
 9 
10 int dcmp(db x){
11     if (fabs(x) < eps) return 0;
12     return x < 0 ? -1 : 1;
13 }
14 
15 struct Point{
16     db x, y;
17 };
18 struct Circle{
19     db r;
20     Point o;
21     void read() { scanf("%lf%lf%lf", &o.x, &o.y, &r); }
22 };
23 
24 db sqr(db x) {
25     return x*x;
26 }
27 
28 db dis(Point a, Point b){
29     return sqrt(sqr(a.x-b.x) + sqr(a.y-b.y));
30 }
31 
32 db get_Int(Circle c1, Circle c2){
33     if (c1.r < c2.r) swap(c1, c2);
34     db d = dis(c1.o, c2.o);
35     if (dcmp(d-(c1.r+c2.r)) >= 0) return 0;
36     if (dcmp(d-(c1.r-c2.r)) <= 0) return Pi*sqr(c2.r);
37     db ang1 = acos((sqr(d)+sqr(c1.r)-sqr(c2.r)) / (2*d*c1.r));
38     db ang2 = acos((sqr(d)+sqr(c2.r)-sqr(c1.r)) / (2*d*c2.r));
39     db ret = ang1*sqr(c1.r) + ang2*sqr(c2.r) - sin(ang1)*d*c1.r;
40     return ret;
41 }
42 
43 int main(){
44     freopen("cv3273.in", "r", stdin);
45     freopen("cv3273.out", "w", stdout);
46     Circle c1, c2;
47     c1.read(); c2.read();
48     printf("%.3lf\n", get_Int(c1, c2));
49     return 0;
50 }
View Code

 

posted @ 2016-03-04 10:13  Lukaluka  阅读(242)  评论(0编辑  收藏  举报