2.1 - Design a class point that has x and y coordinates as data members:
Include the following member functions:
Constructor with arguments
Constructor without arguments (Default constructor)
Scale function: void scale(double a)
This function is supposed to scale the coordinates of the point by a, i.e., multiply each by the
real number a.
Add function: void add(point q)
If p and q are of type point, the function p.add (q) should modify p in such a way that:
p.x is assigned p.x + q.x, and p.y is assigned p.y + q.y.
2.2 Based on the class point, design a class rectangle that defines a rectangle as follows:
In this problem, by a rectangle we mean a rectangle whose edges are either vertical or horizontal
(i.e., we do not deal with rotated rectangles). Thus to specify a rectangle we only need to specify
its lower left corner point and its upper right corner point.
Include the member functions:
Constructor with arguments which allows the user to initialize a rectangle by specifying its 2 defining corner points.
Constructor without arguments (Default constructor)
Area member function : double area()
Scale member function: void scale(double a)
This function is supposed to scale each of the corner points of the rectangle by a.
Translate member function: void translate(point q)
This function is supposed to translate the rectangle by the vector q, i.e., add q to both corner points.
containsPoint member function: bool containsPoint(point p)
This function is supposed to check if a given point p is inside the rectangle.
The pointSetIntersect function:
void pointSetIntersect(point A[], int n, point B[], int &m)
This function is supposed to store in the array B all the points in A[0 . . . n − 1] which are
inside the rectangle. It is supposed also to set m to the number of points in A[0 . . . n − 1]
which are inside the rectangle.
Write a program to test your classes and functions
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
|
#include <iostream>
#include <cmath>
using namespace std;
class point
{
friend class rectangle;
private:
double x,y;
public:
point()
{x=y=0;}
point(double a, double b)
{
x=a;
y=b;
}
void scale(double a)
{
x=x*a;
y=y*a;
}
void add(point q)
{
x= x+ q.x;
y= y+ q.y;
}
double getx()
{
return x;
}
double gety()
{
return y;
}
};
class rectangle
{
private:
point p1,p2;
public:
rectangle (point c1, point c2)
{
p1=c1;
p2=c2;
}
rectangle ()
{
point p;
p1=p;
p2=p;
}
double area()
{
double H=fabs(p1.y-p2.y);
double W=fabs(p1.x-p2.x);
return (H*W);
}
void scale (double a)
{
p1.scale(a);
p2.scale(a);
}
void translate (point q)
{
p1.add(q);
p2.add(q);
}
bool containsPoint(point p)
{
if( (p.x > p1.x && p.x < p2.x) && (p.y < p1.y && p.y > p2.y))
return true;
else
return false;
}
void pointSetIntersect (point * A, int n, point * B)
{
int K=0;
int m=0;
for(int i=0; i<n; i++)
{
if(containsPoint (A[i]))
{ B[K]=A[i];
K++;
m++;
}
}
}
}; //end of class rectangle.
void main()
{
point p(5,6);
p.scale(5);
cout<<"P is now: "<< p.getx()<<" "<< p.gety()<<endl;
p.add(p);
cout<<"P is now: "<< p.getx()<<" "<< p.gety()<<endl;
rectangle r;
cout<<"Area is: "<<r.area ()<<endl;
r.scale(6);
r.translate(p);
r.containsPoint (p);
point A,B;
r.pointSetIntersect(&A,7,&B);
}
|
Why the area always is giving me 0 can somebody correct my error