51NOD 1298 圆与三角形

来源:互联网 发布:java实现第三方登录 编辑:程序博客网 时间:2024/06/04 08:36

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

1.既有点在圆内,又有点在圆外,必相交。
2. 所有点在圆内,必不相交
3. 所有点都在圆外:如果有边满足条件a(见下行),则相交,否则不相交

条件a:当圆心的投影在边上时,如果点到直线距离小于半径,则满足;
当圆心的投影不在边上时,取点到直线两端较小者,如果小于半径,则满足。

代码:

#include<iostream>#include<stdio.h>#include<cstring>#include<cmath>#include<cstdlib>#include<algorithm>#define LL long longusing 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;}
0 0