{"id":22988,"date":"2021-09-29T20:14:46","date_gmt":"2021-09-29T14:44:46","guid":{"rendered":"https:\/\/python-programs.com\/?p=22988"},"modified":"2021-11-22T18:35:34","modified_gmt":"2021-11-22T13:05:34","slug":"python-program-to-find-coordinates-of-rectangle-with-given-points-lie-inside","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-find-coordinates-of-rectangle-with-given-points-lie-inside\/","title":{"rendered":"Python Program to Find Coordinates of Rectangle with Given Points Lie Inside"},"content":{"rendered":"
In the previous article, we have discussed Python Program to Find Number of Rectangles in N*M Grid<\/a> 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.<\/p>\n Examples:<\/strong><\/p>\n Example1:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Example2:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Below are the ways to Find the smallest rectangle in which all of the points from the given input are enclosed in Python:<\/p>\n 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],[\u00a0Xmax, Ymin].<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Remediate your knowledge gap by attempting the Python Code Examples<\/a> regularly and understand the areas of need and work on them.<\/p>\n 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 …<\/p>\n
\nGiven Two lists X and Y where X[i] and Y[i] represent the coordinate system’s points.<\/p>\nGiven X Coordinates List =
[<\/code>
4<\/code>
, <\/code>
3<\/code>
, <\/code>
6<\/code>
, <\/code>
1<\/code>
, <\/code>
-<\/code>
1<\/code>
, <\/code>
12<\/code>
]\u00a0<\/code> Given Y Coordinates List =
[<\/code>
4<\/code>
, <\/code>
1<\/code>
, <\/code>
10<\/code>
, <\/code>
3<\/code>
, <\/code>
7<\/code>
, <\/code>
-<\/code>
1<\/code>
] <\/code><\/pre>\n
The first point of Rectangle is [ -1 , -1 ]\r\nThe second point of Rectangle is [ -1 , 10 ]\r\nThe third point of Rectangle is [ 12 , 10 ]\r\nThe fourth point of Rectangle is [ 12 , -1 ]<\/pre>\n
Given X Coordinates List = 4 8 7 1 6 4 2\r\nGiven Y Coordinates List = 5 7 9 8 3 4 2<\/pre>\n
The first point of Rectangle is [ 1 , 2 ]\r\nThe second point of Rectangle is [ 1 , 9 ]\r\nThe third point of Rectangle is [ 8 , 9 ]\r\nThe fourth point of Rectangle is [ 8 , 2 ]<\/pre>\n
Program to Find Coordinates of Rectangle with Given Points Lie Inside in Python<\/h2>\n
\n
Method #1: Using Mathematical Approach(Static Input)<\/h3>\n
\n
#Give the X coordinates of all the points as a list as static input \r\n#and store it in a variable.\r\nXCordinates=[4, 3, 6, 1, -1, 12] \r\n#Give the Y coordinates of all the points as another list as static input \r\n#and store it in another variable.\r\nYCordinates = [4, 1, 10, 3, 7, -1] \r\n\r\n#Calculate the minimum value of all the x Coordinates using the min() function\r\n#and store it in a variable xMinimum.\r\nxMinimum=min(XCordinates)\r\n#Calculate the maximum value of all the x Coordinates using the max() function \r\n#and store it in a variable xMaximum.\r\nxMaximum=max(XCordinates)\r\n#Calculate the minimum value of all the y Coordinates using the min() function\r\n#and store it in a variable yMinimum.\r\nyMinimum=min(YCordinates)\r\n#Calculate the maximum value of all the y Coordinates using the max() function\r\n#and store it in a variable yMaximum.\r\nyMaximum=max(YCordinates)\r\n#Print the Four Coordinates of the Rectangle using the above 4 calculated values.\r\n#Print the first point of the rectangle by printing values xMinimum, yMinimum.\r\nprint('The first point of Rectangle is [',xMinimum,',',yMinimum,']')\r\n#Print the second point of the rectangle by printing values xMinimum, yMaximum.\r\nprint('The second point of Rectangle is [',xMinimum,',',yMaximum,']')\r\n#Print the third point of the rectangle by printing values xMaximum, yMaximum.\r\nprint('The third point of Rectangle is [',xMaximum,',',yMaximum,']')\r\n#Print the fourth point of the rectangle by printing values xMaximum, yMinimum.\r\nprint('The fourth point of Rectangle is [',xMaximum,',',yMinimum,']')<\/pre>\n
The first point of Rectangle is [ -1 , -1 ]\r\nThe second point of Rectangle is [ -1 , 10 ]\r\nThe third point of Rectangle is [ 12 , 10 ]\r\nThe fourth point of Rectangle is [ 12 , -1 ]<\/pre>\n
Method #2: Using Mathematical Approach (User Input)<\/h3>\n
\n
# Give the X coordinates of all the points as a list as user input using list(),int(),split(),map() functions\r\n# and store it in a variable.\r\nXCordinates = list(map(int, input('Enter some random X Coordinates = ').split()))\r\n# Give the Y coordinates of all the points as another list as user input using list(),int(),split(),map() functions\r\n# and store it in another variable.\r\nYCordinates = list(map(int, input('Enter some random Y Coordinates = ').split()))\r\n\r\n# Calculate the minimum value of all the x Coordinates using the min() function\r\n# and store it in a variable xMinimum.\r\nxMinimum = min(XCordinates)\r\n# Calculate the maximum value of all the x Coordinates using the max() function\r\n# and store it in a variable xMaximum.\r\nxMaximum = max(XCordinates)\r\n# Calculate the minimum value of all the y Coordinates using the min() function\r\n# and store it in a variable yMinimum.\r\nyMinimum = min(YCordinates)\r\n# Calculate the maximum value of all the y Coordinates using the max() function\r\n# and store it in a variable yMaximum.\r\nyMaximum = max(YCordinates)\r\n# Print the Four Coordinates of the Rectangle using the above 4 calculated values.\r\n# Print the first point of the rectangle by printing values xMinimum, yMinimum.\r\nprint('The first point of Rectangle is [', xMinimum, ',', yMinimum, ']')\r\n# Print the second point of the rectangle by printing values xMinimum, yMaximum.\r\nprint('The second point of Rectangle is [', xMinimum, ',', yMaximum, ']')\r\n# Print the third point of the rectangle by printing values xMaximum, yMaximum.\r\nprint('The third point of Rectangle is [', xMaximum, ',', yMaximum, ']')\r\n# Print the fourth point of the rectangle by printing values xMaximum, yMinimum.\r\nprint('The fourth point of Rectangle is [', xMaximum, ',', yMinimum, ']')\r\n<\/pre>\n
Enter some random X Coordinates = 4 8 7 1 6 4 2\r\nEnter some random Y Coordinates = 5 7 9 8 3 4 2\r\nThe first point of Rectangle is [ 1 , 2 ]\r\nThe second point of Rectangle is [ 1 , 9 ]\r\nThe third point of Rectangle is [ 8 , 9 ]\r\nThe fourth point of Rectangle is [ 8 , 2 ]<\/pre>\n
\n