Advertisement
Vladislav8653

laba_2_1_c++

Nov 16th, 2022 (edited)
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.07 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3. int main() {
  4.     cout << "The polygon is given by the coordinates of its vertices. Determine if the given point belongs to the area of the polygon.\n";
  5.     cout << "Input number of vertices:\n";
  6.     int n = 0;
  7.     const int min = 3;
  8.     bool isIncorrect;
  9.     do {
  10.         isIncorrect = false;
  11.         cin >> n;
  12.         if (cin.fail() || n < min) {
  13.             cout << "There must be more than 2 vertices (numbers). \n";
  14.             isIncorrect = true;
  15.             cin.clear();
  16.             while (cin.get() != '\n');
  17.         }
  18.     } while (isIncorrect);
  19.     float* arrX = new float[n];
  20.     float* arrY = new float[n];
  21.     int i;
  22.     for (i = 0; i < n; i++) {
  23.         cout << "Enter coordinates for point "<< i + 1 << ":\n";
  24.         do {
  25.             isIncorrect = false;
  26.             cin >> arrX[i];
  27.             if (cin.fail()) {
  28.                 cout << "Please, enter a number: \n";
  29.                 isIncorrect = true;
  30.                 cin.clear();
  31.                 while (cin.get() != '\n');
  32.             }
  33.         } while (isIncorrect);
  34.         do {
  35.             isIncorrect = false;
  36.             cin >> arrY[i];
  37.             if (cin.fail()) {
  38.                 cout << "Please, enter a number: \n";
  39.                 isIncorrect = true;
  40.                 cin.clear();
  41.                 while (cin.get() != '\n');
  42.             }
  43.         } while (isIncorrect);
  44.     }
  45.     cout << "Input point coordinates: \n";
  46.     float x = 0;
  47.     float y = 0;
  48.     do {
  49.         isIncorrect = false;
  50.         cin >> x;
  51.         if (cin.fail()) {
  52.             cout << "Please, enter a number: \n";
  53.             isIncorrect = true;
  54.             cin.clear();
  55.             while (cin.get() != '\n');
  56.         }
  57.     } while (isIncorrect);
  58.     do {
  59.         isIncorrect = false;
  60.         cin >> y;
  61.         if (cin.fail()) {
  62.             cout << "Please, enter a number: \n";
  63.             isIncorrect = true;
  64.             cin.clear();
  65.             while (cin.get() != '\n');
  66.         }
  67.     } while (isIncorrect);
  68.     bool gotInto = false;
  69.     int j = n - 1;
  70.     for (i = 0; i < n; i++) {
  71.         if ((((arrY[i] <= y) && (y <= arrY[j])) || ((arrY[j] <= y) && (y <= arrY[i]))) && (((arrY[j] - arrY[i]) != 0) && (x >= ((arrX[j] - arrX[i]) * (y - arrY[i]) / (arrY[j] - arrY[i]) + arrX[i]))))
  72.             gotInto = !gotInto;
  73.         j++;
  74.         if (j == n)
  75.             j = 0;
  76.     }
  77.     delete[] arrX;
  78.     delete[] arrY;
  79.     if (gotInto) {
  80.         cout << "Point belongs to area!\n";
  81.     }
  82.     else {
  83.         cout << "Point doesn't belong to area.\n";
  84.     }
  85. }
  86.  
  87.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement