您的位置:首页 > 其它

51nod 1298 圆与三角形(计算几何)

2016-08-30 19:24 344 查看
1298 圆与三角形


题目来源: HackerRank

基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题


 收藏


 关注

给出圆的圆心和半径,以及三角形的三个顶点,问圆同三角形是否相交。相交输出"Yes",否则输出"No"。(三角形的面积大于0)。





Input
第1行:一个数T,表示输入的测试数量(1 <= T <= 10000),之后每4行用来描述一组测试数据。
4-1:三个数,前两个数为圆心的坐标xc, yc,第3个数为圆的半径R。(-3000 <= xc, yc <= 3000, 1 <= R <= 3000)
4-2:2个数,三角形第1个点的坐标。
4-3:2个数,三角形第2个点的坐标。
4-4:2个数,三角形第3个点的坐标。(-3000 <= xi, yi <= 3000)


Output
共T行,对于每组输入数据,相交输出"Yes",否则输出"No"。


Input示例
2
0 0 10
10 0
15 0
15 5
0 0 10
0 0
5 0
5 5


Output示例
Yes
No


(吐血)这道题我自己磕了很长时间,计算几何果然很坑爹,咳咳,其实这道题的想法是比较简单的,只是代码写起来的话一定要注意一下细节!计算几何题的细节真的很重要啊!很重要啊!!给出圆心、三角形三点坐标,以及圆的半径,判断圆与三角形是否相交= ̄ω ̄=
#include<cstdio>
#include<cmath>
#include<algorithm>
using namespace std;
struct P
{
double x,y;
};
struct circle
{
P c;
double r;
};

int cmp(double x)
{
if(fabs(x)<1e-15) return 0;
if(x>0) return 1;
return -1;
}

bool judge(double a,double b,double c,double &x1,double &x2)
{
double tep=b*b-4*a*c;
if(cmp(tep)<0) return 0;
x1=(-b+sqrt(tep))/(2*a);
x2=(-b-sqrt(tep))/(2*a);
return 1;
}
bool judge(circle o,P a,P c)    //线段与圆的关系
{
double k,b;
if(cmp(a.x-c.x)==0)
{
double t=o.r*o.r-(a.x-o.c.x)*(a.x-o.c.x);
if(t<0) return 0;
double maxy=max(a.y,c.y),miny=min(a.y,c.y),y1=sqrt(t)+o.c.y,y2=o.c.y-sqrt(t);
if(cmp(miny-y1)<=0&&cmp(y1-maxy)<=0) return 1;
if(cmp(miny-y2)<=0&&cmp(y2-maxy)<=0) return 1;
return 0;
}
k=(a.y-c.y)/(a.x-c.x);
b=a.y-k*a.x;
double x1,x2;
int f=judge(k*k+1,2*k*(b-o.c.y)-2*o.c.x,o.c.x*o.c.x+(b-o.c.y)*(b-o.c.y)-o.r*o.r,x1,x2);
if(f==0) return 0;
int maxx=max(a.x,c.x),minx=min(a.x,c.x);
if(cmp(minx-x1)<=0&&cmp(x1-maxx)<=0) return 1;
if(cmp(minx-x2)<=0&&cmp(x2-maxx)<=0) return 1;
return 0;
}
bool judge(circle o,P a,P b,P c)
{
if(judge(o,a,b)||judge(o,a,c)||judge(o,b,c)) return 1;
return 0;
}

int main()
{
int t;
circle o;
P a,b,c;
scanf("%d",&t);
while(t--)
{
scanf("%lf%lf%lf",&o.c.x,&o.c.y,&o.r);
scanf("%lf%lf%lf%lf%lf%lf",&a.x,&a.y,&b.x,&b.y,&c.x,&c.y);
if(judge(o,a,b,c)) puts("Yes");
else puts("No");
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: