案例要求如下:
代码案例如下:
#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);
}