no.223 Rectangle Area
Find the total area covered by and rectilinear rectangles in a 2D plane.
Each rectangle was defined by its bottom left corner and top right corner as shown in the figure.
Assume the total area is never beyond the maximum possible value of int.
Find the area where two rectangles intersect
Vertical rectangles, each of which is represented by a coordinate of two points in the lower left and upper right corners
In fact, the intersection area [B,d] and [f,h] intersect region * [A,c] and [e,g] intersect area (x)
Test instructions Understand the problem: the area of the area represented by the two rectangles, not the intersection!!!
So simple a problem is wrong!!
1#include"stdafx.h"2#include <map>3#include <vector>4#include <iostream>5 using namespacestd;6 7 classSolution8 {9 Public:Ten intComputearea (intAintBintCintDintEintFintGintH) One{//find the area where two rectangles intersect A //vertical rectangles, each of which is represented by a coordinate of two points in the lower left and upper right corners - //In fact, the intersection area [B,d] and [f,h] intersect region * [A,c] and [e,g] intersect area (x) - //Test instructions Understand the problem: the area of the area represented by the two rectangles, not the intersection!!! the intArea = (c-a) * (d-b) + (G-E) * (H-F); - if(F>=d | | B>=h | | E>=c | | A>=G)//do not intersect - returnArea ; - //only considering the intersection, not considering the inclusion of!!! + intLength =min (d,h)-Max (b,f);//The formula is wrong!!! - intwidth =min (c,g)-Max (a,e); + A returnarea-length* width; at } - }; - - intMain () - { - solution Sol; in -cout << Sol.computearea (0,0,0,0,-1,-1,1,1) <<Endl; tocout << Sol.computearea (-3,0,3,4,0,-1,9,2) <<Endl; +cout << Sol.computearea (0,-1,9,2,-3,0,3,4) <<Endl; -cout << Sol.computearea (-2,1,-1,3,0,1,1,3) <<Endl; the *}
no.223 Rectangle Area