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"。
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; const double eps = 1e-6; struct Point { double x, y; Point(double _x = 0, double _y = 0) : \ x(_x), y(_y) {} }; typedef Point Vector; int dcmp(double x) { if(fabs(x) < eps) return 0; else return x < 0 ? -1 : 1; } Vector operator + (Vector A, Vector B) { return Vector(A.x+B.x, A.y+B.y); } Vector operator - (Point A, Point B) { return Vector(A.x-B.x, A.y-B.y); } bool operator ==(const Point& a, const Point& b) { return dcmp(a.x-b.x) == 0 && dcmp(a.y-b.y) == 0; } double Dot(Vector A, Vector B) { return A.x*B.x + A.y*B.y; } double Length(Vector A) { return sqrt(Dot(A, A)); } double Cross(Vector A, Vector B) { return A.x*B.y - A.y*B.x; } double DistanceToSegment(Point P, Point A, Point B) { if(A == B) return Length(P-A); Vector v1 = B-A, v2 = P-A, v3 = P-B; if(dcmp(Dot(v1,v2)) < 0) return Length(v2); else if(dcmp(Dot(v1,v3)) > 0) return Length(v3); else return fabs(Cross(v1,v2)) / Length(v1); } Point A, B, C, Circle; double R; int main() { int T; cin >> T; while(T--) { cin >> Circle.x >> Circle.y >> R; cin >> A.x >> A.y; cin >> B.x >> B.y; cin >> C.x >> C.y; int cnt = 0, cnt2 = 0; double Dis1 = DistanceToSegment(Circle, A, B), \ Dis2 = DistanceToSegment(Circle, A, C), \ Dis3 = DistanceToSegment(Circle, B, C); cnt += dcmp(Dis1-R); cnt += dcmp(Dis2-R); cnt += dcmp(Dis3-R); cnt2 += dcmp(Length(A-Circle) - R); cnt2 += dcmp(Length(B-Circle) - R); cnt2 += dcmp(Length(C-Circle) - R); if(cnt == 3 || (cnt == -3 && cnt2 == -3)) printf("No\n"); else printf("Yes\n"); } }