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 <cstring>
#include <algorithm>
#include <queue>
#include <vector>
using namespace std;
#define M 1010
#define LL long long
struct node
{
LL x, y;
};
node no[3];
node c;
LL rl[3], r;
bool judge(node *a, node *b)//判断线段是否与圆相交
{
LL al, bl, cl, num1, num2;//化成ax+by+c=0,找到a,b,c的值
if(a->x == b->x)
{
al=1, bl=0, cl=-a->x;
}
else if(a->y == b->y)
{
al=0, bl=1, cl=-a->y;
}
else
{
al = b->y - a->y;
bl = a->x - b->x;
cl = b->x*a->y - a->x*b->y;
}
num1 = al*c.x + bl*c.y + cl;
num1 *= num1;
num2 = r*r*(al*al+bl*bl);
if(num2 < num1)//判断直线是否可以与圆相交
{
return 0;
}
LL angle1 = (c.x-a->x)*(b->x-a->x) + (c.y-a->y)*(b->y-a->y);//线段与圆是否相交的条件
LL angle2 = (c.x-b->x)*(a->x-b->x) + (c.y-b->y)*(a->y-b->y);
if(angle1>0 && angle2>0)
{
return 1;
}
return 0;
}
int main()
{
int t;
scanf("%d", &t);
while(t--)
{
scanf("%lld%lld%lld", &c.x, &c.y, &r);
for(int i=0; i<3; i++)
{
scanf("%lld%lld", &no[i].x, &no[i].y);
rl[i] = (no[i].x-c.x)*(no[i].x-c.x)+(no[i].y-c.y)*(no[i].y-c.y);
rl[i] = rl[i] - r*r;
}
if(!rl[0] || !rl[1] || !rl[2])
{
printf("Yes\n");
continue;
}
if(rl[0]<0&&rl[1]<0&&rl[2]<0)
{
printf("No\n");
continue;
}
if(rl[0]>0&&rl[1]>0&&rl[2]>0)
{
if(judge(&no[0], &no[1]) || judge(&no[0], &no[2]) || judge(&no[1], &no[2]))
{
printf("Yes\n");
}
else
{
printf("No\n");
}
}
else
{
printf("Yes\n");
}
}
return 0;
}