hdoj Rectangles 2056 (数学几何&技巧)求两矩形相交面积

Rectangles

Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 19901    Accepted Submission(s): 6444


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.
 

Sample Input
   
   
   
   
1.00 1.00 3.00 3.00 2.00 2.00 4.00 4.00 5.00 5.00 13.00 13.00 4.00 4.00 12.50 12.50
 

Sample Output
   
   
   
   
1.00 56.25
//题意:
给你四个坐标(x1,y1),(x2,y2),(x3,y3),(x4,y4);前两个坐标为第一个矩形的对角线上的两个坐标,后两个坐标是第二个矩形的对角线上的两个坐标,现在想让你求出它们重叠的那个小矩形的面积。
//思路:没什么好说的,比较简单,就是一个转换。。。看代码就知道了
但是要注意的是,如果不相交输出0.00,不要输出0,因为题中要求输出两位小数。。。(在这块WA了3次,好伤心)。
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<algorithm>
#include<iostream>
#define INF 0x3f3f3f3f
#define ull unsigned long long
#define ll long long
#define IN __int64
#define N 10010
#define M 1000000007
using namespace std;
struct zz
{
	double x;
	double y;
}p[5];
bool cmpx(zz a,zz b)
{
	return a.x<b.x;
}
bool cmpy(zz a,zz b)
{
	return a.y<b.y;
}
int main()
{
	while(scanf("%lf%lf%lf%lf%lf%lf%lf%lf",&p[1].x,&p[1].y,&p[2].x,&p[2].y,&p[3].x,&p[3].y,&p[4].x,&p[4].y)!=EOF)
	{
		double x1,y1,x2,y2,x,y,l,h,ans;
		x1=fabs(p[1].x-p[2].x);y1=fabs(p[1].y-p[2].y);
		x2=fabs(p[3].x-p[4].x);y2=fabs(p[3].y-p[4].y);
		sort(p+1,p+5,cmpx);x=fabs(p[4].x-p[1].x);
		sort(p+1,p+5,cmpy);y=fabs(p[4].y-p[1].y);
		l=x1+x2-x;h=y1+y2-y;
		if(l<=0||h<=0)
			printf("0.00\n");
		else
		{
			ans=l*h;
			printf("%.2lf\n",ans);
		}
	}
	return 0;
}

你可能感兴趣的:(hdoj Rectangles 2056 (数学几何&技巧)求两矩形相交面积)