c++ 判断两圆位置关系
Posted ggdeblog
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c++ 判断两圆位置关系相关的知识,希望对你有一定的参考价值。
对于两圆的位置一般有五种关系:
(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");
以上是关于c++ 判断两圆位置关系的主要内容,如果未能解决你的问题,请参考以下文章