#include<iostream>
using namespace std;
class Point
{
public:
//设置x坐标
void setX(int x)
{
m_X = x;
}
//获取x坐标
int getX()
{
return m_X;
}
//设置y坐标
void setY(int y)
{
m_Y = y;
}
//获取y坐标
int getY()
{
return m_Y;
}
private:
int m_X;
int m_Y;
};
class Circle//圆类
{
public:
//设置半径
void setR(int r)
{
m_R = r;
}
//获取半径
int getR()
{
return m_R;
}
//设置圆心
void setCenter(Point Center)
{
m_Center = Center;
}
//获取圆心
Point getCenter()
{
return m_Center;
}
private:
int m_R;//半径
Point m_Center;//圆心
};
void isincircle(Circle &c,Point &p )
{
//2点之间距离的平方
int distance =
(c.getCenter().getX() - p.getX()) * (c.getCenter().getX() - p.getX())
+ (c.getCenter().getY() - p.getY()) * (c.getCenter().getY() - p.getY());
int rdistance = c.getR() * c.getR();
if (distance == rdistance) {
cout << "点在圆上" << endl;
}
else if (distance > rdistance)
{
cout << "点在圆外" << endl;
}
else cout << "点在圆内" << endl;
}
int main()
{
//创建圆
Circle c;
c.setR(10);
Point center;
center.setX(10);
center.setY(0);
c.setCenter(center);
//创建点
Point p;
p.setX(10);
p.setY(10);
//判断关系
isincircle(c, p);
return 0;
}
类和对象-封装-点和圆的位置关系
最新推荐文章于 2024-08-14 17:18:35 发布