#include <iostream>
#define PI 3.14
using namespace std;
class Point
{
public:
float x;
float y;
};
class circle
{
public:
float radius;
Point c_p;//圆心
void perimeter()
{
float c = 2 * radius * PI;
cout << "该圆的周长为:" << c << endl;
}
void area()
{
float s = radius * radius * PI;
cout << "该圆的面积为:" << s << endl;
}
void centerpoint()
{
cout << "圆心坐标为:" << "(" << c_p.x << "," << c_p.y << ")" << endl;
cout << "圆半径为:" << radius << endl;
}
void PointCircle(Point p1)
{
float d_x = (c_p.x - p1.x) * (c_p.x - p1.x);//两点间x距离的平方
float d_y = (c_p.y - p1.y) * (c_p.y - p1.y);//两点间y距离的平方
float d_r = radius * radius;//圆半径的平方
cout << "点坐标为:" << "(" << p1.x << "," << p1.y << ")" << endl;
if (d_r>d_x+d_y)//x^2+y^2=d^2点到圆心(0,0)的距离
{
cout << "点在圆内" << endl;
}
else if (d_r<d_x+d_y)
{
cout << "点在圆外" << endl;
}
else
{
cout << "点在圆上" << endl;
}
}
};
int main()
{
Point center;//设置点center为(0,0)
center.x = 0;
center.y = 0;
Point p1;//设置点p1
//p1.x = 1;
//p1.y = 1;
cout << "请输出点坐标(x,y):";
cin >> p1.x;
cin >> p1.y;
circle c1;//设置圆
//c1.radius = 3;//半径为3
cout << "请输入半径:";
cin >> c1.radius;
c1.c_p = center;//圆心为点center
c1.centerpoint();//显示圆心、半径
c1.perimeter();//求周长
c1.area();//求面积
c1.PointCircle(p1);//设置的点p1是否在圆内
return 0;
}
【C++】封装一个圆类,求周长面积,判断点相对于圆的位置
于 2022-04-14 16:53:26 首次发布