Task: A polygon (not necessarily convex) is defined on the plane by listing the coordinates of the vertices in the order of traversing its boundary. Determine the area of ​​the polygon.

Wrote code to calculate the area of ​​a convex polygon (yes, with memory allocated on the heap). How can it be altered to calculate the area of ​​a nonconvex polygon and, in general, an algorithm for determining whether the polygon is convex or not? (If there is no way to break the polygon into shapes, please write how to break it and calculate each shape separately)

#include "stdafx.h" #include <iostream> using namespace System; using namespace std; int main() { setlocale(0, ""); int y, x, c, i = 0; cout << "Кол-во углов "; cin >> c; int(*coor)[2]; coor = new int[c][2]; while (i != c) { for (int j = 0; j < 2; j++) { cout << "Введите координаты " << endl; cout << "По X "; cin >> x; cout << "По Y "; cin >> y; coor[i][j] = x; j = 1; coor[i][j] = y; j = 2; } i++; }; i = 0; int t = i + 1; double sum, pl = 0; for (i; t!= c; i++) { sum = ((coor[i][0] + coor[t][0])*(coor[i][1] - coor[t][1])) / 2; pl = pl + sum; t++; } cout << "Площадь " << abs(pl) << endl; system("pause"); delete[]coor; } 

This is the same program without arrays. Here everything works correctly. Apparently incorrectly integrated here dynamic array ... Help, please

 int n, x, y, x1, x2, y1, y2; double sum = 0; cout << "Введите кол-во углов многоугольника: "; cin >> n; cout << "Введите координаты вершины многоугольника: "; cin >> x >> y; x1 = x; y1 = y; for (int i = 0; i < (n - 1); i++) { cout << "Введите координаты следующей вершины: "; cin >> x2 >> y2; sum = sum + (x1 + x2)*(y2 - y1); x1 = x2; y1 = y2; } sum = sum + (x + x2)*(y - y2); sum = abs(sum) / 2; cout << fixed << setprecision(3) << sum << endl; 
  • What is using namespace System; ??? - AnT
  • Console Application Template - llatibro
  • namespace System is an element of the C ++ / CLI language / platform. What does this issue C + + / CLI? Which language do you want to use: C ++ or C ++ / CLI? - AnT
  • Well, essentially, I'm creating a CLR console application. I think it does not play a special role ... - llatibro
  • You have nothing in the code from the CLI. And you put the [c++] tag to your question, and not [c++-cli] . Therefore, I see no point in this using namespace System; which only litters the code. - AnT

2 answers 2

Your code has some completely meaningless formula, which has nothing to do with any area even for convex polygons.

Your formula as if remotely resembles the formula of "lacing" to calculate the area of ​​the polygon. The lacing formula works great for any simple polygon. There is no requirement for a polygon to be convex. This is the formula you need.

That is: implement the correct formula. And remember that all edges of a polygon participate in it, including the edge from the last vertex to the first.


Your "pseudo-cycle" for (int j = 0; j < 2; j++) when entering coordinates is generally amazing with its surrealism. Why did you write the cycle if you were not going to do any cycle there?


Your code "without arrays" is a variant of the correct implementation of the lacing formula.

  • To allocate memory in a heap for a tick (as it was required in the rules for solving this problem) - llatibro
  • @llatibro: I do not understand. What question do you answer? - AnT
  • About the pseudo-cycle - llatibro
  • And for convex polygons, this formula works fine - llatibro
  • @llatibro: Your pseudo-cycle on j has nothing to do with dynamic memory. And once again: your pseudo-cycle on j is not a cycle at all. He does only one "iteration". Why then the cycle at all? - AnT

Decided as follows, everything works correctly, even with non-convex triangles. Thanks for the comments, AnT.

 #include "stdafx.h" #include <iostream> #include <iomanip> #include <cmath> using namespace std; int main() { setlocale(0, ""); int c, i = 0; cout << "Кол-во углов "; cin >> c; int(*coor)[2]; coor = new int[c][2]; while (i != c) { cout << "Введите координаты " << endl; cout << "По X "; cin >> coor[i][0]; cout << "По Y "; cin >> coor[i][1]; i++; }; double sum = 0; i = 0; int t = 1; while (t != c) { sum = sum + (coor[i][0] + coor[t][0])*(coor[t][1] - coor[i][1]); t++; i++; } t = c - 1; i = 0; sum = sum + (coor[i][0] + coor[t][0])*(coor[i][1] - coor[t][1]); cout << "Площадь " << fixed << setprecision(3) << abs(sum) / 2 << endl; system("pause"); delete[]coor; }