三角形的外接圆 - 计算几何

问题

给定三角形三点坐标(三点不会在同一直线)和一个点(x,y),要你判断点(x,y)
是否在三角形外接圆之外。
若在圆外,输出“Accepted”若在圆上或圆内,则输出“Rejected”;
Sample Input
3
-2 0 0 -2 2 0 2 -2
-2 0 0 -2 2 0 0 2
-2 0 0 -2 2 0 1 1
Sample Output
Accepted
Rejected
Rejected

模板

#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
typedef long long LL;
const double eps = 1e-8;

typedef struct Point
{//平面点
    long double x,y;
}TPoint;
typedef struct TCircle
{//圆
    long double r;
    TPoint centre;
}TCircle;
typedef struct TTriangle
{//三角形三点
    TPoint t[3];
}TTriangle;
long double triArea(const TTriangle t)
{//已知三个顶点坐标,求三角形面积
    return fabs(t.t[0].x * t.t[1].y
                + t.t[1].x * t.t[2].y
                + t.t[2].x * t.t[0].y
                - t.t[1].x * t.t[0].y
                - t.t[2].x * t.t[1].y
                -t.t[0].x*t.t[2].y)/2;
}
long double dis(const TPoint p1,const TPoint p2)
{//计算平面两点距离
    return sqrt((p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y));
}
TCircle f(const TTriangle t)
{
    TCircle tmp;//三角形外接圆
    long double a,b,c,c1,c2;
    long double xa,ya,xb,yb,xc,yc;
    a=dis(t.t[0],t.t[1]);
    b=dis(t.t[1],t.t[2]);
    c=dis(t.t[2],t.t[0]);
    //printf("a=%lfb=%lf,c=%lf\n",a,b,c);
    /*
    正弦定理a/sinA=b/sinB=c/sinC=2R,R为三角形外接圆半径
    三角形面积S=absinC/2=ab(c/2R)/2=abc/4R;
    */
    tmp.r=a*b*c/triArea(t)/4;
    xa=t.t[0].x;ya=t.t[0].y;
    xb=t.t[1].x;yb=t.t[1].y;
    xc=t.t[2].x;yc=t.t[2].y;
    c1=(xa*xa+ya*ya-xb*xb-yb*yb)/2;
    c2=(xa*xa+ya*ya-xc*xc-yc*yc)/2;
    //求外接圆圆心坐标
    //三角形外接圆是三条边上垂直平分线的交点
    tmp.centre.x=(c1*(ya-yc)-c2*(ya-yb))/((xa-xb)*(ya-yc)-(xa-xc)*(ya-yb));
    tmp.centre.y=(c1*(xa-xc)-c2*(xa-xb))/((ya-yb)*(xa-xc)-(ya-yc)*(xa-xb));
    return tmp;
}
int  main()
{
    int T;
    cin>>T;
    while(T--){
        TTriangle t;
        TPoint p;
        //scanf("%lf%lf%lf%lf%lf%lf%lf%lf",&t.t[0].x,&t.t[0].y,&t.t[1].x,&t.t[1].y,&t.t[2].x,&t.t[2].y,&p.x,&p.y);
        cin>>t.t[0].x>>t.t[0].y>>t.t[1].x>>t.t[1].y>>t.t[2].x>>t.t[2].y>>p.x>>p.y;
        TCircle c=f(t);
        if(dis(p,c.centre)-c.r>eps){
            cout<<"Accepted"<;
        }else cout<<"Rejected"<;
    }
    return 0;
}

你可能感兴趣的:(三角形的外接圆,C/C++,计算几何)