題目鏈接:http://acm.hdu.edu.cn/showproblem.php?pid=2056
Rectangles
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瑰谜、
翻譯如下:
矩形?
問(wèn)題描述?
給定兩個(gè)矩形和每個(gè)矩形對(duì)角線上兩點(diǎn)的坐標(biāo),必須計(jì)算兩個(gè)矩形相交部分的面積帝洪。它的側(cè)面與Ox和Oy平行似舵。
?輸入?
輸入第一行輸入的是8個(gè)正數(shù),表示必須在每個(gè)對(duì)角線上的四個(gè)點(diǎn)的坐標(biāo)葱峡。8個(gè)數(shù)字是x1砚哗、y1、x2砰奕、y2蛛芥、x3、y3军援、x4仅淑、y4。這意味著第一個(gè)矩形上的兩個(gè)點(diǎn)是(x1胸哥、y1)涯竟、(x2、y2)空厌;第二個(gè)矩形上的其他兩個(gè)點(diǎn)是(x3庐船、y3)、(x4嘲更、y4)筐钟。
?產(chǎn)量?
每種情況下的輸出在一行中輸出其相交部分的面積。精確到小數(shù)點(diǎn)后2位赋朦。?
樣本輸入?
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
?樣品輸出?
1.00
56.25
解題思路:
這道題求矩形的公共面積可以分成2個(gè)問(wèn)題
1.是否有公共的面積篓冲?
2.如果有公共面積,那么宠哄,是多少壹将??
先解決第一個(gè)問(wèn)題毛嫉。我們考慮數(shù)軸上的有2個(gè)線段瞭恰,顯然,如果2個(gè)線段要有相交的部分狱庇,那么這個(gè)相交的部分一定是2個(gè)線段左端點(diǎn)的較大值和右端點(diǎn)的較小值所構(gòu)成的惊畏。
也就是說(shuō),【2個(gè)左端點(diǎn)的較大值和2個(gè)右端點(diǎn)的較小值】之間的部分就是我們需要的公共區(qū)間密任。
同時(shí)可以推出颜启,如果左端點(diǎn)的較大值如果本身就比右端點(diǎn)的較小值要大,這2個(gè)線段絕對(duì)沒有公共區(qū)間浪讳。
這個(gè)結(jié)論放在2維的坐標(biāo)系中缰盏,很顯然可以推出只要X坐標(biāo)和Y坐標(biāo)同時(shí)都滿足上面這個(gè)條件!那么就一定會(huì)有公共區(qū)間淹遵。
另外需要注意的是口猜,2個(gè)矩形相交,不意味著有一個(gè)頂點(diǎn)在另外一個(gè)矩形內(nèi)透揣。有反例济炎。
那么我們只要根據(jù)
X軸上的2個(gè)左端點(diǎn)的較大值和2個(gè)右端點(diǎn)的較小值
y軸上的2個(gè)下端點(diǎn)的較大值和2個(gè)上端點(diǎn)的較小值
就能得出答案。
當(dāng)然辐真,如果這道題是100%有相交的前提的話须尚,也可以對(duì)橫縱坐標(biāo)分別排序,取中間的2個(gè)值作差就可以計(jì)算答案侍咱,畫個(gè)圖就明白了耐床。
這道題輸入的2個(gè)對(duì)角點(diǎn)順序他完全是隨機(jī)的
代碼如下:
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>
#include <algorithm>
#include <iostream>
using namespace std;
double x[10],y[10],ans,lx,rx,ly,ry;
int i;
void change(double &xx,double &yy){
double t;
t=xx; xx=yy; yy=t;
}
int main(){
? ? while(scanf("%lf%lf",&x[1],&y[1])!=EOF){
? ? ? for(i=2;i<=4;i++){
? ? ? ? scanf("%lf%lf",&x[i],&y[i]);
? ? ? }
? ? ? if(x[1]>x[2]){change(x[1],x[2]);}
? ? ? if(x[3]>x[4]){change(x[4],x[3]);}
? ? ? if(y[1]>y[2]){change(y[1],y[2]);}
? ? ? if(y[3]>y[4]){change(y[4],y[3]);}? //處理讀入的數(shù)據(jù)。
? ? ? lx=max(x[1],x[3]); rx=min(x[2],x[4]); //計(jì)算X軸上的左端的較大值楔脯,右端的較小值
? ? ? ly=max(y[1],y[3]); ry=min(y[2],y[4]); //計(jì)算y軸上的下端的較大值撩轰,上端的較小值
? ? ? if(lx<=rx&&ly<=ry){? ? ? ? ? ? ? ? ? //公共矩形的左端不能超過(guò)右端,下端不能超上端
? ? ? ? ans=1.0*fabs(rx-lx)*fabs(ry-ly);? //計(jì)算答案
? ? ? ? printf("%.2lf\n",ans);
? ? ? }
? ? ? else{printf("0.00\n"); continue;} //沒有公共區(qū)間昧廷,輸出0.00
? ? }
? ? return 0;
}