题解 | #点和圆的关系#
点和圆的关系
https://www.nowcoder.com/practice/fdbbd0aaf89f49818e9ee1afb02deb10
使用友元类
#include <iostream> using namespace std; class Circle; // 点类 class Pointer { private: int x; // x 坐标 int y; // y 坐标 public: void setX(int x) { this->x = x; } int getX() { return x; } void setY(int y) { this->y = y; } int getY() { return y; } friend Circle; }; // 圆类 class Circle { private: Pointer center; // 圆心 int radius; // 半径 public: void setCenter(int x, int y) { center.setX(x); center.setY(y); } void setRadius(int radius) { this->radius = radius; } // write your code here...... void isPointerInCircle(Pointer p){ int dd = (p.x - this->center.x)* (p.x - this->center.x) + (p.y - this->center.y) * (p.y - this->center.y); if(dd > radius*radius) cout<<"out"<<endl; else if(dd < radius*radius) cout<<"in"<<endl; else cout<<"on"<<endl; } }; int main() { // 键盘输入点的坐标 int x, y; cin >> x; cin >> y; // 创建一个点 Pointer p; p.setX(x); p.setY(y); // 创建一个圆 Circle c; c.setCenter(5, 0); c.setRadius(5); // 判断点和圆的关系 c.isPointerInCircle(p); return 0; }