Advertisement
Sauka1337

Untitled

Oct 18th, 2023
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.83 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int main() {
  5.  
  6.     int n;
  7.     bool isIncorrect;
  8.  
  9.     do {
  10.         isIncorrect = false;
  11.  
  12.         cout << "Enter the number of sides of the polygon:";
  13.         cin >> n;
  14.  
  15.         if (cin.fail()) {
  16.             cin.clear();
  17.             cout << "Invalid input." << endl;
  18.             isIncorrect = true;
  19.         }
  20.  
  21.         if (!isIncorrect && n > 1000) {
  22.                 cout << "Number of sides is too big. Please, choose different value that is less than 1000." << endl;
  23.                 isIncorrect = true;
  24.             }
  25.  
  26.         if (!isIncorrect && n < 3) {
  27.             cout << "Polygon must have at least 3 sides." << endl;
  28.             isIncorrect = true;
  29.         }
  30.  
  31.         while (cin.get() != '\n');
  32.     } while (isIncorrect);
  33.  
  34.     int* xValues = new int[n];
  35.     int* yValues = new int[n];
  36.  
  37.     for (int i = 0; i < n; i++)
  38.     {
  39.         do {
  40.             isIncorrect = false;
  41.  
  42.             cout << "Enter x value for " << i+1 << " point :" << endl;
  43.             cin >> xValues[i];
  44.  
  45.             cout << "Enter y value for " << i+1 << " point :" << endl;
  46.             cin >> yValues[i];
  47.  
  48.             if (cin.fail()) {
  49.                 cin.clear();
  50.                 cout << "Invalid input!" << endl;
  51.                 isIncorrect = true;
  52.             }
  53.  
  54.             if (!isIncorrect && (xValues[i] < -1000 || xValues[i] > 1000)) {
  55.                 cout << "X value is out of range [-1000; 1000]. Please, choose different value." << endl;
  56.                 isIncorrect = true;
  57.             }
  58.  
  59.             if (!isIncorrect && (yValues[i] < -1000 || yValues[i] > 1000)) {
  60.                 cout << "Y value is out of range [-1000; 1000]. Please, choose different value." << endl;
  61.                 isIncorrect = true;
  62.             }
  63.  
  64.             while (cin.get() != '\n');
  65.         } while (isIncorrect);
  66.     }
  67.  
  68.     bool hasPositive = false;
  69.     bool hasNegative = false;
  70.  
  71.     for (int i = 0; i < n; ++i) {
  72.         double result = (xValues[(i + 1) % n] - xValues[i]) * (yValues[(i + 2) % n] - yValues[(i + 1) % n]) -
  73.                         (yValues[(i + 1) % n] - yValues[i]) * (xValues[(i + 2) % n] - xValues[(i + 1) % n]);
  74.  
  75.         if (result > 0) {
  76.             hasPositive = true;
  77.         } else if (result < 0) {
  78.             hasNegative = true;
  79.         }
  80.  
  81.         // If both positive and negative results are encountered, the polygon is not convex
  82.         // If no positive or no negative results are encountered, the polygon is not convex
  83.         if ((hasPositive && hasNegative) || !(hasPositive && hasNegative)) {
  84.             cout << "The polygon is not convex." << endl;
  85.             return 0;
  86.         }
  87.     }
  88.  
  89.     // If the code reaches this point, the polygon is convex
  90.     cout << "The polygon is convex." << endl;
  91.  
  92.     delete[] xValues;
  93.     delete[] yValues;
  94.  
  95.     return 0;
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement