用 C++ 語言確定給定點位於內部的矩形的座標
在本文中,我們將討論一個程式,以便找到矩形的座標
且給定點位於內部。
為此,我們將提供一些座標點。我們的任務是找到最小的矩形,使所有點位於其內部,並且其邊平行於座標軸。
示例
#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; }
輸出
{-1, -1} {-1, 10} {12, 10} {12, -1}
廣告