xyz的計算圓與多邊形相交面積模板

#include<bits/stdc++.h>
 
using namespace std;
 
const double eps = 1e-8;
const double PI = acos(-1.0);
 
int dcmp(double x){//判斷誤差在eps範圍內 
    if( x > eps ) return 1;
    return x < -eps ? -1 : 0;
}
 
struct Point{
    double x,y;
    Point(){
        x = y = 0;
    }
    Point(double a,double b){//給點賦值 
        x = a;y = b;
    }
    inline void input(){
        scanf("%lf%lf",&x,&y);//輸入點的座標 
    }
    inline Point operator-(const Point &b)const{//求點a、b構成的向量 
        return Point(x - b.x,y - b.y);
    }
    inline Point operator+(const Point &b)const{
        return Point(x + b.x,y + b.y);
    }
    inline Point operator*(const double &b)const{
        return Point(x * b,y * b);
    }
    inline double dot(const Point &b)const{//點乘 
        return x * b.x + y * b.y;
    }
    inline double cross(const Point &b,const Point &c)const{//多邊形一邊上的兩點與圓心形成的向量叉乘 
        return (b.x - x) * (c.y - y) - (c.x - x) * (b.y - y);
    }
    inline double Dis(const Point &b)const{//求圓心與點之間的距離 
        return sqrt((*this-b).dot(*this-b));
    }
    inline bool InLine(const Point &b,const Point &c)const{ //三點共線 
        return !dcmp(cross(b,c));
    }
    inline bool OnSeg(const Point &b,const Point &c)const{ //點在線段上,包括端點 
        return InLine(b,c) && (*this - c).dot(*this - b) < eps;
    }
};
 
inline double min(double a,double b){
    return a < b ? a : b;
}
inline double max(double a,double b){
    return a > b ? a : b;
}
inline double Sqr(double x){
    return x * x;
}
inline double Sqr(const Point &p){
    return p.dot(p);
}
 
Point LineCross(const Point &a,const Point &b,const Point &c,const Point &d){
    double u = a.cross(b,c) , v = b.cross(a,d);
    return Point((c.x * v + d.x * u) / (u + v) , (c.y * v + d.y * u) / (u + v));

 
double LineCrossCircle(const Point &a,const Point &b,const Point &r,
    double R,Point &p1,Point & p2){
    Point fp = LineCross(r , Point(r.x+a.y-b.y , r.y+b.x-a.x) , a , b);
    double rtol = r.Dis(fp);
    double rtos = fp.OnSeg(a , b) ? rtol : min(r.Dis(a) , r.Dis(b));
    double atob = a.Dis(b);
    double fptoe = sqrt(R * R - rtol * rtol) / atob;
    if( rtos > R - eps ) return rtos;
    p1 = fp + (a - b) * fptoe;
    p2 = fp + (b - a) * fptoe;
    return rtos;
}
 
double SectorArea(const Point &r,const Point &a,const Point &b,double R){ //不大於180度扇形面積,r->a->b逆時針 
    double A2 = Sqr(r - a) , B2 = Sqr(r - b) , C2 = Sqr(a - b);
    return R * R * acos( (A2 + B2 - C2) * 0.5 / sqrt(A2) / sqrt(B2)) * 0.5;
}
 
double TACIA(const Point &r,const Point &a,const Point &b,double R){
    double adis = r.Dis(a) , bdis = r.Dis(b);
    if( adis < R + eps && bdis < R + eps )//兩點,也就是該邊在圓內 
        return r.cross(a , b) * 0.5;
    Point ta , tb;
    if( r.InLine(a,b) ) return 0.0;//三點共線,則 
    double rtos = LineCrossCircle(a, b, r, R, ta, tb);
    if( rtos > R - eps ) 
        return SectorArea(r, a, b, R);
    if( adis < R + eps )
        return r.cross(a, tb) * 0.5 + SectorArea(r, tb, b, R);
    if( bdis < R + eps )
        return r.cross(ta, b) * 0.5 + SectorArea(r, a, ta, R);
    return r.cross(ta, tb) * 0.5 + SectorArea(r, tb, b, R) + SectorArea(r, a, ta, R);
}
 
const int MAXN  = 505;
Point p[MAXN];
 
double SPICA(int n,Point r,double R){
    int i;
    double ret = 0 , if_clock_t;
    for( i = 0 ; i < n ; ++i ){
        if_clock_t = dcmp(r.cross(p[i], p[(i + 1) % n]));//如果if_clock_t<0說明兩個向量叉乘小於0,兩個向量所成夾角爲鈍角,
        //這兩個向量與p[i]、p[i+1]的邊形成的三角形在圓和多邊形外。 
        if( if_clock_t < 0 )
            ret -= TACIA(r, p[(i + 1) % n], p[i], R);
        else ret += TACIA(r, p[i], p[(i + 1) % n], R);
    }
    return fabs(ret);
}
 
int main(){
    int n;
    double x0,y0,ang,v0,g,t,r,vx,vy;
    while(scanf("%lf %lf %lf %lf %lf %lf %lf",&x0,&y0,&v0,&ang,&t,&g,&r)!=EOF)
    {
     if(x0==0&&y0==0&&v0==0&ang==0&&t==0&&g==0&&r==0)
     break;
    scanf("%d",&n);//多邊形n個頂點 
    for(int i = 0 ; i < n ; ++i ) //頂點座標 
        p[i].input();
        vx = v0*cos(ang/180*PI);
        vy = v0*sin(ang/180*PI);
    Point circle;
    //circle.input(); //圓心座標 
    circle.x = x0+vx*t;
    circle.y = y0+vy*t-g*t*t/2;
    printf("%.2f\n",SPICA(n,circle,r));
   }
    return 0;
}
 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章