// A1Q4.CPP #include #include #include using namespace std; class Point { float X, Y; public: void setPoint(float x, float y); float getX(); float getY(); }; class Circle { float A, B, R; public: void setCircle(float a, float b, float r); void positionPointCircle(Circle c, Point p); void position2Circles(Circle c1,Circle c2); float getA(); float getB(); float getR(); }; void Circle::setCircle(float a, float b, float r) { A=a;B=b;R=r; } void Circle::positionPointCircle(Circle c, Point p) { float temp1 = (float)(pow(c.getA()-p.getX(),2) + pow(c.getB() - p.getY(),2)); float temp2 = (float)pow(c.getR(),2); if (temp1 < temp2) // compare the distance between the centre and the point cout << "The point is inside the circle."; else if (temp1 == temp2) cout << "The point is on the circle."; else if (temp1 > temp2) cout << "The point is outside the circle."; else cout << "Error."; cout << endl; }; float Point::getX() { return X; }; float Point::getY() { return Y; }; float Circle::getA() { return A; }; float Circle::getB() { return B; }; float Circle::getR() { return R; }; void position2Circles(Circle c1,Circle c2) { float temp1 = sqrt(pow(c1.getA()-c2.getA(),2) + pow(c1.getB()- c2.getB(), 2)); float temp2 = c1.getR() + c2.getR(); if (temp1 == temp2) // comepare the distance between two centres and the ridius cout << "One circle is (outside) tangent to the other."; else if (temp1 < temp2) if (temp1 == abs(c1.getR() - c2.getR())) cout << "One circle is (inside) tangent to the other."; else cout << "Two circles are cross each other."; else if (temp1 > temp2) cout << "There is no cross between two circle."; else cout << "Error"; cout << endl; }; void Point::setPoint(float x, float y) { X=x;Y=y; }; int main() { float a,b,r; float x,y; short i; cout << "1. Given a Point and a circle, check to see if a Point is inside the circle, on the circle or outside the circle.\n"; cout << "2. Given 2 circles, check to see if one circle is inside the other or tangent to it.\n"; cout << "Please choose the function (1 or 2):"; cin >> i; if (i==1) { Circle circle1; Point Point1; cout << "Please input the centre and ridius of the circle (a, b, r): "; cin >> a >> b >> r; circle1.setCircle(a,b,r); cout << "Please input the Point (x, y): "; cin >> x >> y; Point1.setPoint(x,y); circle1.positionPointCircle(circle1,Point1); } else if (i==2) { Circle circle1,circle2; cout << "Please input the centre and ridius of the 1st circle (a, b, r): "; cin >> a >> b >> r; circle1.setCircle(a,b,r); cout << "Please input the centre and ridius of the 2nd circle (a, b, r): "; cin >> a >> b >> r; circle2.setCircle(a,b,r); position2Circles(circle1,circle2); } return 0; }