Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Define a struct Rect as follows: typedef struct Point { double x; double y; } Po

ID: 3881106 • Letter: D

Question

Define a struct Rect as follows:

typedef struct Point {

double x;

double y;

} PointT;

typedef struct Rect {

PointT location;

char color;

double w;//width;

double h; //height;

} RectT;

Write a program that declares RectT a, b, *recs; and asks user to enter location.x, location.y, w, h for both a and b.

Write a function int chk_overlap(RectT *r1, RectT *r2) which can be called to check if a and b overlap or not. If so return 1, else return 0. Note you need to pass the addresses of a an b to that function!

Dynamically create an array of 50 rectangles (an array of RectT type data) and save the address of that array to recs. Then randomly initialize location.x, location.y, w, h of each rectangle (e.g., recs[i].location.x = rand()%20; etc...)

Count/print how many of these rectangles overlap with a alone, b alone, and both a and b. So you need to keep three counters.

Explanation / Answer

#include<iostream>
using namespace std;
typedef struct Point {
double x;
double y;
} PointT;
typedef struct Rect {
PointT location;
char color;
double w;//width; in x axis
double h; //height; in y axis
} RectT;
int chk_overlap(RectT *r1, RectT *r2)
{
   int x=r1->location.x+r1->w;//x--axis last point of r1
   int y=r1->location.y+r1->h;//y--axis last point of r1
   if((r2->location.x >= r1->location.x && r2->location.x<=x))
   {
               if((r2->location.y >= r1->location.y && r2->location.y<=y))
               {
                   return 1;//if overlapping  
               }
   }
  
   int x2=r2->location.x+r2->w;//x--axis last point of r2
   int y2=r2->location.y+r2->h;//y--axis last point of r2
   if((r1->location.x >= r2->location.x && r1->location.x<=x2))
   {
               if((r1->location.y >= r2->location.y && r1->location.y<=y2))
               {
                   return 1;//if overlapping  
               }
   }
  
  
  
   return 0;// if not over lapping..
}

int main()
{//a
   RectT a, b, *recs;//declaration
   //reading input from keyboard to RectT a
   cout<<"a:Enter location x:";
   cin>>a.location.x;//readin to a.location.x;
   cout<<"a:Enter location y:";
   cin>>a.location.y;//readin to a.location.y;
   cout<<"a:Enter width :";
   cin>>a.w;//readin to a.w;
   cout<<"a:Enter height :";
   cin>>a.h;//readin to a.h;
  
   cout<<"b:Enter location x:";
   cin>>b.location.x;//readin to b.location.x;
   cout<<"b:Enter location y:";
   cin>>b.location.y;//readin to b.location.y;
   cout<<"b:Enter width :";
   cin>>b.w;//readin to b.w;
   cout<<"b:Enter height :";
   cin>>b.h;//readin to b.h;
  
   //printing the details...
   cout<<"Rectangle a: ";
   cout<<"location x:"<<a.location.x<<", y:"<<a.location.y<<endl;
   cout<<"width :"<<a.w<<", height:"<<a.h<<endl;
  
   cout<<"Rectangle b: ";
   cout<<"location x:"<<b.location.x<<", y:"<<b.location.y<<endl;
   cout<<"width :"<<b.w<<", height:"<<b.h<<endl;
  
   //checking overlapped or not..
  
   if(chk_overlap(&a,&b))
   {
   cout<<"Over lapping ";  
   }
   else
   cout<<"Not over lapping ";
  
  
  
  
  
     
   return 0;
}

output:

a:Enter location x:1
a:Enter location y:1
a:Enter width :4
a:Enter height :4
b:Enter location x:2
b:Enter location y:2
b:Enter width :3
b:Enter height :3
Rectangle a:
location x:1, y:1
width :4, height:4
Rectangle b:
location x:2, y:2
width :3, height:3
Over lapping


Process exited normally.
Press any key to continue . . .

a:Enter location x:1
a:Enter location y:1
a:Enter width :4
a:Enter height :4
b:Enter location x:10
b:Enter location y:10
b:Enter width :3
b:Enter height :3
Rectangle a:
location x:1, y:1
width :4, height:4
Rectangle b:
location x:10, y:10
width :3, height:3
Not over lapping


Process exited normally.
Press any key to continue . . .

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote