Problem C: 判断两个圆之间的关系

网友投稿 822 2022-11-28

Problem C: 判断两个圆之间的关系

Problem C: 判断两个圆之间的关系

Problem C: 判断两个圆之间的关系

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

每个测试用例对应一行输出,输出两个圆之间的位置关系。见样例。

#include using namespace std;int cmp() {}class Point{public: double x, y; Point(double a, double b): x(a), y(b) {} Point(const Point& p) { x = p.x; y = p.y; } void setx(int a) { x = a; } void sety(int b) { y = b; } double getx() { return x; } double gety() { return y; }};class Circle{private: Point coord; double r;public: Circle(Point p, double a): coord(p), r(a) {} int JudgeRelation(const Circle& another) { double d1 = r + another.r, d2 = fabs(r - another.r); double x1 = coord.x, y1 = coord.y; double x2 = another.coord.x, y2 = another.coord.y; if ( (x1 - x2)*(x1 - x2) + (y1 - y2)*(y1 - y2) > d1*d1 ) return 1; if ( (x1 - x2)*(x1 - x2) + (y1 - y2)*(y1 - y2) < d2*d2 ) return 2; if ( (x1 - x2)*(x1 - x2) + (y1 - y2)*(y1 - y2) == d1*d1 ) return 3; if ( (x1 - x2)*(x1 - x2) + (y1 - y2)*(y1 - y2) == d2*d2 ) return 4; if ( ((x1 - x2)*(x1 - x2) + (y1 - y2)*(y1 - y2) < d1*d1) && ((x1 - x2)*(x1 - x2) + (y1 - y2)*(y1 - y2) > d2*d2)) 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"<

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Spring boot 在idea中添加热部署插件的图文教程
下一篇:1738: 最小路径覆盖问题 ——最小不相交路径覆盖
相关文章

 发表评论

暂时没有评论,来抢沙发吧~