51nod 1298 圆与三角形——计算几何
题目链接:http://www.51nod.com/Challenge/Problem.html#!#problemId=1298
转化成判断三条线段和圆是否🍌相交就行了
1 #include<cstdio> 2 using namespace std; 3 4 struct point { double x,y; }; 5 point A,B,C,O; 6 double r; 7 8 bool pan1(point P){///判断点是否在圆内 9 if( (P.x-O.x)*(P.x-O.x) + (P.y-O.y)*(P.y-O.y) -r*r <=0) 10 return 1; 11 return 0; 12 } 13 bool pan2(point p1, point p2,double r) {///判线段是否与圆相交 14 if(pan1(p1)&&pan1(p2))//都在圆内 不相交 15 return 0; 16 if(!pan1(p1)&&pan1(p2)||pan1(p1)&&!pan1(p2))//一个圆内一个圆外 相交 17 return 1; 18 19 //都在圆外 20 double a, b, c, dist1, dist2, angle1, angle2; // ax + by + c = 0; 21 if (p1.x == p2.x)//特殊情况判断,分母不能为零 22 a = 1, b = 0, c = -p1.x; 23 else if (p1.y == p2.y)//特殊情况判断,分母不能为零 24 a = 0, b = 1, c = -p1.y; 25 else { 26 a = p1.y - p2.y; 27 b = p2.x - p1.x; 28 c = p1.x * p2.y - p1.y * p2.x; 29 } 30 dist1 = a * O.x + b * O.y + c; 31 dist1 *= dist1; 32 dist2 = (a * a + b * b) * r * r; 33 if (dist1 > dist2) return 0;///点到直线距离大于半径r 34 angle1 = (O.x - p1.x) * (p2.x - p1.x) + (O.y - p1.y) * (p2.y - p1.y); 35 angle2 = (O.x - p2.x) * (p1.x - p2.x) + (O.y - p2.y) * (p1.y - p2.y); 36 if (angle1 > 0 && angle2 > 0) return 1;///余弦都为正,则是锐角 37 return 0; 38 } 39 40 int main(){ 41 int t; scanf("%d",&t); 42 while(t--){ 43 scanf("%lf%lf%lf",&O.x,&O.y,&r); 44 scanf("%lf%lf%lf%lf%lf%lf",&A.x,&A.y,&B.x,&B.y,&C.x,&C.y); 45 if(pan2(A,B,r)||pan2(A,C,r)||pan2(B,C,r)) 46 puts("Yes"); 47 else 48 puts("No"); 49 } 50 return 0; 51 }
Stay Hungry, Stay Foolish