判断点与圆的关系(两种方法)


一、建立点类

//点类
class point {
public:
	//设置
	void setX(float  x) {
		X = x;
	}
	//
	void setY(float  y) {
		Y = y;
	}
	//获取
	float getX() {
		return X;
	}
	float  getY() {
		return Y;
	}
private:
	float  X;
	float  Y;
};

二、建立圆类(含有判断的成员函数)

//圆类
class circle {
public:
	//设置
	void setR(float  r) {
		R = r;
	}
	void setCenter(point p) {
		Center = p;
	}
	//获取
	float  getR() {
		return R;
	}
	point getCenter() {
		return Center;
	}
	//构建成员函数判断点与圆的关系
	void pointandcircle( point& p) {
		float  d = (Center.getX() - p.getX()) * (Center.getX() - p.getX()) + (Center.getY() - p.getY()) * (Center.getY() - p.getY());//点到圆心距离的平方
		float  rD = R * R;//半径的平方
		if (rD == d) {
			cout << "成员::点在圆上!" << endl;
		}
		else if (rD > d) {
			cout << "成员::点在圆内!" << endl;
		}
		else {
			cout << "成员::点在圆外!" << endl;
		}
	}
private:
	float  R;
	point Center;
};

三、全局函数判断

//构建全局函数判断点与圆的关系
void pointandcircle(circle& c, point& p) {
	float  d = (c.getCenter().getX() - p.getX()) * (c.getCenter().getX() - p.getX()) + (c.getCenter().getY() - p.getY()) * (c.getCenter().getY() - p.getY());//点到圆心距离的平方
	float  rD = c.getR() * c.getR();//半径的平方
	if (rD == d) {
		cout << "全局::点在圆上!" << endl;
	}
	else if (rD > d) {
		cout << "全局::点在圆内!" << endl;
	}
	else {
		cout << "全局::点在圆外!" << endl;
	}
}

四、源代码

#define _CRT_SECURE_NOWARNINGS//宏定义,防止调用函数时报错
#include <iostream>
using namespace std;
//点类
class point {
public:
	//设置
	void setX(float  x) {
		X = x;
	}
	//
	void setY(float  y) {
		Y = y;
	}
	//获取
	float getX() {
		return X;
	}
	float  getY() {
		return Y;
	}
private:
	float  X;
	float  Y;
};
//圆类
class circle {
public:
	//设置
	void setR(float  r) {
		R = r;
	}
	void setCenter(point p) {
		Center = p;
	}
	//获取
	float  getR() {
		return R;
	}
	point getCenter() {
		return Center;
	}
	//构建成员函数判断点与圆的关系
	void pointandcircle( point& p) {
		float  d = (Center.getX() - p.getX()) * (Center.getX() - p.getX()) + (Center.getY() - p.getY()) * (Center.getY() - p.getY());//点到圆心距离的平方
		float  rD = R * R;//半径的平方
		if (rD == d) {
			cout << "成员::点在圆上!" << endl;
		}
		else if (rD > d) {
			cout << "成员::点在圆内!" << endl;
		}
		else {
			cout << "成员::点在圆外!" << endl;
		}
	}
private:
	float  R;
	point Center;
};
//构建全局函数判断点与圆的关系
void pointandcircle(circle& c, point& p) {
	float  d = (c.getCenter().getX() - p.getX()) * (c.getCenter().getX() - p.getX()) + (c.getCenter().getY() - p.getY()) * (c.getCenter().getY() - p.getY());//点到圆心距离的平方
	float  rD = c.getR() * c.getR();//半径的平方
	if (rD == d) {
		cout << "全局::点在圆上!" << endl;
	}
	else if (rD > d) {
		cout << "全局::点在圆内!" << endl;
	}
	else {
		cout << "全局::点在圆外!" << endl;
	}
}
void test01() {
	//点
	point p1;
	float  x1,y1;
	
	cout << "请输入点的X坐标:" << endl;
	cin >> x1;
	p1.setX(x1);
	
	cout << "请输入点的Y坐标:" << endl;
	cin >> y1;
	p1.setY(y1);
	//圆
	circle c1;
	point center;
	
	float  x2, y2, r;
	
	cout << "请输入圆心的X坐标:" << endl;
	cin >> x2;
	center.setX(x2);
	
	cout << "请输入圆心的Y坐标:" << endl;
	cin >> y2;
	center.setY(y2);
	
	cout << "请输入圆的半径:" << endl;
	cin >> r;
	c1.setR(r);
	c1.setCenter(center);
	//全局函数判断
	pointandcircle(c1, p1);
	//成员函数判断
	c1.pointandcircle(p1);
}
float main() {
    test01();
	system("pause");
	return 0;
}

 

原创文章,作者:Carrie001128,如若转载,请注明出处:https://blog.ytso.com/tech/pnotes/267720.html

(0)
上一篇 2022年6月18日 22:14
下一篇 2022年6月18日 22:20

相关推荐

发表回复

登录后才能评论