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

Wrote code to calculate the area of a convex polygon (yes, with heap allocation). How can it be remade for calculating the area of a non-convex polygon and, in general, an algorithm for determining whether the introduced polygon is convex or not?

(If there is no way without breaking the polygon into figures, then please write how to break it and calculate each figure separately)

```
# include "stdafx.h"
#include & lt; iostream & gt;
using namespace System;
using namespace std;
int main ()
{
setlocale (0, "");
int y, x, c, i = 0;
cout & lt; & lt; "Number of corners";
cin & gt; & gt; c;
int (* coor) [2];
coor = new int [c] [2];
while (i! = c)
{
for (int j = 0; j & lt; 2; j ++)
{
cout & lt; & lt; "Enter coordinates" & lt; & lt; endl;
cout & lt; & lt; "By X";
cin & gt; & gt; x;
cout & lt; & lt; "By Y";
cin & gt; & gt; 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 & lt; & lt; "Area" & lt; & lt; abs (pl) & lt; & lt; endl;
system ("pause");
delete [] coor;
}
```

This is the same program without arrays. Everything works right here. Apparently I did not correctly integrate a dynamic array here … Help, please

```
int n, x, y, x1, x2, y1, y2;
double sum = 0;
cout & lt; & lt; "Enter the number of corners of the polygon:";
cin & gt; & gt; n;
cout & lt; & lt; "Enter the coordinates of the vertex of the polygon:";
cin & gt; & gt; x & gt; & gt; y;
x1 = x;
y1 = y;
for (int i = 0; i & lt; (n - 1); i ++)
{
cout & lt; & lt; "Enter the coordinates of the next vertex:";
cin & gt; & gt; x2 & gt; & gt; y2;
sum = sum + (x1 + x2) * (y2 - y1);
x1 = x2;
y1 = y2;
}
sum = sum + (x + x2) * (y - y2);
sum = abs (sum) / 2;
cout & lt; & lt; fixed & lt; & lt; setprecision (3) & lt; & lt; sum & lt; & lt; endl;
```

## Answer 1, authority 100%

You have implemented some completely meaningless formula in your code, which has nothing to do with any area, even for convex polygons.

Your formula seems to vaguely resemble ” lacing “formula to calculate the area of a polygon. The lacing formula works great for any simple polygon. This formula has no requirement that the polygon be convex. This is the formula you need.

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

Your “pseudo-cycle” `for (int j = 0; j & lt; 2; j ++) `

when entering coordinates is generally striking in its surrealism. Why did you write a *loop *if you weren’t going to do any loop there?

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

## Answer 2

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

```
# include "stdafx.h"
#include & lt; iostream & gt;
#include & lt; iomanip & gt;
#include & lt; cmath & gt;
using namespace std;
int main ()
{
setlocale (0, "");
int c, i = 0;
cout & lt; & lt; "Number of corners";
cin & gt; & gt; c;
int (* coor) [2];
coor = new int [c] [2];
while (i! = c)
{
cout & lt; & lt; "Enter coordinates" & lt; & lt; endl;
cout & lt; & lt; "By X";
cin & gt; & gt; coor [i] [0];
COUT & LT; & LT; "By y";
CIN & GT; & GT; 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 & LT; & LT; "Square" & lt; & lt; Fixed & lt; & lt; SetPrecision (3) & lt; & lt; ABS (SUM) / 2 & lt; & lt; Endl;
SYSTEM ("PAUSE");
Delete [] Coor;
}
```