Advertisement
MadCortez

Untitled

Oct 11th, 2020
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.48 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. void start();
  4. void print(bool flag);
  5. int inputValue(int min, int max);
  6. int** userInputArray(int n);
  7. bool checkPolygon(int** coords, int n);
  8. int userInput();
  9.  
  10. void start() {
  11.     int n = userInput();
  12.     int **coords = userInputArray(n);
  13.     bool isPolygon = checkPolygon(coords, n);
  14.     print(isPolygon);
  15. }
  16.  
  17. void print(bool flag) {
  18.     if (flag)
  19.         std::cout << "Введённый многоугольник выпуклый";
  20.     else
  21.         std::cout << "Введённый многоугольник не выпуклый";
  22. }
  23.  
  24. int inputValue(int min, int max) {
  25.     int currentValue;
  26.     bool isNotValid = true;
  27.     do {
  28.         std::cin >> currentValue;
  29.         if (currentValue >= min && currentValue <= max)
  30.             isNotValid = false;
  31.         else
  32.             std::cout << "Введите число в заданном диапазоне\n";
  33.     } while (isNotValid);
  34.     return currentValue;
  35. }
  36.  
  37. int** userInputArray(int n) {
  38.     const int MIN_VALUE = -500;
  39.     const int MAX_VALUE = 500;
  40.     int** coords = new int* [2];
  41.     coords[0] = new int[n];
  42.     coords[1] = new int[n];
  43.     std::cout << "Введите координаты вершин в порядке обхода в диапазоне " << MIN_VALUE << ".." << MAX_VALUE << ": \n";
  44.     for (int i = 0; i < n; i++) {
  45.         std::cout << "Введите координаты " << i + 1 << "-й вершины: ";
  46.         coords[0][i] = inputValue(MIN_VALUE, MAX_VALUE);
  47.         coords[1][i] = inputValue(MIN_VALUE, MAX_VALUE);
  48.     }
  49.     return coords;
  50. }
  51.  
  52. bool checkPolygon(int** coords, int n) {
  53.     int i = 0;
  54.     bool flag = true;
  55.     n--;
  56.     do {
  57.         int j = (i + 1) % n;
  58.         int k = (i + 2) % n;
  59.         int ans = (coords[0][j] - coords[0][i]) * (coords[1][k] - coords[1][j]) - (coords[1][j] - coords[1][i]) * (coords[0][k] - coords[0][j]);
  60.         if (ans < 0)
  61.             flag = false;
  62.         i++;
  63.     } while (flag && i <= n);    
  64.     return flag;
  65. }
  66.  
  67. int userInput() {
  68.     int n;
  69.     const int MIN_SIZE = 3;
  70.     const int MAX_SIZE = 20;
  71.     std::cout << "Данная программа определяет, является ли данный многоугольник выпуклым\n";
  72.     std::cout << "Введите количество вершин в диапазоне " << MIN_SIZE << ".." << MAX_SIZE << ": ";
  73.     n = inputValue(MIN_SIZE, MAX_SIZE);
  74.     return n;
  75. }
  76.  
  77. int main()
  78. {
  79.     setlocale(LC_ALL, "Russian");
  80.     start();
  81. }
  82.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement