Problem description:
(1) first create a point class, including data members X and Y (coordinate points );
(2) A circle class is derived based on the point class, and a data member (RADIUS) is added. The members of the base class represent the center of the circle;
(3) Compile the constructor, destructor, and necessary operator overload functions in the above two classes (this project is mainly for input and output );
(4) define the int locate function and determine the positional relationship between point P and the circle (Return Value: <0 in the circle, = 0 in the circle,> 0 out of the circle );
Int main () {circle C1 (, 4), C2 (, 5); // C2 should be greater than c1point p1 (), P2 (3,-2 ), p3 (); // It is located in the cout inside, up, and out of C1 <"circle C1:" <C1; cout <"Point P1:" <P1; cout <"Point P1 in the circle C1" <(locate (P1, C1)> 0 )? "External" :( (locate (P1, C1) <0 )? "Inner": "upper") <Endl; cout <"point P2:" <P2; cout <"point P2 in circle C1" <(locate (P2, C1)> 0 )? "External" :( (locate (P2, C1) <0 )? "Inner": "upper") <Endl; cout <"Point P3:" <P3; cout <"Point P3 in the circle C1" <(locate (P3, C1)> 0 )? "External" :( (locate (P3, C1) <0 )? "Inner": "On") <Endl; return 0 ;}
(6) A straight line connected to the center: Given a point of P, the straight line connecting to the center of the circle will have two intersections with the circle ,. Based on the point class and circle class defined above, a scheme is designed to output the coordinates of these two points.
Tip:
Code:
# Include <iostream> # include <cmath> using namespace STD; Class circle; Class Point {protected: Double X, Y; public: Point () {x = 0, y = 0;} Point (double A, double B): X (A), y (B) {} double getx () {return X;} double Gety () {return y;} double distance (const point & C) {double dx = x-c.x; double DY = y-c.y; return SQRT (dx * dx + dy * Dy );} friend istream & operator> (istream & in, point & C); friend ostream & operator <(ostream & ou T, const point & C); friend void fun (circle & C, point & P1) ;}; istream & operator >>( istream & in, point & C) {cout <"Enter the coordinate (x, y) format \ n"; char A, B, E; do {CIN> A> C. x> B> C. y> E; if (a = '(' & B = ',' & E = ') break; cout <"format error. Please enter it again! \ N ";}while (1); Return in;} ostream & operator <(ostream & out, const point & C) {cout <" ("<C. x <"," <C. Y <") \ n"; Return out;} class circle: Public point {PRIVATE: Double R; public: Circle (): R (0) {point ();} circle (double A, double B, double C): Point (a, B), R (c) {} friend istream & operator> (istream & in, circle & C); friend ostream & operator <(ostream & out, const circle & C); friend int locate (point &, circle &); bool Operator> (const circle &); bool operator <(const circle &); bool operator >=( const circle &); bool operator <= (const circle &); bool operator = (const circle &); bool Operator! = (Const circle &); friend void fun (circle & C, point & P1); double area () {return 3.14159 * r ;}}; void fun (circle & C, point & P) {Point P1, P2; p1.x = (C. X + SQRT (C. R * C. r/(1 + (C. y-p.gety ()/(c. x-p.getx () * (C. y-p.gety ()/(c. x-p.getx (); p2.x = (C. x-SQRT (C. R * C. r/(1 + (C. y-p.gety ()/(c. x-p.getx () * (C. y-p.gety ()/(c. x-p.getx (); p1.y = (P. gety () + (p1.x-P. getx () * (C. y-p.gety ()/(c. x-p.getx ())); P2.y = (P. gety () + (p2.x-P. gety () * (C. y-p.gety ()/(c. x-p.getx ())); cout <"point P" <p <"and circle" <C <"intersection" <P1 <"and" <P2 <" \ n ";} bool circle: Operator> (const circle & C) {If (r> C. r) return true; return false;} bool circle: Operator <(const circle & C) {If (r <C. r) return true; return false;} bool circle: Operator >=( const circle & C) {If (r <C. r) return false; return true;} bool circle: Operator <= (const circle & C) {I F (r> C. r) return false; return true;} bool circle: Operator = (const circle & C) {If (r = C. r) return true; return false;} bool circle: Operator! = (Const circle & C) {If (r = C. r) return false; return true;} istream & operator> (istream & in, circle & C) {point D; CIN> D; C. X = D. getx (); C. y = D. gety (); cout <"Enter the radius:"; CIN> C. r; Return in;} ostream & operator <(ostream & out, const circle & C) {cout <"<C. x <"," <C. Y <") radius =" <C. r <'\ 12'; Return out;} int locate (point & T2, circle & T1) {point T = T1; Double X = t1.distance (T2 ); if (ABS (x-t1.r) <1e-7) return 0; else if (x <t1.r) Return-1; else return 1 ;}int main () {circle C1, 4); point P; CIN> P; fun (C1, P); Return 0 ;}
Running result:
WEEK 1 [Project 4-Relationship between points and circles] (3)