您的位置:首页 > 其它

[51nod]1298 圆与三角形

2016-07-02 09:36 375 查看
https://www.51nod.com/onlineJudge/questionCode.html#problemId=1298¬iceId=54853

判断圆与三角形是否相交,转化为判断点到线段的最小距离。

1A

要整理模板

#include<cstring>
#include<cstdio>
#include<cmath>
#include<iostream>
#include<algorithm>
using namespace std;
const double eps=1e-8;
struct node
{
double x,y;
node(){}
node(int x_,int y_)
{
x=x_;
y=y_;
}
node operator -(const node &b)
{
return node(x-b.x,y-b.y);
}
double operator *(const node &b)
{
return x*b.x+y*b.y;
}
double operator ^(const node &b)
{
return x*b.y-b.x*y;
}
} p,a[3];
double r;
int sgn(double x)
{
if(fabs(x)<eps) return 0;
if(x>0) return 1;
else return -1;
}
double dist(const node &a,const node &b)
{
return (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y);
}
double point_to_seg(node p,node a,node b)
{
double y=(p-a)*(b-a);
if(sgn(y)<=0) return dist(a,p);
if(sgn(y-dist(a,b))>=0) return dist(b,p);
double s=(p-a)^(b-a);
return s*s/dist(a,b);
}

bool ok()
{
for(int i=0;i<3;i++)
{
int x=sgn(dist(a[i],p)-r*r);
int y=sgn(dist(a[(i+1)%3],p)-r*r);
if(x*y==0 || x*y==-1) return true;
if(x<0 && y<0) continue;
if(sgn(point_to_seg(p,a[i],a[(i+1)%3])-r*r)<=0) return true;
}
return false;
}
int main()
{
int sk;
scanf("%d",&sk);
while(sk--)
{
scanf("%lf%lf%lf",&p.x,&p.y,&r);
for(int i=0;i<3;i++)
{
scanf("%lf%lf",&a[i].x,&a[i].y);
}
puts(ok()?"Yes":"No");
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  51nod