欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

判断点是否在 圆的内部(类与对象)

程序员文章站 2024-03-16 14:34:28
...
#include<iostream>
using namespace std;


class Point
{
public:
	void setXY(int x, int y)
	{
		m_x = x;
		m_y = y;
	}
	int getX()
	{
		return m_x;
	}
	int getY()
	{
		return m_y;
	}
private:
	int m_x;
	int m_y;

};

class Circle
{
public:
	void setXY(int x, int y)
	{
		x0 = x;
		y0 = y;
	}
	void setR(int r)
	{
		m_r = r;
	}
	bool judgePoint(Point &p)
	{
		int dd;
		dd = (p.getX() - x0)*(p.getX() - x0) + (p.getY() - y0)*(p.getY() - y0);
		if (dd > m_r*m_r)
		{
			return false;
		}
		else
		{
			return true;
		}
	}
private:
	int x0;
	int y0;
	int m_r;
};

int main()
{
	Circle c;
	c.setXY(2, 2);
	c.setR(2);

	Point p;
	p.setXY(8, 8);

	if (c.judgePoint(p) == true)
	{
		cout << "圆的内部" << endl;
	}
	else
	{
		cout << "圆的外部" << endl;
	}

	return 0;
}