Coordinates of rectangle with given points lie inside in C++

C++Server Side ProgrammingProgramming

In this tutorial, we will be discussing a program to find the coordinates of rectangle

with given points lying inside.

For this we will be provided with some coordinate points. Our task is to find the smallest rectangle such that all the points lie inside it and it should have its sides parallel to the coordinate axis.

Example

 Live Demo

#include <bits/stdc++.h>
using namespace std;
//calculating the coordinates of smallest rectangle
void print_rectangle(int X[], int Y[], int n){
   //finding minimum and maximum points
   int Xmax = *max_element(X, X + n);
   int Xmin = *min_element(X, X + n);
   int Ymax = *max_element(Y, Y + n);
   int Ymin = *min_element(Y, Y + n);
   cout << "{" << Xmin << ", " << Ymin << "}" << endl;
   cout << "{" << Xmin << ", " << Ymax << "}" << endl;
   cout << "{" << Xmax << ", " << Ymax << "}" << endl;
   cout << "{" << Xmax << ", " << Ymin << "}" << endl;
}
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]);
   print_rectangle(X, Y, n);
   return 0;
}

Output

{-1, -1}
{-1, 10}
{12, 10}
{12, -1}
raja
Published on 29-Jan-2020 07:30:11
Advertisements