1. 程式人生 > >51Nod 1298 圓與三角形 (計算幾何)

51Nod 1298 圓與三角形 (計算幾何)

#include<stdio.h>
#include<string.h>
#include<math.h>
#include<algorithm>
using namespace std;
double x,y,r;
double dis(double x1,double y1)
{
	return (x1-x)*(x1-x)+(y1-y)*(y1-y);
}
bool judge(double x1,double y1,double x2,double y2)
{
	double d1=dis(x1,y1);
	double d2=dis(x2,y2); 
	if(d1==r*r||d2==r*r)
	return true;
	if((d1<r*r&&d2>r*r)||(d1>r*r&&d2<r*r))
	return true;
	if(d1<r*r&&d2<r*r)
	return false;
    if (x1 == x2)     //斜率不存在,特殊處理   
    {   
        double maxy=max (y1,y2);  
        double miny= min (y1,y2);  
        if(miny>y)  //開始這地方沒判斷, WA了 
        return false;  
        else if(maxy<y)  
        return false;  
        else if(fabs(x-x1)<= r)     //點到直線的距離小於半徑  
        return true;  
        else  
        return false;   
    }  
	double k,b;
	k=(y1-y2)/(x1-x2);//斜率 
	b=y1-k*x1;
	double maxx=max(x1,x2);  
    double minx=min(x1,x2);  
    double A,B,C;  
    double K;       //對稱軸   
    A=k*k+1;  
    B=2*k*(b-y)-2*x;  
    C = pow(b-y,2) + x*x;  
    K=-B/2/A;  
    if (K>maxx)  
    {  
        if(A*maxx*maxx+B*maxx + C <= r*r)  
        return true;  
        else  
        return false;  
    }  
    else if (K < minx)  
    {  
        if(A*minx*minx+B*minx+C<= r*r)  
        return true;  
        else  
        return false;  
    }  
    else  
    {  
        if(A*K*K+B*K+C<=r*r)  
        return true;  
        else  
        return false;  
    }  
}
using namespace std;
int main()
{
	int t,i;
	double x1[5],y1[5];
	scanf("%d",&t);
	while(t--)
	{
		scanf("%lf%lf%lf",&x,&y,&r);
		for(i=0;i<3;i++)
		{
			scanf("%lf%lf",&x1[i],&y1[i]);
		}
		if(judge(x1[0],y1[0],x1[1],y1[1])||judge(x1[0],y1[0],x1[2],y1[2])||judge(x1[1],y1[1],x1[2],y1[2]))
		printf("Yes\n");
		else printf("No\n");
		 
	}
	return 0;
}