给定一个半径为r、圆心在坐标系原点的圆,判断点K是在这个圆内还是在它的轮廓上

问题描述 投票:0回答:0

我的代码在函数 bool isInsideCircle(double r) 中有问题。有了这个功能,我需要找到点 K 是在这个圆上还是在它的轮廓上。我成功地输入了点的坐标,它成功地打印了它并且成功地计算了从中心到点的距离。 但我不知道问题出在哪里。一切正常,期待这个功能。

这是我的代码。

#include <iostream>

using namespace std;

struct Point {
    double x, y;

    void read() {
       cin >> x >> y;
    }

    void print() {
       cout << "(" << x << ", " << y << ")";
    }

    double distanceToCenter() {
        return sqrt(x * x + y * y);
    }

    bool isInsideCircle(double r)  {
        double distanceToCenter = distanceToCenter();
        if (distanceToCenter < r) {
            return true;  
        }
        else if (distanceToCenter == r) {
            return true;  
        }
        else {
            return false; 
        }
    }
};

int main() {
    Point p1, p2;

    cout << "Enter the first point (x, y): ";
    p1.read();

    cout << "The first point is: ";
    p1.print();
    cout << endl;


    cout << "The distance from the center to the first point is: ";
    cout << p1.distanceToCenter() << endl;

    Point K;
    K.read();
    double r;
    cout << "Enter the radius of the circle: ";
    cin >> r;
    if (K.isInsideCircle(r)) {
        cout << "The point ";
        K.print();
        cout << " is inside the circle." << endl;
    }
    else {
        cout << "The point ";
        K.print();
        cout << " is outside the circle." << endl;
    }

    return 0;
}

c++ oop struct coordinate-systems
© www.soinside.com 2019 - 2024. All rights reserved.