C++ 判断两个圆是否相交

#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;
	
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值