Problem D: 判断两个圆之间的关系
Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 1613 Solved: 1346
[ Submit][ Status][ Web Board]
Description
定义Point类,包括double类型的两个属性,分别表示二维空间中一个点的横纵坐标;定义其必要的构造函数和拷贝构造函数。
定义Circle类,包括Point类的对象和一个double类型的数据作为其属性,分别表示圆心坐标及半径;定义其必要的构造函数、拷贝构造函数。定义Circle类的成员函数:
int JudgeRelation(const Circle& another)
用于判断当前圆与another之间的位置关系。该函数的返回值根据以下规则确定:当两个圆外离时返回1;当两个圆内含时返回2;当两个圆外切时返回3; 当两个圆内且时返回4;当两个圆相交时返回5。
Input
第1行N>0表示测试用例个数。
每个测试用例包括2行,第1行是第1个圆的位置及半径;第2行是第2个圆的位置和半径。
Output
每个测试用例对应一行输出,输出两个圆之间的位置关系。见样例。
Sample Input
50 0 1020 20 10 0 101 1 40 0 100 20 100 0 100 5 50 0 1015 0 10
Sample Output
OutsideInsideExternally tangentInternally tangentIntersection
HINT
外离与内含均指两个圆没有任何交点,但内含是指一个圆完全包含在另一个的内部,否则便是外离。
Append Code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
|
#include<iostream>
#include<cmath>
using
namespace
std;
class
Point{
public
:
double
x,y;
Point(
double
a=0,
double
b=0):x(a),y(b){}
~Point(){}
};
class
Circle{
friend
class
Point;
public
:
Point p;
double
r;
Circle(Point a,
double
R):p(a),r(R){}
~Circle(){}
int
JudgeRelation(
const
Circle& cc)
{
double
dc=sqrtl((cc.p.x - p.x)*(cc.p.x - p.x)+(cc.p.y - p.y)*(cc.p.y - p.y));
if
(
fabs
(dc - (r + cc.r)) < 1e-6) {
return
3;
}
if
(
fabs
(dc -
fabs
(r - cc.r)) < 1e-6)
{
return
4;
}
if
(dc <
fabs
(r - cc.r))
return
2;
if
(dc > r + cc.r)
return
1;
return
5;
}
};
int
main()
{
int
cases;
double
x, y, r;
cin>>cases;
for
(
int
i = 0; i < cases; i++)
{
cin>>x>>y>>r;
Point p1(x,y);
Circle c1(p1, r);
cin>>x>>y>>r;
Point p2(x, y);
Circle c2(p2, r);
switch
(c1.JudgeRelation(c2))
{
case
1:
cout<<
"Outside"
<<endl;
break
;
case
2:
cout<<
"Inside"
<<endl;
break
;
case
3:
cout<<
"Externally tangent"
<<endl;
break
;
case
4:
cout<<
"Internally tangent"
<<endl;
break
;
case
5:
cout<<
"Intersection"
<<endl;
}
}
}
|