判断两个圆是否相交

定义一个Point类, 属性包括点的坐标,提供计算两点之间距离的方法;
定义一个圆类,包括其属性和半径;
创建两个圆形对象,提示用户输入圆心坐标和半径,判断两个圆是否相交,并输出结果。

#include <iostream>
#include <cmath>

using namespace std;

//点类
class Point
{
public:
	void setXY(int x, int y)
	{
		m_x = x;
		m_y = y;
	}


	//计算两点距离的方法
	double pointDistance(Point &another)
	{
		int d_x = m_x - another.m_x;
		int d_y = m_y - another.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)
	{
		//两个半径之和
		int rr = m_r + another.m_r;
		//两圆心之间距离
		double dis = p0.pointDistance(another.p0);

		if (dis <= rr) {
			//相交
			return true;
		}
		else {
			return false;
		}
	}
private:
	int m_r;
	Point p0;
};

int main(void)
{
	Circle c1, c2;

	int x, y, r;

	cout << "请输入第一个圆的半径" << endl;
	cin >> r;
	c1.setR(r);
	cout << "请输入第一个圆的x" << endl;
	cin >> x;
	cout << "请输入第一个圆的y" << endl;
	cin >> y;
	c1.setXY(x, y);


	cout << "请输入第2个圆的半径" << endl;
	cin >> r;
	c2.setR(r);
	cout << "请输入第2个圆的x" << endl;
	cin >> x;
	cout << "请输入第2个圆的y" << endl;
	cin >> y;
	c2.setXY(x, y);

	if (c1.isIntersection(c2) == true) {
		cout << "相交" << endl;
	}
	else {
		cout << "不相交" << endl;
	}

	return 0;
}

输出结果!

  • 1
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值