In the previous article, we have discussed Python Program to Find Number of Rectangles in N*M Grid

Given Two lists X and Y where X[i] and Y[i] represent the coordinate system’s points.

The task is to Find the smallest rectangle in which all of the points from the given input are enclosed, and the sides of the rectangle must be parallel to the Coordinate axis. Print the obtained rectangle’s four coordinates.

**Examples:**

**Example1:**

**Input:**

Given X Coordinates List =`[`

`4`

`,`

`3`

`,`

`6`

`,`

`1`

`,`

`-`

`1`

`,`

`12`

`]Â`

Given Y Coordinates List =`[`

`4`

`,`

`1`

`,`

`10`

`,`

`3`

`,`

`7`

`,`

`-`

`1`

`]`

**Output:**

The first point of Rectangle is [ -1 , -1 ] The second point of Rectangle is [ -1 , 10 ] The third point of Rectangle is [ 12 , 10 ] The fourth point of Rectangle is [ 12 , -1 ]

**Example2:**

**Input:**

Given X Coordinates List = 4 8 7 1 6 4 2 Given Y Coordinates List = 5 7 9 8 3 4 2

**Output:**

The first point of Rectangle is [ 1 , 2 ] The second point of Rectangle is [ 1 , 9 ] The third point of Rectangle is [ 8 , 9 ] The fourth point of Rectangle is [ 8 , 2 ]

## Program to Find Coordinates of Rectangle with Given Points Lie Inside in Python

Below are the ways to Find the smallest rectangle in which all of the points from the given input are enclosed in Python:

The logic behind this is very simple and efficient: find the smallest and largest x and y coordinates among all given points, and then all possible four combinations of these values result in the four points of the required rectangle as **[Xmin, Ymin], [Xmin, Ymax], [Xmax, Ymax],[Â Xmax, Ymin].**

### Method #1: Using Mathematical Approach(Static Input)

**Approach:**

- Give the X coordinates of all the points as a list as static input and store it in a variable.
- Give the Y coordinates of all the points as another list as static input and store it in another variable.
- Calculate the minimum value of all the x Coordinates using the min() function and store it in a variable xMinimum.
- Calculate the maximum value of all the x Coordinates using the max() function and store it in a variable xMaximum.
- Calculate the minimum value of all the y Coordinates using the min() function and store it in a variable yMinimum.
- Calculate the maximum value of all the y Coordinates using the max() function and store it in a variable yMaximum.
- Print the Four Coordinates of the Rectangle using the above 4 calculated values.
- Print the first point of the rectangle by printing values xMinimum, yMinimum.
- Print the second point of the rectangle by printing values xMinimum, yMaximum.
- Print the third point of the rectangle by printing values xMaximum, yMaximum.
- Print the fourth point of the rectangle by printing values xMaximum, yMinimum.
- The Exit of the Program.

**Below is the implementation:**

#Give the X coordinates of all the points as a list as static input #and store it in a variable. XCordinates=[4, 3, 6, 1, -1, 12] #Give the Y coordinates of all the points as another list as static input #and store it in another variable. YCordinates = [4, 1, 10, 3, 7, -1] #Calculate the minimum value of all the x Coordinates using the min() function #and store it in a variable xMinimum. xMinimum=min(XCordinates) #Calculate the maximum value of all the x Coordinates using the max() function #and store it in a variable xMaximum. xMaximum=max(XCordinates) #Calculate the minimum value of all the y Coordinates using the min() function #and store it in a variable yMinimum. yMinimum=min(YCordinates) #Calculate the maximum value of all the y Coordinates using the max() function #and store it in a variable yMaximum. yMaximum=max(YCordinates) #Print the Four Coordinates of the Rectangle using the above 4 calculated values. #Print the first point of the rectangle by printing values xMinimum, yMinimum. print('The first point of Rectangle is [',xMinimum,',',yMinimum,']') #Print the second point of the rectangle by printing values xMinimum, yMaximum. print('The second point of Rectangle is [',xMinimum,',',yMaximum,']') #Print the third point of the rectangle by printing values xMaximum, yMaximum. print('The third point of Rectangle is [',xMaximum,',',yMaximum,']') #Print the fourth point of the rectangle by printing values xMaximum, yMinimum. print('The fourth point of Rectangle is [',xMaximum,',',yMinimum,']')

**Output:**

The first point of Rectangle is [ -1 , -1 ] The second point of Rectangle is [ -1 , 10 ] The third point of Rectangle is [ 12 , 10 ] The fourth point of Rectangle is [ 12 , -1 ]

### Method #2: Using Mathematical Approach (User Input)

**Approach:**

- Give the X coordinates of all the points as a list as user input using list(),int(),split(),map() functions and store it in a variable.
- Give the Y coordinates of all the points as another list as user input using list(),int(),split(),map() functions and store it in another variable.
- Calculate the minimum value of all the x Coordinates using the min() function and store it in a variable xMinimum.
- Calculate the maximum value of all the x Coordinates using the max() function and store it in a variable xMaximum.
- Calculate the minimum value of all the y Coordinates using the min() function and store it in a variable yMinimum.
- Calculate the maximum value of all the y Coordinates using the max() function and store it in a variable yMaximum.
- Print the Four Coordinates of the Rectangle using the above 4 calculated values.
- Print the first point of the rectangle by printing values xMinimum, yMinimum.
- Print the second point of the rectangle by printing values xMinimum, yMaximum.
- Print the third point of the rectangle by printing values xMaximum, yMaximum.
- Print the fourth point of the rectangle by printing values xMaximum, yMinimum.
- The Exit of the Program.

**Below is the implementation:**

# Give the X coordinates of all the points as a list as user input using list(),int(),split(),map() functions # and store it in a variable. XCordinates = list(map(int, input('Enter some random X Coordinates = ').split())) # Give the Y coordinates of all the points as another list as user input using list(),int(),split(),map() functions # and store it in another variable. YCordinates = list(map(int, input('Enter some random Y Coordinates = ').split())) # Calculate the minimum value of all the x Coordinates using the min() function # and store it in a variable xMinimum. xMinimum = min(XCordinates) # Calculate the maximum value of all the x Coordinates using the max() function # and store it in a variable xMaximum. xMaximum = max(XCordinates) # Calculate the minimum value of all the y Coordinates using the min() function # and store it in a variable yMinimum. yMinimum = min(YCordinates) # Calculate the maximum value of all the y Coordinates using the max() function # and store it in a variable yMaximum. yMaximum = max(YCordinates) # Print the Four Coordinates of the Rectangle using the above 4 calculated values. # Print the first point of the rectangle by printing values xMinimum, yMinimum. print('The first point of Rectangle is [', xMinimum, ',', yMinimum, ']') # Print the second point of the rectangle by printing values xMinimum, yMaximum. print('The second point of Rectangle is [', xMinimum, ',', yMaximum, ']') # Print the third point of the rectangle by printing values xMaximum, yMaximum. print('The third point of Rectangle is [', xMaximum, ',', yMaximum, ']') # Print the fourth point of the rectangle by printing values xMaximum, yMinimum. print('The fourth point of Rectangle is [', xMaximum, ',', yMinimum, ']')

**Output:**

Enter some random X Coordinates = 4 8 7 1 6 4 2 Enter some random Y Coordinates = 5 7 9 8 3 4 2 The first point of Rectangle is [ 1 , 2 ] The second point of Rectangle is [ 1 , 9 ] The third point of Rectangle is [ 8 , 9 ] The fourth point of Rectangle is [ 8 , 2 ]

Remediate your knowledge gap by attempting the Python Code Examples regularly and understand the areas of need and work on them.