http://blog.csdn.net/f_zyj/article/details/52066901
实在不好意思。看完了大神的博客,还是自己写着费劲哇,不好意思写个注释就说是原创。。当个模板先记下来吧。看两天晚上了。
说下学长给讲得这篇博客有些具体点的数学思路:
#include <iostream>
#include <cstdio>
using namespace std;
typedef long long ll;
typedef struct // 点结构
{
ll x, y;
} Point;
Point A, B, C, O; // 三角形三点与圆心
ll r; // 半径<span style="white-space:pre"> </span>
ll distan(Point a,Point b) //点到点的距离。
{
return (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y);
}
int solve(Point *p_1, Point *p_2)
{
ll a, b, c, dist_1, dist_2, angle_1, angle_2; //构造ax + by +c=0的形式
a = p_1->y - p_2->y;
b = p_2->x - p_1->x;
c = p_1->x * p_2->y - p_1->y * p_2->x;
dist_1 = a * O.x + b * O.y + c;
dist_1 *= dist_1; //这一步和下一步其实就是点到直线距离 与 半径的比较,不过为了防止/法的精度损失将下面的 a^2+b^2乘过去了
dist_2 = (a * a + b * b) * r * r;
if (dist_1 > dist_2)<span style="white-space:pre"> </span> //如果R比距离要小,那么一定就不相交了
{
return 0;
}<span style="white-space:pre"> </span> //反之,如果不小,就不一定相交不相交,因为这个是线段与圆是否相交的问题,要用向量点乘积的夹角去考虑!
angle_1 = (O.x - p_1->x) * (p_2->x - p_1->x) + (O.y - p_1->y) * (p_2->y - p_1->y);
angle_2 = (O.x - p_2->x) * (p_1->x - p_2->x) + (O.y - p_2->y) * (p_1->y - p_2->y);
if (angle_1 > 0 && angle_2 > 0)
{
return 1;
}
return 0;
}
int intersect()
{
ll distance_a=distan(O,A);
ll distance_b=distan(O,B);
ll distance_c=distan(O,C);
ll dist=r*r;
if(distance_a<dist&&distance_b<dist&&distance_c<dist)//在圆内必定不相交
return 0;
if(distance_a>dist&&distance_b>dist&&distance_c>dist) //三点都在圆外
{
if(solve(&A,&B)||solve(&A,&C)||solve(&B,&C))//里面注释
return 1;
return 0;
}
return 1;//其他情况必定相交,一点在内,一点在外
}
int main()
{
int t;
scanf("%d", &t);
while (t--)
{
scanf("%lld%lld%lld%lld%lld%lld%lld%lld%lld", &O.x, &O.y, &r, &A.x, &A.y, &B.x, &B.y, &C.x, &C.y);
printf("%s\n", intersect() ? "Yes" : "No");
}
return 0;
}