码迷,mamicode.com
首页 > 其他好文 > 详细

51 Nod 1298 圆与三角形(计算几何)

时间:2018-02-14 19:58:34      阅读:156      评论:0      收藏:0      [点我收藏+]

标签:input   get   线段   技术分享   模板题   define   include   name   ios   

题目链接:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=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
题解:模板题
 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 
 4 #define PI acos(-1.0)
 5 #define INF 0x3f3f3f3f
 6 #define FAST_IO ios::sync_with_stdio(false)
 7 
 8 typedef long long LL;
 9 
10 //点结构
11 struct point{
12     double x,y;
13 };
14 
15 //两点之间距离
16 double dis(point p1,point p2){
17     return sqrt((p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y));
18 }
19 
20 //判断线段是否和圆相交,这个是点p1和p2都不在圆内的情况。
21 //其他的判断很简单的,单独判断一下即可。
22 bool check(point p1,point p2,point O,double r){
23     double a,b,c,dis1,dis2,angle1,angle2;
24     if(p1.x==p2.x){
25         a=1;b=0;c=-p1.x;
26     }
27     else if(p1.y==p2.y){
28         a=0;b=1;c=-p1.y;
29     }
30     else{
31         a=p1.y-p2.y;
32         b=p2.x-p1.x;
33         c=p1.x*p2.y-p1.y*p2.x;
34     }
35     dis1=a*O.x+b*O.y+c;
36     dis1*=dis1;
37     dis2=(a*a+b*b)*r*r;
38     if(dis1>dis2) return false;
39     angle1=(O.x-p1.x)*(p2.x-p1.x)+(O.y-p1.y)*(p2.y-p1.y);
40     angle2=(O.x-p2.x)*(p1.x-p2.x)+(O.y-p2.y)*(p1.y-p2.y);
41     if(angle1>0&&angle2>0) return true;
42     return false;
43 }
44 
45 
46 int main(){
47     int t;
48     cin>>t;
49     while(t--){
50         double r;
51         point O,p1,p2,p3;
52         cin>>O.x>>O.y>>r;
53         cin>>p1.x>>p1.y>>p2.x>>p2.y>>p3.x>>p3.y;
54         double d1=dis(p1,O);
55         double d2=dis(p2,O);
56         double d3=dis(p3,O);
57         if(d1<r&&d2<r&&d3<r) cout<<"No"<<endl;
58         else if(d1>r&&d2>r&&d3>r){
59             if(check(p1,p2,O,r)||check(p1,p3,O,r)||check(p2,p3,O,r)) cout<<"Yes"<<endl;
60             else cout<<"No"<<endl;
61         }
62         else cout<<"Yes"<<endl;
63     }
64     return 0;
65 }

 

51 Nod 1298 圆与三角形(计算几何)

标签:input   get   线段   技术分享   模板题   define   include   name   ios   

原文地址:https://www.cnblogs.com/Leonard-/p/8448765.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!