Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- int main() {
- cout << "The polygon is given by the coordinates of its vertices. Determine if the given point belongs to the area of the polygon.\n";
- cout << "Input number of vertices:\n";
- int n = 0;
- const int min = 3;
- bool isIncorrect;
- do {
- isIncorrect = false;
- cin >> n;
- if (cin.fail() || n < min) {
- cout << "There must be more than 2 vertices (numbers). \n";
- isIncorrect = true;
- cin.clear();
- while (cin.get() != '\n');
- }
- } while (isIncorrect);
- float* arrX = new float[n];
- float* arrY = new float[n];
- int i;
- for (i = 0; i < n; i++) {
- cout << "Enter coordinates for point "<< i + 1 << ":\n";
- do {
- isIncorrect = false;
- cin >> arrX[i];
- if (cin.fail()) {
- cout << "Please, enter a number: \n";
- isIncorrect = true;
- cin.clear();
- while (cin.get() != '\n');
- }
- } while (isIncorrect);
- do {
- isIncorrect = false;
- cin >> arrY[i];
- if (cin.fail()) {
- cout << "Please, enter a number: \n";
- isIncorrect = true;
- cin.clear();
- while (cin.get() != '\n');
- }
- } while (isIncorrect);
- }
- cout << "Input point coordinates: \n";
- float x = 0;
- float y = 0;
- do {
- isIncorrect = false;
- cin >> x;
- if (cin.fail()) {
- cout << "Please, enter a number: \n";
- isIncorrect = true;
- cin.clear();
- while (cin.get() != '\n');
- }
- } while (isIncorrect);
- do {
- isIncorrect = false;
- cin >> y;
- if (cin.fail()) {
- cout << "Please, enter a number: \n";
- isIncorrect = true;
- cin.clear();
- while (cin.get() != '\n');
- }
- } while (isIncorrect);
- bool gotInto = false;
- int j = n - 1;
- for (i = 0; i < n; i++) {
- 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]))))
- gotInto = !gotInto;
- j++;
- if (j == n)
- j = 0;
- }
- delete[] arrX;
- delete[] arrY;
- if (gotInto) {
- cout << "Point belongs to area!\n";
- }
- else {
- cout << "Point doesn't belong to area.\n";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement