📜  用给定的坐标集找到矩形的最小面积

📅  最后修改于: 2021-04-22 06:39:05             🧑  作者: Mango

给定一个数组arr  XY平面中的点集。任务是找到可以从这些点形成的矩形的最小面积。矩形的边应与X和Y轴平行。如果不能用给定的点形成矩形,则打印0
例子:

方法:将分数分组X  坐标,以便将垂直直线上的点组合在一起。然后,对于组中的每对点,例如坐标(X,Y1)和(X,Y2),我们检查最小的矩形,并将这对点作为要形成的矩形的最右边。我们可以通过跟踪之前访问过的所有其他点对来实现。最后返回获得的矩形的最小可能面积。
下面是上述方法的实现:

CPP
// C++ Implementation of above approach
#include 
using namespace std;
 
// function to find minimum area of Rectangle
int minAreaRect(vector> A){
 
    // creating empty columns
    map> columns;
 
    // fill columns with coordinates
    for(auto i:A)
        columns[i[0]].push_back(i[1]);
 
    map,int > lastx;
 
    int ans = INT_MAX;
 
    for (auto x:columns)
    {
        vector 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> A = {{1, 1}, {1, 3}, {3, 1}, {3, 3}, {2, 2}};
    cout << (minAreaRect(A));
    return 0;
}
 
// This code is contributed by mohit kumar 29


Java
/*package whatever //do not write package name here */
// Java Implementation of above approach
import java.io.*;
import java.util.*;
 
class GFG {
 
  //# function to find minimum area of Rectangle
  public static int minAreaRect(int[][] points)
  {
 
    // creating empty columns
    @SuppressWarnings("unchecked")
    Set columns = new HashSet();
 
    // fill columns with coordinates
    for (int[] point : points)
      columns.add(40001 * point[0] + point[1]);
 
    int ans = Integer.MAX_VALUE;
    for (int i = 0; i < points.length; ++i)
      for (int j = i + 1; j < points.length; ++j) {
        if (points[i][0] != points[j][0]
            && points[i][1] != points[j][1]) {
          if (columns.contains(40001
                               * points[i][0]
                               + points[j][1])
              && columns.contains(
                40001 * points[j][0]
                + points[i][1])) {
            ans = Math.min( ans, Math.abs(points[j][0]
                            - points[i][0])
              * Math.abs(points[j][1]
                         - points[i][1]));
          }
        }
      }
 
    return ans < Integer.MAX_VALUE ? ans : 0;
  }
   
  // Driver code
  public static void main(String[] args)
  {
 
    int[][] A = {{1, 1}, {1, 3}, {3, 1}, {3, 3}, {2, 2}};
    System.out.println(minAreaRect(A));
  }
}
 
// This code is contributed by maheshwaripiyush9


Python
# Python Implementation of above approach
import collections
 
# function to find minimum area of Rectangle
def minAreaRect(A):
 
    # creating empty columns
    columns = collections.defaultdict(list)
 
    # fill columns with coordinates
    for x, y in A:
        columns[x].append(y)
 
    lastx = {}
    ans = float('inf')
 
    for x in sorted(columns):
        column = columns[x]
        column.sort()
        for j, y2 in enumerate(column):
            for i in range(j):
                y1 = column[i]
 
                # check if rectangle can be formed
                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
 
# Driver code
A = [[1, 1], [1, 3], [3, 1], [3, 3], [2, 2]]
print(minAreaRect(A))


输出:
4