no.223 Rectangle Area

Source: Internet
Author: User

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&GT;=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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.