Problem Description
Given two rectangles and the coordinates of two points on the diagonals of each rectangle,you have to calculate the area of the intersected part of two rectangles. its sides are parallel to OX and OY .
Input
Input The first line of input is 8 positive numbers which indicate the coordinates of four points that must be on each diagonal.The 8 numbers are x1,y1,x2,y2,x3,y3,x4,y4.That means the two points on the first rectangle are(x1,y1),(x2,y2);the other two points on the second rectangle are (x3,y3),(x4,y4).
Output
Output For each case output the area of their intersected part in a single line.accurate up to 2 decimal places.
#include <iostream>
#include <algorithm>
#include <stdio.h>
using namespace std;
int main()
{
double x[5],y[5],a[5],b[5];
double area;
int i;
while(cin>>x[1]>>y[1]>>x[2]>>y[2]>>x[3]>>y[3]>>x[4]>>y[4]){
for(i=1;i<5;i++){
a[i]=x[i];
b[i]=y[i];
}
sort(x+1,x+5);
sort(y+1,y+5);
if((x[3]>a[2]&&x[3]>a[1])||(x[3]>a[4]&&x[3]>a[3])||(y[3]>b[2]&&y[3]>b[1])||(y[3]>b[4]&&y[3]>b[3]))
area=0.0;
else
area=(y[3]-y[2])*(x[3]-x[2]);
printf("%.2lf\n",area);
}
return 0;
}
【后记】
1.自己分了17种情况还是错了。。
转自https://blog.csdn.net/hpulw/article/details/50783001