题解 | 点和圆的关系

#include <cmath>
#include <iostream>
#include <type_traits>
using namespace std;

// 点类
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;
        }

};

// 圆类
class Circle {

    private:
        Pointer center; // 圆心
        int radius; // 半径

        int Dist(const int& x1, const int& y1, const int& x2, const int& y2){
            int d2 = pow(x1 - x2, 2) + pow(y1 - y2, 2);
            
            return sqrt(d2);
        }

    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 d = Dist(p.getX(), p.getY(), center.getX(), center.getY());

            if (d > this->radius) {
                cout << "out" << endl;
            } else if (d < this->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;
}

全部评论

相关推荐

ZywOo_求职版:谁问你了....
投递字节跳动等公司8个岗位
点赞 评论 收藏
分享
07-10 12:17
已编辑
商丘师范学院 Java
后来123321:别着急,我学院本大二,投了1100份,两个面试,其中一个还是我去线下招聘会投的简历,有时候这东西也得看运气
无实习如何秋招上岸
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
07-08 10:39
一个证都没&nbsp;我能填什么
程序员小白条:别人有,你为什么没有,还是这个道理,社会就是比较,竞争,淘汰,你要安逸,那么就要做好淘汰的准备
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务