51nod1298 圆与三角形(点到点,点到线段距离模板)

本文介绍了一个通过计算几何的方法来判断给定圆与三角形是否相交的问题。通过计算圆心到三角形各顶点及边的距离,确定二者间的关系。适用于基础算法训练。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目来源:  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 <bits/stdc++.h>
using namespace std;
typedef long long LL;
double dis(double x,double y,double x1,double y1)//点到点
{
    double a=(x-x1)*(x-x1);
    double b=(y-y1)*(y-y1);
    return a+b;
}
double ldis(double x,double y,double x1,double y1,double x2,double y2)//点到线段
{
    double cross=(x-x1)*(x2-x1)+(y-y1)*(y2-y1);
    if(cross<=0)
        return dis(x,y,x1,y1);
    double d2=(x2-x1)*(x2-x1)+(y2-y1)*(y2-y1);
    if(cross>=d2)
        return dis(x,y,x2,y2);
    double r=cross/d2;
    double px=x1+(x2-x1)*r;
    double py=y1+(y2-y1)*r;
    return dis(x,y,px,py);
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        double x,y,r,x1,x2,x3,y1,y2,y3;
        scanf("%lf%lf%lf",&x,&y,&r);
        scanf("%lf%lf%lf%lf%lf%lf",&x1,&y1,&x2,&y2,&x3,&y3);
        r*=r;
        double a1=dis(x,y,x1,y1);

        double b1=dis(x,y,x2,y2);

        double c1=dis(x,y,x3,y3);
        if(a1<r&&b1<r&&c1<r)
        {
            printf("No\n");
        }
        else
        {
            double a=ldis(x,y,x1,y1,x2,y2);
            double b=ldis(x,y,x1,y1,x3,y3);
            double c=ldis(x,y,x2,y2,x3,y3);
            if(a>r&&b>r&&c>r)
                printf("No\n");
            else printf("Yes\n");
        }


    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值