hdu 5130 圆和多边形的面积


圆的一般式方程:

 

设A(x1,y1),B(x2,y2),P(x,y), 那么有

 

最后化成:

一般式里面的D,E,F自然就出来了,剩下套模版。


///                 .-~~~~~~~~~-._       _.-~~~~~~~~~-.
///             __.'              ~.   .~              `.__
///           .'//                  \./                  \\`.
///        .'//                     |                     \\`.
///       .'// .-~"""""""~~~~-._     |     _,-~~~~"""""""~-. \\`.
///     .'//.-"                 `-.  |  .-'                 "-.\\`.
///   .'//______.============-..   \ | /   ..-============.______\\`.
/// .'______________________________\|/______________________________`.
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <vector>
#include <iostream>
#include <string>
#include <map>
#include <stack>
#include <cstring>
#include <queue>
#include <list>
#include <stdio.h>
#include <set>
#include <algorithm>
#include <cstdlib>
#include <cmath>
#include <iomanip>
#include <cctype>
#include <sstream>
#include <functional>
#include <stdlib.h>
#include <time.h>
#include <bitset>
using namespace std;

#define pi acos(-1)
#define s_1(x) scanf("%d",&x)
#define s_2(x,y) scanf("%d%d",&x,&y)
#define s_3(x,y,z) scanf("%d%d%d",&x,&y,&z)
#define s_4(x,y,z,X) scanf("%d%d%d%d",&x,&y,&z,&X)
#define S_1(x) scan_d(x)
#define S_2(x,y) scan_d(x),scan_d(y)
#define S_3(x,y,z) scan_d(x),scan_d(y),scan_d(z)
#define PI acos(-1)
#define endl '\n'
#define srand() srand(time(0));
#define me(x,y) memset(x,y,sizeof(x));
#define foreach(it,a) for(__typeof((a).begin()) it=(a).begin();it!=(a).end();it++)
#define close() ios::sync_with_stdio(0); cin.tie(0);
#define FOR(x,n,i) for(int i=x;i<=n;i++)
#define FOr(x,n,i) for(int i=x;i<n;i++)
#define fOR(n,x,i) for(int i=n;i>=x;i--)
#define fOr(n,x,i) for(int i=n;i>x;i--)
#define W while
#define sgn(x) ((x) < 0 ? -1 : (x) > 0)
#define bug printf("***********\n");
#define db double
#define ll long long
#define mp make_pair
#define pb push_back
typedef long long LL;
typedef pair <int, int> ii;
const int INF=0x3f3f3f3f;
const LL LINF=0x3f3f3f3f3f3f3f3fLL;
const int dx[]={-1,0,1,0,1,-1,-1,1};
const int dy[]={0,1,0,-1,-1,1,-1,1};
const int maxn=1e5+10;
const int maxx=4e5+10;
const double EPS=1e-8;
const double eps=1e-8;
const int mod=1e9+7;
template<class T>inline T min(T a,T b,T c) { return min(min(a,b),c);}
template<class T>inline T max(T a,T b,T c) { return max(max(a,b),c);}
template<class T>inline T min(T a,T b,T c,T d) { return min(min(a,b),min(c,d));}
template<class T>inline T max(T a,T b,T c,T d) { return max(max(a,b),max(c,d));}
template <class T>
inline bool scan_d(T &ret){char c;int sgn;if (c = getchar(), c == EOF){return 0;}
while (c != '-' && (c < '0' || c > '9')){c = getchar();}sgn = (c == '-') ? -1 : 1;ret = (c == '-') ? 0 : (c - '0');
while (c = getchar(), c >= '0' && c <= '9'){ret = ret * 10 + (c - '0');}ret *= sgn;return 1;}

inline bool scan_lf(double &num){char in;double Dec=0.1;bool IsN=false,IsD=false;in=getchar();if(in==EOF) return false;
while(in!='-'&&in!='.'&&(in<'0'||in>'9'))in=getchar();if(in=='-'){IsN=true;num=0;}else if(in=='.'){IsD=true;num=0;}
else num=in-'0';if(!IsD){while(in=getchar(),in>='0'&&in<='9'){num*=10;num+=in-'0';}}
if(in!='.'){if(IsN) num=-num;return true;}else{while(in=getchar(),in>='0'&&in<='9'){num+=Dec*(in-'0');Dec*=0.1;}}
if(IsN) num=-num;return true;}

void Out(LL a){if(a < 0) { putchar('-'); a = -a; }if(a >= 10) Out(a / 10);putchar(a % 10 + '0');}
void print(LL a){ Out(a),puts("");}
//freopen( "in.txt" , "r" , stdin );
//freopen( "data.txt" , "w" , stdout );
//cerr << "run time is " << clock() << endl;

int dcmp(double x)
{
    if(fabs(x) < EPS) return 0;
    else return x < 0 ? -1 : 1;
}


struct Point
{
    double x, y;
    int id;
    //Point(const Point& rhs): x(rhs.x), y(rhs.y) { } //¿½±´¹¹Ô캯Êý
    Point(double x = 0, double y = 0) : x(x), y(y) { }
    inline void input()
    {
        scanf("%lf%lf",&x,&y);
    }
    inline void print()
    {
        printf("%.6lf %.6lf\n",x,y);
    }
    bool operator == (const Point& e) const
    {
        return dcmp(x - e.x) == 0 && dcmp(y - e.y) == 0;
    }
    Point operator + (Point q){ return Point(x+q.x,y+q.y);}
    Point operator - (Point q){ return Point(x-q.x,y-q.y);}
    Point operator * (double q){ return Point(x*q,y*q);}
    Point operator / (double q){ return Point(x/q,y/q);}
    Point &operator +=(Point q){ x+=q.x;y+=q.y; return *this;}
    Point &operator -=(Point q){ x-=q.x;y-=q.y; return *this;}
    double operator *(const Point& q) const{
        return x*q.x+y*q.y;
    }
    double operator ^(const Point& q) const{
        return x*q.y-y*q.x;
    }
    double len2()
    {
        return x*x+y*y;
    }
    double len()
    {
        return sqrt(x*x+y*y);
    }
    void read()
    {
        db xx,yy;
        scanf("%lf%lf",&xx,&yy);
        x=xx,y=yy;
    }

}Q[2010];

struct Circle
{
    Point c;
    double r;
    Circle() {}
    //Circle(const Circle& rhs): c(rhs.c), r(rhs.r) { }
    Circle(const Point& c, const double& r): c(c), r(r) { }

    inline void input()
    {
        scanf("%lf%lf%lf",&c.x,&c.y,&r);
    }
    inline void print()
    {
        printf("%.6lf %.6lf\n",c.x,c.y);
    }
    Point point(double ang) const { return Point(c.x + cos(ang)*r, c.y + sin(ang)*r); } //圆心角所对应的点
    double area(void) const { return PI * r * r; }
};
typedef Point Vector;

//向量点积
double Dot(Vector A, Vector B) { return A.x*B.x + A.y*B.y; }

//向量长度
double Length(Vector A) { return sqrt(Dot(A, A)); }

//向量叉积  |a||b|sin<a,b>
//叉积的结果也是一个向量,是垂直于向量a,b所形成的平面,如果看成三维坐标的话是在 z 轴上,上面结果是它的模。
double Cross(Vector A, Vector B) { return A.x*B.y - A.y*B.x; }



//圆和多边形相交面积
//for(int i=1;i<=n;i++)
//    ans += TriAngleCircleInsection(C, Q[i], Q[i+1]);

double TriAngleCircleInsection(Circle C, Point A, Point B)
{
    Vector OA = A-C.c, OB = B-C.c;
    Vector BA = A-B, BC = C.c-B;
    Vector AB = B-A, AC = C.c-A;
    double DOA = Length(OA), DOB = Length(OB),DAB = Length(AB), r = C.r;
    if(dcmp(Cross(OA,OB)) == 0) return 0;
    if(dcmp(DOA-C.r) < 0 && dcmp(DOB-C.r) < 0) return Cross(OA,OB)*0.5;
    else if(DOB < r && DOA >= r)
    {
        double x = (Dot(BA,BC) + sqrt(r*r*DAB*DAB-Cross(BA,BC)*Cross(BA,BC)))/DAB;
        double TS = Cross(OA,OB)*0.5;
        return asin(TS*(1-x/DAB)*2/r/DOA)*r*r*0.5+TS*x/DAB;
    }
    else if(DOB >= r && DOA < r)
    {
        double y = (Dot(AB,AC)+sqrt(r*r*DAB*DAB-Cross(AB,AC)*Cross(AB,AC)))/DAB;
        double TS = Cross(OA,OB)*0.5;
        return asin(TS*(1-y/DAB)*2/r/DOB)*r*r*0.5+TS*y/DAB;
    }
    else if(fabs(Cross(OA,OB)) >= r*DAB || Dot(AB,AC) <= 0 || Dot(BA,BC) <= 0)
    {
        if(Dot(OA,OB) < 0)
        {
            if(Cross(OA,OB) < 0) return (-acos(-1.0)-asin(Cross(OA,OB)/DOA/DOB))*r*r*0.5;
            else                 return ( acos(-1.0)-asin(Cross(OA,OB)/DOA/DOB))*r*r*0.5;
        }
        else                     return asin(Cross(OA,OB)/DOA/DOB)*r*r*0.5;
    }
    else
    {
        double x = (Dot(BA,BC)+sqrt(r*r*DAB*DAB-Cross(BA,BC)*Cross(BA,BC)))/DAB;
        double y = (Dot(AB,AC)+sqrt(r*r*DAB*DAB-Cross(AB,AC)*Cross(AB,AC)))/DAB;
        double TS = Cross(OA,OB)*0.5;
        return (asin(TS*(1-x/DAB)*2/r/DOA)+asin(TS*(1-y/DAB)*2/r/DOB))*r*r*0.5 + TS*((x+y)/DAB-1);
    }
}
int n;
db k;
Point A,B;
void solve()
{
    int cas=1;
    while(scanf("%d%lf",&n,&k)!=EOF)
    {
        FOR(1,n,i) Q[i].read();
        Q[n+1]=Q[1];
        A.read();B.read();
        double D = (2.0*k*k*A.x-2.0*B.x)/(1.0-k*k);
        double E = (2.0*k*k*A.y-2.0*B.y)/(1.0-k*k);
        double F = (B.x*B.x+B.y*B.y-k*k*(A.x*A.x+A.y*A.y))/(1.0-k*k);
        Circle C = Circle(Point(-D*0.5,-E*0.5),sqrt(D*D+E*E-4.0*F)*0.5);
        double ans = 0.0;
        for(int i=1;i<=n;i++)
            ans += TriAngleCircleInsection(C, Q[i], Q[i+1]);
        printf("Case %d: %.10f\n",cas++,fabs(ans));
    }
}
int main()
{
    //freopen( "in.txt" , "r" , stdin );
    //freopen( "data.txt" , "w" , stdout );
    int t=1;
    //init();
    //s_1(t);
    for(int cas=1;cas<=t;cas++)
    {
        //printf("Case #%d: ",cas);
        solve();
    }
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值