
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Rectangle Area in C++
Suppose we want to find the total area covered by two rectilinear rectangles in a 2D plane. Here each rectangle is defined by its bottom left corner and top right corner as shown in the figure.
To solve this, we will follow these steps −
if C = E or A >= G or B >= H or D <= F, then
return (C – A) * (D – B) + (G – E) * (H – F)
Define an array h, insert A, C, E, G into h
Define an array v, insert B, D, F, H into v
sort h array and sort v array
temp := (h[2] – h[1]) * (v[2] – v[1])
total := temp
total := total + (C – A) * (D – B)
total := total + (G – E) * (H – F)
return total
Example(C++)
Let us see the following implementation to get better understanding −
#include <bits/stdc++.h> using namespace std; class Solution { public: int computeArea(int A, int B, int C, int D, int E, int F, int G, int H) { if(C <= E || A >= G || B >= H || D <= F) return (C - A) * (D - B) + (G - E) * (H - F); vector <int> h; h.push_back(A); h.push_back(C); h.push_back(E); h.push_back(G); vector <int> v; v.push_back(B); v.push_back(D); v.push_back(F); v.push_back(H); sort(h.begin(), h.end()); sort(v.begin(), v.end()); long long int temp = (h[2] - h[1]) * (v[2] - v[1]); long long int total = - temp; total += (C - A) * (D - B); total += (G - E) * (H - F); return total; } }; main(){ Solution ob; cout << (ob.computeArea(-3, 0, 3, 4, 0, -1, 9, 2)); }
Input
-3 0 3 4 0 -1 9 2
Output
45
- Related Articles
- Rectangle Area II in C++
- C Program for Area And Perimeter Of Rectangle
- Maximum area of rectangle possible with given perimeter in C++
- Program to find largest rectangle area under histogram in python
- Area of Largest rectangle that can be inscribed in an Ellipse?
- Maximum area rectangle by picking four sides from array in C++
- Java program to find the area of a rectangle
- Swift Program to find the area of the rectangle
- Golang program to find the area of a rectangle
- Haskell Program to Find the Area of a Rectangle
- JavaScript program to find Area and Perimeter of Rectangle
- If the length of a rectangle is tripled and its breadth is doubled, what will happen to the area of this rectangle? Find the ratio of the area of the resultant to the area of the original rectangle.
- Find minimum area of rectangle with given set of coordinates in C++
- The length of a rectangle is 50 cm if the perimeter of the rectangle is 150 cm. Find the area of the rectangle.
- Area of a circle inscribed in a rectangle which is inscribed in a semicircle?

Advertisements