You are on page 1of 10

1: //Name: Adityendra Tiwari

2: //Roll No.: 7504


3: //Date: 15-11-2021
4:
5: //POINTS IN THE PLANE
6:
7: #include <iostream>
8: #include <cmath>
9: #include <iomanip>
10: using namespace std;
11:
12: class Point {
13:
14: private:
15: double x; //only methods of class can access these directly
16: double y;
17:
18: public:
19: Point(); //constructors
20: Point(double xx, double yy); //enables us to declare points as P(2,4);
21: double getX() const; //to access the private data
22: double getY() const;
23: void setX(double xx); //for setting a value in x-coordinate
24: void setY(double yy);
25: double getR() const; //for polar coordinates
26: void setR(double r);
27: double getA() const;
28: void setA(double theta);
29: void rotate(double theta);
30: bool operator==(const Point& Q) const;
31: bool operator!=(const Point& Q) const;
32: };
33:
34: double dist( Point P, Point Q); //distance measure
35: Point midpoint(Point P,Point Q); //center of two points
36: ostream& operator<<(ostream& os, const Point& P);
37:
38: Point::Point(){ //default value
39: x=y=0.;
40: }
41:
42: Point::Point(double xx, double yy){ //point declaration
43: x=xx;
44: y=yy;
45: }
46:
47: double Point::getX() const{ //accessing the x-coordinate
48: return x;
49: }
50:
51: double Point::getY() const{ //accessing the y-coordinate
52: return y;
53: }
54:
55: void Point::setX(double xx){ //setting up the x-coordinate
56: x=xx;
57: }
58:
59: void Point::setY(double yy){ //setting up the y-coordinate
60: y=yy;
61: }
62:
63: double Point::getR() const{ //to calc. distance from origin
64: return sqrt(x*x+y*y);
65: }
66:
67: void Point::setR(double r){
68: //if the point is at origin set point (r,0);
69: if(x==0 && y==0){
70: x=r;
71: return;
72: }
73: //otherwise set point as (rcosA, rsinA);
74: double A=getA();
75: x=r*cos(A);
76: y=r*sin(A);
77: }
78:
79: double Point::getA() const{ //to calc. angle
80: if(x==0 && y==0) return 0.;
81:
82: double A=atan2(y,x);
83: if(A<0) A+= 2*M_PI;
84: return A;
85: }
86:
87: void Point::setA(double theta){ //setting up polar coordinate
88: double r=getR();
89: x=r*cos(theta);
90: y=r*sin(theta);
91: }
92:
93: void Point::rotate(double theta){ //rotating
94: double A=getA();
95: A+=theta;
96: setA(A);
97: }
98:
99: bool Point::operator==(const Point& Q) const{ //comparison
100: return ((x==Q.x) && (y==Q.y));
101: }
102:
103: bool Point::operator!=(const Point& Q) const{ //comparison
104: return ((x!=Q.x) || (y!=Q.y));
105: }
106: double dist(Point P, Point Q){ //distance measure
107: double dx=P.getX()-Q.getX();
108: double dy=P.getY()-Q.getY();
109: return sqrt(dx*dx + dy*dy);
110: }
111:
112: Point midpoint(Point P,Point Q){ //midpoint measure
113: double xx=(P.getX()+Q.getX())/2;
114: double yy=(P.getY()+Q.getY())/2;
115: return Point(xx,yy);
116: }
117:
118: //setting up the output(representation)
119: ostream& operator<<(ostream& os, const Point& P){
120: os << "(" << P.getX() <<","<< P.getY() <<")";
121: return os;
122: }
123:
124: int main()
125: {
126: double a,b,c,d;
127: cout<< "Enter the co-ordinates of the first point: ";
128: cin>>a;
129: cin>>b;
130: cout<< "Enter the co-ordinates of the second point: ";
131: cin>>c;
132: cin>>d;
133:
134: cout<<setprecision(2);
135: Point P(a,b);
136: Point Q(c,d);
137:
138: cout<<"\nGiven the points "<<P<< " and "<<Q;
139: if(P==Q) cout<< ": They are equal."<<endl;
140: if(P!=Q) cout<< ": They are not equal."<<endl;
141:
142: cout<<"The distance between the points "<<P<<" and "<<Q<<" is: "
143: <<dist(P,Q)<<endl;
144: cout<<"Midpoint of "<<P<<" and "<<Q<< " is: "<<midpoint(P,Q)<<endl;
145: Point R1(P.getR(),P.getA());
146: Point R2(Q.getR(),Q.getA());
147: cout<<"In polar co-ordinates "<<P<< " is "<<R1<<endl;
148: cout<<"In polar co-ordinates "<<Q<< " is "<<R2<<endl;
149:
150: Q.setR(5);
151: cout<<"The point Q when rescaled to 5 becomes "<< Q<<endl;
152: cout<<fixed<<"The point "<<P<< " when rotated by an angle of"
153: " 90 degrees anticlockwise becomes ";
154: P.rotate(M_PI/2);
155: cout<< P;
156: }
OUTPUT 1:
OUTPUT 2:
OUTPUT 3:

You might also like