You are given a rectangle in a plane. The corner coordinates of this rectangle is provided to you. You have to print the amount of area of the plane covered by this rectangles.
The end coordinates are provided as four integral values: x1, y1, x2, y2. It is given that x1 < x2 and y1 < y2.
Answers
Answer:
// C++ Implementation of above approach
#include <bits/stdc++.h>
using namespace std;
// function to find minimum area of Rectangle
int minAreaRect(vector<vector<int>> A){
// creating empty columns
map<int,vector<int>> columns;
// fill columns with coordinates
for(auto i:A)
columns[i[0]].push_back(i[1]);
map<pair<int,int>,int > lastx;
int ans = INT_MAX;
for (auto x:columns)
{
vector<int> column = x.second;
sort(column.begin(), column.end());
for (int j = 0; j < column.size(); j++)
{
for (int i = 0; i < j; i++)
{
int y1 = column[i];
// check if rectangle can be formed
if (lastx.find({y1, column[j]}) != lastx.end())
{
ans = min(ans, (x.first - lastx[{y1, column[j]}]) *
(column[j] - column[i]));
}
lastx[{y1, column[j]}] = x.first;
}
}
}
if (ans < INT_MAX)
return ans;
else
return 0;
}
// Driver code
int main()
{
vector<vector<int>> A = {{1, 1}, {1, 3}, {3, 1}, {3, 3}, {2, 2}};
cout << (minAreaRect(A));
return 0;
}
// This code is contributed by mohit kumar 29