#include <iostream>
#include <cmath>
using namespace std;
//同类之间无私处。
class Point{
public:
//设置point的两个坐标
void setXY(int x, int y){
m_x = x;
m_y = y;
}
int getX(){
return m_x;
}
//根据另一个点,返回两个点之间的距离
double pointDistance(Point &p){
int d_x = p.m_x - m_x;
int d_y = p.m_y - m_y;
double dis = sqrt(d_x*d_x + d_y*d_y);
return dis;
}
private:
int m_x;
int m_y;
};
class Circle{
public:
//设置圆的半径
void setR(int r){
m_r = r;
}
//设置圆的坐标
void setXY(int x, int y){
p0.setXY(x, y);
}
//判断另一个圆是否跟自己相交
bool isIntersection(Circle & another_c){
int rr = m_r + another_c.m_r; //两个圆的半径之和
double dis = p0.pointDistance(another_c.p0);
if (dis <= rr) {
//相交
return true;
}
else {
//不相交
return false;
}
}
private:
int m_r; //半径
Point p0;// p0 就是圆的圆心点
};
int main(void){
Circle *circle1 = new Circle();
Circle *circle2 = new Circle();
int x, y, r;
cout << "请输入第一个圆的半径" << endl;
cin >> r;
circle1 -> setR(r);
cout << "请输入第一个圆的x" << endl;
cin >> x;
cout << "请输入第一个圆的y" << endl;
cin >> y;
circle1 -> setXY(x, y);
cout << "请输入第2个圆的半径" << endl;
cin >> r;
circle2 -> setR(r);
cout << "请输入第2个圆的x" << endl;
cin >> x;
cout << "请输入第2个圆的y" << endl;
cin >> y;
circle2 -> setXY(x, y);
if (circle1 -> isIntersection(*circle2) == true) {
cout << "两个圆相交" << endl;
} else {
cout << "两个圆不相交" << endl;
}
delete circle1;
delete circle2;
}
C++ 判断两个圆是否相交
最新推荐文章于 2022-05-22 08:47:21 发布