Coordinates of rectangle with given points lie inside
Given two arrays X[] and Y[] with n-elements, where (Xi, Yi) represent a point on coordinate system, find the smallest rectangle such that all points from given input lie inside that rectangle and sides of rectangle must be parallel to Coordinate axis. Print all four coordinates of obtained rectangle.
Note : n >= 4 (we have at least 4 points as input).
Examples :
Input : X[] = {1, 3, 0, 4}, y[] = {2, 1, 0, 2}
Output : {0, 0}, {0, 2}, {4, 2}, {4, 0}
Input : X[] = {3, 6, 1, 9, 13, 0, 4}, Y[] = {4, 2, 6, 5, 2, 3, 1}
Output : {0, 1}, {0, 6}, {13, 6}, {13, 1}
For finding the smallest rectangle you may take two of basic approach :
- Consider origin of coordinate plane as smallest rectangle and then step by step keep expanding it as per value of coordinates of points if they don’t lie inside the current rectangle. This concept requires a little of complex logic to find the exact smallest rectangle.
- A very simple and efficient logic behind this is to find the smallest as well as largest x & y coordinates among all given points and then the all possible four combinations of these values result the four points of required rectangle as {Xmin, Ymin}, {Xmin, Ymax}, {Xmax, Ymax}, {Xmax, Ymin}.
C++
// Program to find smallest rectangle // to conquer all points #include <bits/stdc++.h> using namespace std; // function to print coordinate of smallest rectangle void printRect(int X[], int Y[], int n) { // find Xmax and Xmin int Xmax = *max_element(X, X + n); int Xmin = *min_element(X, X + n); // find Ymax and Ymin int Ymax = *max_element(Y, Y + n); int Ymin = *min_element(Y, Y + n); // print all four coordinates cout << "{" << Xmin << ", " << Ymin << "}" << endl; cout << "{" << Xmin << ", " << Ymax << "}" << endl; cout << "{" << Xmax << ", " << Ymax << "}" << endl; cout << "{" << Xmax << ", " << Ymin << "}" << endl; } // driver program int main() { int X[] = { 4, 3, 6, 1, -1, 12 }; int Y[] = { 4, 1, 10, 3, 7, -1 }; int n = sizeof(X) / sizeof(X[0]); printRect(X, Y, n); return 0; } |
Java
// Program to find smallest rectangle // to conquer all points import java.util.Arrays; import java.util.Collections; class GFG { // function to print coordinate of smallest rectangle static void printRect(Integer X[], Integer Y[], int n) { // find Xmax and Xmin int Xmax = Collections.max(Arrays.asList(X)); int Xmin = Collections.min(Arrays.asList(X)); // find Ymax and Ymin int Ymax = Collections.max(Arrays.asList(Y)); int Ymin = Collections.min(Arrays.asList(Y)); // print all four coordinates System.out.println("{" + Xmin + ", " + Ymin + "}"); System.out.println("{" + Xmin + ", " + Ymax + "}"); System.out.println("{" + Xmax + ", " + Ymax + "}"); System.out.println("{" + Xmax + ", " + Ymin + "}"); } //Driver code public static void main (String[] args) { Integer X[] = { 4, 3, 6, 1, -1, 12 }; Integer Y[] = { 4, 1, 10, 3, 7, -1 }; int n = X.length; printRect(X, Y, n); } } // This code is contributed by Anant Agarwal. |
Python 3
# Program to find smallest rectangle # to conquer all points # function to print coordinate of smallest rectangle def printRect(X, Y, n): # find Xmax and Xmin Xmax = max(X) Xmin = min(X) # find Ymax and Ymin Ymax = max(Y) Ymin = min(Y) # print all four coordinates print("{",Xmin,", ",Ymin,"}",sep="" ) print("{",Xmin,", ",Ymax,"}",sep="" ) print("{",Xmax,", ",Ymax,"}",sep="" ) print("{",Xmax,", ",Ymin,"}",sep="" ) # driver program X = [4, 3, 6, 1, -1, 12] Y = [4, 1, 10, 3, 7, -1] n = len(X) printRect(X, Y, n) # This code is contributed by # Smitha Dinesh Semwal |
C#
// Program to find smallest rectangle // to conquer all points using System.Linq; using System; public class GFG{ // function to print coordinate // of smallest rectangle static void printRect(int[] X, int[] Y, int n) { // find Xmax and Xmin int Xmax = X.Max(); int Xmin = X.Min(); // find Ymax and Ymin int Ymax = Y.Max(); int Ymin = Y.Min(); // print all four coordinates Console.WriteLine("{" + Xmin + ", " + Ymin + "}"); Console.WriteLine("{" + Xmin + ", " + Ymax + "}"); Console.WriteLine("{" + Xmax + ", " + Ymax + "}"); Console.WriteLine("{" + Xmax + ", " + Ymin + "}"); } // Driver code static public void Main () { int[] X = { 4, 3, 6, 1, -1, 12 }; int[] Y = { 4, 1, 10, 3, 7, -1 }; int n = X.Length; printRect(X, Y, n); } } // This code is contributed by Ajit. |
PHP
<?php // PHP Program to find smallest // rectangle to conquer all points // function to print coordinate // of smallest rectangle function printRect($X, $Y, $n) { // find Xmax and Xmin $Xmax = max($X); $Xmin = min($X); // find Ymax and Ymin $Ymax = max($Y); $Ymin = min($Y); // print all four coordinates echo "{" , $Xmin , ", " , $Ymin , "}","\n"; echo "{" , $Xmin , ", " , $Ymax , "}" ,"\n"; echo "{" , $Xmax , ", " , $Ymax , "}","\n"; echo "{" , $Xmax , ", " , $Ymin , "}" ; } // Driver Code $X = array(4, 3, 6, 1, -1, 12); $Y = array(4, 1, 10, 3, 7, -1); $n =count($X); printRect($X, $Y, $n); // This code is contributed by anuj_67. ?> |
{-1, -1}
{-1, 10}
{12, 10}
{12, -1}
Recommended Posts:
- Find minimum area of rectangle with given set of coordinates
- Find whether only two parallel lines contain all coordinates points or not
- Sum of Area of all possible square inside a rectangle
- Check whether a given point lies inside a rectangle or not
- Check if a point lies on or inside a rectangle | Set-2
- Check whether a given point lies on or inside the rectangle | Set 3
- Count the number of rhombi possible inside a rectangle of given size
- Count Integral points inside a Triangle
- Queries on count of points lie inside a circle
- Find Corners of Rectangle using mid points
- Find the number of rectangles of size 2*1 which can be placed inside a rectangle of size n*m
- Largest subset of rectangles such that no rectangle fit in any other rectangle
- Minimum number of points to be removed to get remaining points on one side of axis
- Count of obtuse angles in a circle with 'k' equidistant points between 2 given points
- Ways to choose three points with distance between the most distant points <= L
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



