Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- int main() {
- int n;
- bool isIncorrect;
- do {
- isIncorrect = false;
- cout << "Enter the number of sides of the polygon:";
- cin >> n;
- if (cin.fail()) {
- cin.clear();
- cout << "Invalid input." << endl;
- isIncorrect = true;
- }
- if (!isIncorrect && n > 1000) {
- cout << "Number of sides is too big. Please, choose different value that is less than 1000." << endl;
- isIncorrect = true;
- }
- if (!isIncorrect && n < 3) {
- cout << "Polygon must have at least 3 sides." << endl;
- isIncorrect = true;
- }
- while (cin.get() != '\n');
- } while (isIncorrect);
- int* xValues = new int[n];
- int* yValues = new int[n];
- for (int i = 0; i < n; i++)
- {
- do {
- isIncorrect = false;
- cout << "Enter x value for " << i+1 << " point :" << endl;
- cin >> xValues[i];
- cout << "Enter y value for " << i+1 << " point :" << endl;
- cin >> yValues[i];
- if (cin.fail()) {
- cin.clear();
- cout << "Invalid input!" << endl;
- isIncorrect = true;
- }
- if (!isIncorrect && (xValues[i] < -1000 || xValues[i] > 1000)) {
- cout << "X value is out of range [-1000; 1000]. Please, choose different value." << endl;
- isIncorrect = true;
- }
- if (!isIncorrect && (yValues[i] < -1000 || yValues[i] > 1000)) {
- cout << "Y value is out of range [-1000; 1000]. Please, choose different value." << endl;
- isIncorrect = true;
- }
- while (cin.get() != '\n');
- } while (isIncorrect);
- }
- bool hasPositive = false;
- bool hasNegative = false;
- for (int i = 0; i < n; ++i) {
- double result = (xValues[(i + 1) % n] - xValues[i]) * (yValues[(i + 2) % n] - yValues[(i + 1) % n]) -
- (yValues[(i + 1) % n] - yValues[i]) * (xValues[(i + 2) % n] - xValues[(i + 1) % n]);
- if (result > 0) {
- hasPositive = true;
- } else if (result < 0) {
- hasNegative = true;
- }
- // If both positive and negative results are encountered, the polygon is not convex
- // If no positive or no negative results are encountered, the polygon is not convex
- if ((hasPositive && hasNegative) || !(hasPositive && hasNegative)) {
- cout << "The polygon is not convex." << endl;
- return 0;
- }
- }
- // If the code reaches this point, the polygon is convex
- cout << "The polygon is convex." << endl;
- delete[] xValues;
- delete[] yValues;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement