C++利用类和对象判断点和圆的位置关系

案例要求如下:

代码案例如下:

#include<iostream>
using namespace std;

class Circle
{
private: 
	double centerX;
	double centerY;
	double radius;

public:
	void setCenterX(double x)
	{
		centerX = x;
	}
	
	void setCenterY(double y)
	{
		centerY = y;
	}

	void setRadius(double r)
	{
		radius = r;
	}

	double getCenterX()
	{
		return centerX;
	}

	double getCenterY()
	{
		return centerY;
	}

	double getRadius()
	{
		return radius;
	}
};

class Point
{
private:
	double P_x;
	double P_y;

public:
	void setP_x(double x)
	{
		P_x = x;
	}

	void setP_y(double y)
	{
		P_y = y;
	}

	double getP_x()
	{
		return P_x;
	}

	double getP_y()
	{
		return P_y;
	}

};

//计算点离圆心距离的平方
double calDistance(Circle c, Point p)
{
	return (c.getCenterX() - p.getP_x()) * (c.getCenterX() - p.getP_x()) + (c.getCenterY() - p.getP_y()) * (c.getCenterY() - p.getP_y());
}

//比较点离圆心距离的平方和半径的平方。判断点在圆外,圆上还是圆内
void judge(Circle c, Point p)
{
	if (calDistance(c, p) > c.getRadius() * c.getRadius())
	{
		cout << "点在圆外" << endl;
	}
	else if (calDistance(c, p) == c.getRadius() * c.getRadius())
	{
		cout << "点在圆上" << endl;
	}
	else
	{
		cout << "点在圆内" << endl;
	}
}

int main() {
	Circle c1;
	Point p1;

	c1.setCenterX(2.4);
	c1.setCenterY(4.3);
	c1.setRadius(5.1);

	p1.setP_x(3.5);
	p1.setP_y(6.2);

	judge(c1, p1);
}

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值