天天看点

Problem C: 点在圆内吗?

Problem C: 点在圆内吗?

Description

定义一个Point类和Circle类,用于判断给定的一系列的点是否在给定的圆内。

其中,Point类:

1.有2个成员x和y,分别为其横坐标和纵坐标;1个静态成员numOfPoints,用于计算生成的点的个数。

2.具有构造函数、析构函数和拷贝构造函数,具体格式输出根据样例自行判断。

3. 具有静态方法int getNumOfPoints(),用于返回numOfPoints的值。

4. 具有int getX()和int getY()方法,用于获取横坐标和纵坐标。

Circle类:

1. 拥有Point类的对象center,表示圆心坐标。拥有radius对象,表示圆的半径;1个静态成员numOfCircles,用于指示生成了多少个圆对象。

2. 具有构造函数、析构函数和拷贝构造函数,具体格式根据样例自行判断。

3.具有静态方法int getNumOfCircles(),返回numOfCircles的值。

4. 具有getCenter()方法,返回圆心坐标。注意:根据输出结果判断返回值类型。

5. 具有bool pointInCircle(Point &)方法,用于判断给定的点是否在当前圆内。是则返回true,否则返回false。

Input

输入分多行。

第一行M>0,表示有M个测试用例。

每个测试用例又包括多行。第1行包含3个整数,分别为一个圆的横坐标、纵坐标和半径。第2行N>0,表示之后又N个点,每个点占一行,分别为其横坐标和纵坐标。

所有输入均为整数,且在int类型范围内。

Output

输出见样例。注意:在圆的边上的点,不在圆内。

#include <iostream>

using namespace std;

class Point {
private:
    int x, y;
    static int numOfPoint;
public:
    Point(int xx = 0, int yy = 0) : x(xx), y(yy) {
        numOfPoint++;
        cout << "The Point ("<<x<<", "<<y<<") is created! Now, we have "<<numOfPoint<<" points." << endl;
    }
    Point(const Point &another) {
        x = another.x, y = another.y;
        numOfPoint++;
        cout << "A Point ("<<x<<", "<<y<<") is copied! Now, we have "<<numOfPoint<<" points." << endl;
    }
    ~Point() {
        numOfPoint--;
        cout << "A Point ("<<x<<", "<<y<<") is erased! Now, we have "<<numOfPoint<<" points." << endl;
    }
    int getX() { return x; }
    int getY() { return y; }
    static int getNumOfPoints() { return numOfPoint; }
};

class Circle {
private:
    Point center;
    int r;
    static int numOfCircle;
public:
    Circle(int x, int y, int r) : center(x, y), r(r) {
        numOfCircle++;
        cout << "A circle at ("<<x<<", "<<y<<") and radius "<<r<<" is created! Now, we have "<<numOfCircle<<" circles." << endl;
    }
    Circle(Point p, int r) : center(p), r(r) {
        numOfCircle++;
        int x = p.getX(), y = p.getY();
        cout << "A circle at ("<<x<<", "<<y<<") and radius "<<r<<" is created! Now, we have "<<numOfCircle<<" circles." << endl;
    }
    ~Circle() {
        numOfCircle--;
        int x = center.getX(), y = center.getY();
        cout << "A circle at ("<<x<<", "<<y<<") and radius "<<r<<" is erased! Now, we have "<<numOfCircle<<" circles." << endl;
    }
    bool pointInCircle(Point &p) {
        int x0 = center.getX(), y0 = center.getY();
        int x1 = p.getX(), y1 = p.getY();
        int len = (x1 - x0) * (x1 - x0) + (y1 - y0) * (y1 - y0);
        if (len < r * r) return true;
        else return false;
    }
    Point &getCenter() {
        Point &temp = center;
        return temp;
    }
    static int getNumOfCircles() { return numOfCircle; }
};

int Point::numOfPoint = 0;
int Circle::numOfCircle = 0;


int main()
{
    int cases,num;
    int x, y, r, px, py;
    Point aPoint(0,0), *bPoint;
    Circle aCircle(1,1,1);
    cin>>cases;
    cout<<"We have "<<Point::getNumOfPoints()<<" points and "<<Circle::getNumOfCircles()<<" circles now."<<endl;
    for (int i = 0; i < cases; i++)
    {
        cin>>x>>y>>r;
        bPoint = new Point(x,y);
        Circle circle(*bPoint, r);
        cin>>num;
        for (int j = 0; j < num; j++)
        {
            cin>>px>>py;
            if (circle.pointInCircle(*(new Point(px, py))))
            {
                cout<<"("<<px<<", "<<py<<") is in the circle at (";
                cout<<circle.getCenter().getX()<<", "<<circle.getCenter().getY()<<")."<<endl;
            }
            else
            {
                cout<<"("<<px<<", "<<py<<") is not in the circle at (";
                cout<<circle.getCenter().getX()<<", "<<circle.getCenter().getY()<<")."<<endl;
            }
        }
        delete bPoint;
    }
    cout<<"We have "<<Point::getNumOfPoints()<<" points, and "<<Circle::getNumOfCircles()<<" circles."<<endl;
    return 0;
}      

继续阅读