标签:end iostream include point private pause abs 判断 std
对于两圆的位置一般有五种关系:
(1) 外离:两圆的半径之和小于两圆圆心距离
(2) 外切:两圆的半径之和等于两圆圆心距离
(3) 相交:两圆的半径之和大于两圆圆心距离,两圆圆心距离大于两圆半径之差
(4) 内切:两圆的半径之差等于两圆圆心距离
(5) 内含:两圆的半径之差小于两圆圆心距离
具体实现C++代码如下:
#include<iostream>
#include<cmath>
using namespace std;
class Point {
private:
double m_x,m_y;
public:
void set(double x, double y) {
m_x = x; m_y = y;
}
double dispoint(Point& another) {
return hypot(m_x - another.m_x, m_y - another.m_y);
}
};
class Circle {
private:
double m_r;
Point p;
public:
void set(double x, double y) {
p.set(x, y);
}
void setr(double r) {
m_r = r;
}
int judge(Circle &another) {
double rr = m_r + another.m_r;
double rs = fabs(m_r - another.m_r);
double dis = p.dispoint(another.p);
if (rr == dis) return 1; //外切
else if (rr < dis) return 2; //外离
else if (dis<rr && dis>rs) return 3;//相交
else if (rs == dis) return 4;//内切
else if (rs > dis) return 5;//内含
}
};
void main() {
Circle c1, c2;
double x, y,r,jude;
cout << "请输入圆1x坐标:" << endl;
cin >> x;
cout << "请输入圆1y坐标:" << endl;
cin >> y;
c1.set(x, y);
cout << "请输入圆1半径:" << endl;
cin >> r;
c1.setr(r);
cout << "请输入圆2x坐标:" << endl;
cin >> x;
cout << "请输入圆2y坐标:" << endl;
cin >> y;
c2.set(x, y);
cout << "请输入圆2半径:" << endl;
cin >> r;
c2.setr(r);
jude = c1.judge(c2);
if (jude == 1) {
cout << "两圆相外切" << endl;
}
else if (jude == 3) {
cout << "两圆相交" << endl;
}
else if(jude == 2) {
cout << "两圆相外离" << endl;
}
else if (jude == 4) {
cout << "两圆相内切" << endl;
}
else if (jude == 5) {
cout << "两圆相内含" << endl;
}
system("pause");
}
标签:end iostream include point private pause abs 判断 std
原文地址:https://www.cnblogs.com/ggdeblog/p/10977551.html