点和圆的关系
点类
class Point
{
public:
void setX(int x)
{
m_X = x;
}
int getX()
{
return m_X;
}
void setY(int y)
{
m_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)
{
//点到圆心距离的平方
int d =
(c.getCenter().getX() - p.getX()) *
(c.getCenter().getX() - p.getX()) +
(c.getCenter().getY() - p.getY()) *
(c.getCenter().getY() - p.getY());
//半径的平方
int rd = c.getR() * c.getR();
//判断关系
if (d == rd)
cout << "点在圆上" << endl;
else if (d > rd)
cout << "点在圆外" << endl;
else if (d < rd)
cout << "点在圆内" << endl;
}
int main()
{
Circle c;
c.setR(10);
Point center;
center.setX(0);
center.setY(0);
c.getCenter(center);
Point p;
p.setX(10);
p.setY(10);
isincircle(c, p);
system("pause");
}
c++实例点和圆的关系
最新推荐文章于 2024-07-10 10:45:37 发布