On XY plane, you are given upper left corner coordinates of two equal sized rect
ID: 3702721 • Letter: O
Question
On XY plane, you are given upper left corner coordinates of two equal sized rectangles. Find the intersection percentage of the rectangles.
Question: Write a program that is going to read two rectangle information and find the intersection percentage.
Input specification
You will be first given the width (w) and height (h) of the rectangles. Then upper left coordinates (x, y) of the two rectangles are given in the following two lines where 0 <= (x, y) <= 10000 and 1 <= (w, h) <= 100.
Output specification
Show a floating point number with 3 decimal places that is the intersection percentage to the second rectangle.
Input
4 3
1 4
3 5
Output
33.333
Explanation / Answer
#include <bits/stdc++.h>
using namespace std;
double calculate(double x1,double y1,double x2,double y2,double w,double h)
{
if(x1>x2)
return calculate(x2,y2,x1,y1,w,h);
if(x2>=x1+w)
return 0.0;
if(y1<y2)
if(y2>=y1+h)
return 0.0;
else
return (x1+w-x2)*(y1-(y2-h))*100.0/(w*h);
else
if(y1-h>=y2)
return 0.0;
else
return (x1+w-x2)*(y2-(y1-h))*100.0/(w*h);
}
int main()
{
double w,h,x1,y1,x2,y2;
cin>>w>>h;
cin>>x1>>y1>>x2>>y2;
cout<<fixed;
cout<<setprecision(3);
double ans=calculate(x1,y1,x2,y2,w,h);
cout<<ans;
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.