I have N points in trigonometric order that define a polygon. I've used the following code to calculate the area of that polygon but I get a stack overflow and I can't exactly understand why. At first I thought it was the while function getting out of bounds but apparently is not?
#include<iostream>
#include<fstream>
using namespace std;
struct punct {
long double x;
long double y;
};
int main()
{
int N;
long double aria = 0.0f;
punct v[100000];
ifstream f("aria.in");
ofstream g("aria.out");
f >> N;
for (int i = 1; i <= N; i++) {
f >> v[i].x >> v[i].y;
}
int k = 1;
while (k <= N - 1) {
aria = aria + (v[k].x * v[k + 1].y - v[k + 1].x * v[k].y);
k++;
}
aria = aria + (v[N].x * v[1].y - v[1].x * v[N].y);
aria = 1 / 2 * aria;
g << aria;
f.close();
g.close();
return 0;
}
My input file aria.in
has the following:
4
-2 -2
2 -2
2 2
-2 2