- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Find minimum area of rectangle with given set of coordinates in C++
Suppose we have an array of some points in XY plane. We have to find the minimum area of rectangle that can be formed from these points. The side of the rectangle should be parallel to the X and Y axes. If we cannot form the rectangle, then return 0. So if the array of points is like [(1, 1), (1, 3), (3, 1), (3, 3), (2, 2)]. The output will be 4. As the rectangle can be formed using the points (1, 1), (1, 3), (3, 1) and (3, 3).
To solve this, give the points by x coordinates, so that points on straight vertical lines are grouped together. Then for each pair of points in the group like (x, y1) and (x, y2) we will find the smallest rectangle with this pair of points, as the right most edge of the rectangle to be formed. We can do this by keeping track of all other pairs of points, we have visited before. Finally return the minimum possible area of the rectangle obtained.
Example
import collections def findMinArea(Arr): columns = collections.defaultdict(list) for x, y in Arr: columns[x].append(y) lastx = {} ans = float('inf') for x in sorted(columns): col = columns[x] col.sort() for j, y2 in enumerate(col): for i in range(j): y1 = col[i] if (y1, y2) in lastx: ans = min(ans, (x - lastx[y1, y2]) * (y2 - y1)) lastx[y1, y2] = x if ans < float('inf'): return ans else: return 0 A = [[1, 1], [1, 3], [3, 1], [3, 3], [2, 2]] print('Minimum area of rectangle:',findMinArea(A))
Output
Minimum area of rectangle: 4
- Related Articles
- Coordinates of rectangle with given points lie inside in C++
- Maximum area of rectangle possible with given perimeter in C++
- Minimum height of a triangle with given base and area in C++
- Possible number of Rectangle and Squares with the given set of elements in C++
- C++ Program to get minimum perimeter of rectangle whose area is n
- How To Find Minimum Height of the Triangle with Given Base and Area in Java?
- Set the coordinates of the area in an image map in HTML?
- Find coordinates of the triangle given midpoint of each side in C++
- Minimum number using set bits of a given number in C++
- Rectangle Area in C++
- Find minimum possible size of array with given rules for removing elements in C++
- How to set the minimum allowed scale value of Rectangle using FabricJS?
- Rectangle Area II in C++
- C Program for Area And Perimeter Of Rectangle
- Java program to find the area of a rectangle
