给出圆的圆心和半径,以及三角形的三个顶点,问圆同三角形是否相交。相交输出"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 <iostream>
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#define eps 1e-6
using namespace std;
double getDistance(double x1,double y1,double x2,double y2)
{
return sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
}
double getNearestDistance(double x1,double y1,double x2,double y2,double x,double y,double r)
{
double a = getDistance(x1,y1,x,y);
double b = getDistance(x2,y2,x,y);
double c = getDistance(x1,y1,x2,y2);
if(a*a>=b*b+c*c)
return b;
if(b*b>=a*a+c*c)
return a;
double L = (a+b+c)/2;
double S = sqrt(L*(L-a)*(L-b)*(L-c)); //海伦公式 自行百度
return 2*S/c; // 知道面积和一条边长求高
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
double x,y,r; ///圆心和半径
double x1,y1,x2,y2,x3,y3; ///三角形三点坐标
double dis1,dis2,dis3,dis4,dis5,dis6;
scanf("%lf%lf%lf",&x,&y,&r);
scanf("%lf%lf",&x1,&y1);
scanf("%lf%lf",&x2,&y2);
scanf("%lf%lf",&x3,&y3);
dis1 = getDistance(x,y,x1,y1); ///点1到圆心的距离
dis2 = getDistance(x,y,x2,y2); ///点2到圆心的距离
dis3 = getDistance(x,y,x3,y3); ///点3到圆心的距离
dis4 = getNearestDistance(x1,y1,x2,y2,x,y,r);
dis5 = getNearestDistance(x1,y1,x3,y3,x,y,r);
dis6 = getNearestDistance(x2,y2,x3,y3,x,y,r);
if(dis4-r>eps && dis5-r>eps && dis6-r>eps) ///三点在圆外且圆心到直线到圆心的距离大于2,且
printf("No\n");
else if(dis1<r && dis2<r && dis3<r) ///三点在圆内,一定没有交点,
printf("No\n");
else
printf("Yes\n"); ///三点中有在圆外的点,有在圆内的点。
}
system("pause");
return 0;
}