输入平面上的一个点的坐标,以及一个圆的圆心坐标和半径,输出该点到原点的距离,并判断该点在圆内,圆外还是在圆周上。 在圆外时,将输出“outside",在圆内时,输出"inside",在圆周上时,输出“on"。 下面已经给出了点类point的部分定义,请将其函数定义补充完整,并添加对圆类circle的完整定义。
时间: 2023-05-31 07:18:58 浏览: 457
### 回答1:
点类point的函数定义:
class point{
private:
double x,y;
public:
point(double x=,double y=):x(x),y(y){}
double distance(){return sqrt(x*x+y*y);}
double getX(){return x;}
double getY(){return y;}
void setX(double x){this->x=x;}
void setY(double y){this->y=y;}
void setXY(double x,double y){this->x=x;this->y=y;}
void print(){cout<<"("<<x<<","<<y<<")";}
bool isInCircle(circle c);
};
圆类circle的完整定义:
class circle{
private:
point center;
double radius;
public:
circle(point center,double radius):center(center),radius(radius){}
point getCenter(){return center;}
double getRadius(){return radius;}
void setCenter(point center){this->center=center;}
void setRadius(double radius){this->radius=radius;}
void print(){cout<<"center:";center.print();cout<<",radius:"<<radius;}
bool contains(point p);
};
bool point::isInCircle(circle c){
double d=sqrt(pow(x-c.getCenter().getX(),2)+pow(y-c.getCenter().getY(),2));
if(d<c.getRadius()) return true;
else if(d>c.getRadius()) return false;
else return true;
}
bool circle::contains(point p){
double d=sqrt(pow(p.getX()-center.getX(),2)+pow(p.getY()-center.getY(),2));
if(d<radius) return true;
else if(d>radius) return false;
else return true;
}
int main(){
double x,y,r;
cout<<"请输入点的坐标:";
cin>>x>>y;
point p(x,y);
cout<<"请输入圆的圆心坐标和半径:";
cin>>x>>y>>r;
circle c(point(x,y),r);
cout<<"该点到原点的距离为:"<<p.distance()<<endl;
if(p.isInCircle(c)){
if(c.contains(p)) cout<<"该点在圆内"<<endl;
else cout<<"该点在圆周上"<<endl;
}
else cout<<"该点在圆外"<<endl;
return ;
}
### 回答2:
点类的部分定义:
class point{
public:
double x, y;
point(double a = 0, double b = 0){
x = a;
y = b;
}
double distance(){
//补充代码
}
bool in_circle(circle C){
//补充代码
}
};
圆类的完整定义:
class circle{
public:
double x, y, r;
circle(double a = 0, double b = 0, double c = 0){
x = a;
y = b;
r = c;
}
bool in_circle(point P){
//补充代码
}
};
点类中的distance函数可以使用勾股定理计算点到原点的距离,代码如下:
double distance(){
return sqrt(x*x + y*y);
}
点类中的in_circle函数需要判断点是否在圆内,可以利用勾股定理和圆的半径判断,代码如下:
bool in_circle(circle C){
double d = sqrt((x - C.x) * (x - C.x) + (y - C.y) * (y - C.y));
if(d > C.r) return false;
else if(d < C.r) return true;
else return false;
}
圆类中的in_circle函数需要判断点是否在圆内,同样可以利用勾股定理和圆的半径判断,代码如下:
bool in_circle(point P){
double d = sqrt((P.x - x) * (P.x - x) + (P.y - y) * (P.y - y));
if(d > r) return false;
else if(d < r) return true;
else return false;
}
最后,在主函数中,可以根据点到原点的距离和点在圆内外的情况输出不同的结果,代码如下:
int main(){
double x, y, r;
cin >> x >> y >> r;
circle C(x, y, r);
point P;
cin >> P.x >> P.y;
double d = P.distance();
if(C.in_circle(P) == false) cout << "outside" << endl;
else if(d < r) cout << "inside" << endl;
else if(d == r) cout << "on" << endl;
else cout << "outside" << endl;
return 0;
}
### 回答3:
点类point的定义:
```c++
class point{
public:
int x,y;
point(int _x=0,int _y=0):x(_x),y(_y){}
point(const point &p):x(p.x),y(p.y){}
point operator+(const point &p)const{return point(x+p.x,y+p.y);}
point operator-(const point &p)const{return point(x-p.x,y-p.y);}
point operator*(const int &d)const{return point(x*d,y*d);}
bool operator==(const point &p)const{return x==p.x&&y==p.y;}
bool operator<(const point &p)const{return x<p.x||x==p.x&&y<p.y;}
int distance(){return sqrt(x*x+y*y);}
};
```
圆类circle的定义:
```c++
class circle{
public:
point O;
int r;
circle(){}
circle(point _O,int _r):O(_O),r(_r){}
circle(const circle &c):O(c.O),r(c.r){}
int area(){return PI*r*r;}
bool in_circle(point p);
};
bool circle::in_circle(point p){
if( (p-O).distance()<r ) return true;
else if( (p-O).distance()==r ) return false;
else return false;
}
```
主程序部分:
```c++
int main(){
point p,c;
int r;
cin>>p.x>>p.y>>c.x>>c.y>>r;
cout<<p.distance()<<" "; //输出到原点的距离
circle C(c,r);
if( C.in_circle(p) ) cout<<"inside"<<endl; //在圆内
else if( (p-O).distance()>r ) cout<<"outside"<<endl; //在圆外
else cout<<"on"<<endl; //在圆周上
return 0;
}
```
阅读全文