Answer to Question #188317 in C++ for Drake

Question #188317
Question 1: Overlapping rectangles – A rectangle is identified by its four coordinates, as shown in the
figure below. Make a structure to hold data for every rectangle, there is bare minimum information that
you to store for each rectangle i.e. its coordinates. You can also improvise and make additional members
in the structure if you think it will help in doing any of the following tasks. Write the following functions:
1. Generate n number of rectangles
2. Find and return the area of all the rectangles, given the rectangles in the above format
3. Sort and return all rectangles in the ascending order based on their area.
4. Find the set of rectangles that overlap the largest rectangle
5. Write a function main (as a controller) to call the above functions in the correct order to identify
the set of rectangles that overlap the largest rectangle
1
Expert's answer
2021-05-03T01:27:21-0400


#include<iostream>
#include<cmath>
using namespace std;


class Point{
   private:
       int x;
       int y;
   public:

       Point(){
           x=0;
           y=0;
       }

       Point(int _x,int _y){
           x=_x;
           y=_y;
       }

       void setX(int _x){
           x=_x;
       }
       int getX(){
           return x;
       }

       void setY(int _y){
           y=_y;
       }
       int getY(){
           return y;
       }

       void print(){
           cout<<"("<<x<<","<<y<<")";
       }
};

class Rectangle{
   private:
       Point *point1;
       Point *point2;
       Point *point3;
       Point *point4;
   public:

       Rectangle(){
           point1=NULL;
           point2=NULL;
           point3=NULL;
           point4=NULL;
       }


       void setPoint1(Point *p){
           point1=p;
       }

       void setPoint2(Point *p){
           point2=p;
       }

       void setPoint3(Point *p){
           point3=p;
       }

       void setPoint4(Point *p){
           point4=p;
       }



       Point* getPoint1(){
           return(point1);
       }

       Point* getPoint2(){
           return(point2);
       }

       Point* getPoint3(){
           return(point3);
       }

       Point* getPoint4(){
           return(point4);
       }

       double area(){
           double w=sqrt(pow(point1->getX()-point2->getX(),2)+pow(point1->getY()-point2->getY(),2));
           double l=sqrt(pow(point1->getX()-point4->getX(),2)+pow(point1->getY()-point4->getY(),2));

           return (w*l);
       }

       void print(){
           point1->print();
           cout<<" , ";
           point2->print();
           cout<<" , ";
           point3->print();
           cout<<" , ";
           point4->print();
           cout<<"\tArea is "<<area()<<endl;
       }
};


void generateRect(Rectangle *rect,int n){
   for(int i=0;i<n;i++){
       cout<<"Enter top point of rectangle "<<(i+1)<<" : ";
       int x;
       cin>>x;
       cout<<"Enter left point of rectangle "<<(i+1)<<" : ";
       int y;
       cin>>y;
       cout<<"Enter width of rectangle "<<(i+1)<<" : ";
       int w;
       cin>>w;
       cout<<"Enter length of rectangle "<<(i+1)<<" : ";
       int l;
       cin>>l;

       Point *point1=new Point(x,y);
       Point *point2=new Point(x+w,y);
       Point *point3=new Point(x+w,y+l);
       Point *point4=new Point(x,y+l);

       rect[i].setPoint1(point1);
       rect[i].setPoint2(point2);
       rect[i].setPoint3(point3);
       rect[i].setPoint4(point4);

       cout<<"Rectangle "<<(i+1)<<" created\n\n";
   }
}

void printAreaOfRectangle(Rectangle *rect,int n){
   for(int i=0;i<n;i++){
       cout<<"Area of Rectangle "<<(i+1)<<" is "<<rect[i].area()<<endl;
   }
}

void printRectangles(Rectangle *rect,int n){
   for(int i=0;i<n;i++){
       rect[i].print();
   }
}

void sort(Rectangle *rect,int n){
   for (int i = 0; i < n-1; i++) {
       for (int j = 0; j < n-i-1; j++) {
           if (rect[j].area() > rect[j+1].area()) {
               Rectangle tmp=rect[j];
               rect[j]=rect[j+1];
               rect[j+1]=tmp;
           }
       }
   }
}


   bool isOverlap(Rectangle rect1,Rectangle rect2){
       Point *l1=rect1.getPoint4();
       Point *r1=rect1.getPoint2();
       Point *l2=rect2.getPoint4();
       Point *r2=rect2.getPoint2();

       if (l1->getX() == r1->getX() || l1->getY() == r2->getY() || l2->getX() == r2->getX() || l2->getY() == r2->getY()) {
       return false;
       }


       if (l1->getX() >= r2->getX() || l2->getX() >= r1->getX()){
       return false;
           }

       if (l1->getY() <= r2->getY() || l2->getY() <= r1->getY()){
           return false;
           }

       return true;
       }


void calcOverlaps(Rectangle *rect,int *lst,int n){
   for(int i=0;i<n;i++){
       for(int j=0;j<n;j++){
           if(i!=j){
               if(isOverlap(rect[i],rect[j])==true){
                   lst[i]++;
               }
           }
       }
   }
}

int main(){
   int n;
   cout<<"Enter the value of N : ";
   cin>>n;


   Rectangle *rect=new Rectangle[n];
   int *overlaps=new int[n];


   for(int i=0;i<n;i++){
       overlaps[i]=0;
   }

   generateRect(rect,n);

   cout<<"Display rectangles---\n";
   printRectangles(rect,n);

   sort(rect,n);

   cout<<"Display rectangles after sorting ---\n";
   printRectangles(rect,n);


   calcOverlaps(rect,overlaps,n);


   for(int i=0;i<n;i++){
       cout<<"Number of overlaps of rectangle "<<(i+1)<<" is "<<overlaps[i]<<endl;
   }

   return 0;
}


Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS