Rectangle Area Solutions in C++
Number 223
Difficulty Medium
Acceptance 37.8%
Link LeetCode
Other languages Go
Solutions
C++ solution by haoel/leetcode
// Source : https://leetcode.com/problems/rectangle-area/// Author : Hao Chen// Date : 2015-06-12#include <iostream>using namespace std;namespace leetcode{class Point {public:Point(int _x, int _y):x(_x),y(_y) {}int x, y;};class Rectangle {public:Rectangle(int a, int b, int c, int d):topLeft(a,d), bottomRight(c,b) { }int Area(){return (bottomRight.x - topLeft.x)*(topLeft.y - bottomRight.y);}int InclusiveArea (Rectangle &r){// I include itif (r.topLeft.x >= topLeft.x && r.bottomRight.x <= bottomRight.x &&r.topLeft.y <= topLeft.y && r.bottomRight.y >= bottomRight.y ) {return this->Area();}// it includes meif (r.topLeft.x <= topLeft.x && r.bottomRight.x >= bottomRight.x &&r.topLeft.y >= topLeft.y && r.bottomRight.y <= bottomRight.y ) {return r.Area();}// 0 - no inclusivereturn 0;}int OverlappedArea(Rectangle &r) {int overlap_x = max(0, min(r.bottomRight.x, bottomRight.x) - max(r.topLeft.x, topLeft.x));int overlap_y = max(0, min(r.topLeft.y, topLeft.y) - max(r.bottomRight.y, bottomRight.y));return overlap_x * overlap_y;}Point topLeft;Point bottomRight;};};int computeArea(int A, int B, int C, int D, int E, int F, int G, int H) {using namespace leetcode;Rectangle r1(A,B,C,D);Rectangle r2(E,F,G,H);int area = r1.InclusiveArea(r2);if (area > 0) return area;return r1.Area() + r2.Area() - r1.OverlappedArea(r2);}int main(){//16cout << "16 : " << computeArea(-1, -1, 1, 1, -2, -2, 2, 2) << endl;//16cout << "16 : " << computeArea(-2, -2, 2, 2, -1, -1, 1, 1) << endl;//17cout << "17 : " << computeArea(-2, -2, 2, 2, -4, 3, -3, 4) << endl;//45cout << "45 : " << computeArea(-3, -0, 3, 4, 0, -1, 9, 2) << endl;//24cout << "24 : " << computeArea(-2, -2, 2, 2, -3, -3, 3, -1) << endl;return 0;}