51nod 1298

来源:互联网 发布:网络电视直播软件pc版 编辑:程序博客网 时间:2024/06/05 15:18

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示例
20 0 1010 015 015 50 0 100 05 05 5
Output示例
YesNo



#include <iostream>#include <stdio.h>#include <math.h>#include <stdlib.h>#define eps 1e-6using 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;}


原创粉丝点击