給出圓的圓心和半徑,以及三角形的三個頂點(diǎn),問圓同三角形是否相交。相交輸出”Yes”,否則輸出”No”。(三角形的面積大于0)。

Input
第1行:一個數(shù)T,表示輸入的測試數(shù)量(1 <= T <= 10000),之后每4行用來描述一組測試數(shù)據(jù)。 4-1:三個數(shù),前兩個數(shù)為圓心的坐標(biāo)xc, yc,第3個數(shù)為圓的半徑R。(-3000 <= xc, yc <= 3000, 1 <= R <= 3000) 4-2:2個數(shù),三角形第1個點(diǎn)的坐標(biāo)。 4-3:2個數(shù),三角形第2個點(diǎn)的坐標(biāo)。 4-4:2個數(shù),三角形第3個點(diǎn)的坐標(biāo)。(-3000 <= xi, yi <= 3000)
Output
共T行,對于每組輸入數(shù)據(jù),相交輸出”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.既有點(diǎn)在圓內(nèi),又有點(diǎn)在圓外,必相交。 2. 所有點(diǎn)在圓內(nèi),必不相交 3. 所有點(diǎn)都在圓外:如果有邊滿足條件a(見下行),則相交,否則不相交
條件a:當(dāng)圓心的投影在邊上時,如果點(diǎn)到直線距離小于半徑,則滿足; 當(dāng)圓心的投影不在邊上時,取點(diǎn)到直線兩端較小者,如果小于半徑,則滿足。
代碼:
#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){ //三點(diǎn)到圓心距離 double d1 = dis_p(sjx + 0, &yx); double d2 = dis_p(sjx + 1, &yx); double d3 = dis_p(sjx + 2, &yx); //圓心到3點(diǎn)距離都小于r 不相交 if ((d1<r) && (d2<r) && (d3<r)) return false; //有點(diǎn)在圓上 相交 if ((d1 == r) || (d2 == r) || (d3 == r)) return true; //有點(diǎn)在園內(nèi) 有點(diǎn)不在 相交 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;}新聞熱點(diǎn)
疑難解答