51Nod 1298 圆与三角形

本文介绍了一个用于判断圆与三角形是否相交的算法,详细解释了输入输出格式、算法流程,并提供了相应的代码实现。适用于计算机科学领域的算法研究与应用。

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

题目链接:

51Nod 1298 圆与三角形

题目描述:

给出圆的圆心和半径,以及三角形的三个顶点,问圆同三角形是否相交。相交输出”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”

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

两种情况:相交,不相交

(1)相交:

  1. 三角形三个点有在圆内的,也有在圆外的,必相交
  2. 三角形有一个顶点在圆上,必相交。
  3. 三角形三边都在圆外,圆心到三边距离,存在小于等于半径的,必相交

(2)不相交:

  1. 三角形三边都在圆内,必不相交
  2. 三角形三边都在圆外,且圆心到三边距离均大于半径,必不相交。

下面代码(改编):

/*************************************************************************
    > File Name: 1298.cpp
    > Author: dulun
    > Mail: dulun@xiyoulinux.org
    > Created Time: 2016年03月24日 星期四 11时22分10秒
 ************************************************************************/

#include<iostream>
#include<stdio.h>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<algorithm>
#define LL long long
using namespace std;
int const N = 10086;

struct P
{
    int x, y;
};

bool isonline(const P*a, const P*b, const P*c)
{
    double k;
    P ab = { b->x - a->x,b->y - a->y };
    P ac = { c->x - a->x,c->y - a->y };
    k = (ab.x*ac.x + ab.y*ac.y);/* / (sqrt(ab.x*ab.x + ab.y*ab.y)*sqrt(ac.x*ac.x + ac.y*ac.y));*/
    if (k<0) return false;
    P ba = { a->x - b->x,a->y - b->y };
    P bc = { c->x - b->x,c->y - b->y };
    k = (ba.x*bc.x + ba.y*bc.y); /* / (sqrt(ba.x*ba.x + ba.y*ba.y)*sqrt(bc.x*bc.x + bc.y*bc.y));*/
    if (k<0) return false;
    return true;
}
inline double dis_p(const P*a, const P*b)
{
    int x = a->x - b->x;
    int y = a->y - b->y;
    return sqrt(x*x + y*y);
}
double dis_xd(const P*a, const P*b, const P*c)
{
    //投影在线段上 面积法
    if (isonline(a, b, c))
    {
        P buf1 = { a->x - c->x,a->y - c->y };
        P buf2 = { b->x - c->x,b->y - c->y };
        return fabs((buf1.x*buf2.y - buf1.y*buf2.x) / dis_p(a, b));
    }
    else//不在
    {
        double t1 = dis_p(a, c);
        double t2 = dis_p(b, c);
        if (t1<t2) return t1;

        else       return t2;
    }
}

bool xj(P&yx, P*sjx, int r)
{
    //三点到圆心距离
    double d1 = dis_p(sjx + 0, &yx);
    double d2 = dis_p(sjx + 1, &yx);
    double d3 = dis_p(sjx + 2, &yx);
    //圆心到3点距离都小于r 不相交
    if ((d1<r) && (d2<r) && (d3<r)) return false;

    //有点在圆上 相交
    if ((d1 == r) || (d2 == r) || (d3 == r)) return true;

    //有点在园内 有点不在 相交
    if (d1<r)
    if ((d2>r) || (d3>r)) return true;
    if (d2<r)
    if ((d1>r) || (d3>r)) return true;
    if (d3<r)
    if ((d2>r) || (d1>r)) return true;

    //都不在 判断圆心到各边距离是否存在小于等于r的
    if (dis_xd(sjx + 0, sjx + 1, &yx) <= r) return true;
    if (dis_xd(sjx + 0, sjx + 2, &yx) <= r) return true;
    if (dis_xd(sjx + 1, sjx + 2, &yx) <= r) return true;
    return false;
}

void f(int T, bool*op)
{
    for (int i = 0; i<T; ++i)
    {
        P yx;
        P sjx[3];
        int r;
        cin >> yx.x >> yx.y >> r;
        for (int j = 0; j<3; ++j)
            cin >> sjx[j].x >> sjx[j].y;
        /*判断相交*/
        op[i] = xj(yx, sjx, r);
    }
}
void print(bool*op, int T)
{
    for (int i = 0; i<T; ++i)
        if (op[i])
            cout << "Yes" << endl;
        else
            cout << "No" << endl;
}


int main()
{
    int T;
    cin >> T;
    bool*op = new bool[T];
    f(T, op);
    print(op, T);
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值