Could someone please explain to me what is going on in this code?
#include <stdio.h>
#include <math.h>
// (1):
typedef struct
{
double x,y;
} POINT;
POINT c, o, p[10000]; // (2):
double a, q=0.0; // (3):
int n; // (4):
// (5):
double ccw(POINT a, POINT b, POINT c)
{
return a.x*b.y + a.y*c.x + b.x*c.y - c.x*b.y - b.x*a.y - c.y*a.x;
}
int main(void)
{
int i, j;
o.x=-10001; o.y=-47; // (6):
while (scanf("%d", &n) > 0) // (7):
{
if(!n) // (8):
break;
for (i = 0; i < n; i++) // (9):
scanf("%lf %lf", &p[i].x, &p[i].y);
a=0; // (10):
for(i=0; i<n; i++)
a += ccw(o, p[i], p[(i+1)%n]); // (11):
c.x = c.y = 0.0; // (12):
for(i=0; i<n; i++)
{
q = ccw(o, p[i], p[(i+1)%n])/(3.0*a); // (13):
c.x += q*(o.x + p[i].x + p[(i+1)%n].x); // (14):
c.y += q*(o.y + p[i].y + p[(i+1)%n].y);
}
printf("%.3lf\n", fabs(a) / 2.0); // (15):
printf("%.3lf %.3lf\n", c.x, c.y); // (16):
}
return 0;
}
I really can't grasp C at all, no such problems with JavaScript, HTML or XML or even Python (although I haven't really looked into that one yet), but C will most likely kill me for some reason…
I get the basis and some of the theory of what is going on in this particular code (calculates the area of the object formed by the co-ordinates, as well as the center of gravity(? - English is not my native language, so not sure if I got it right)), but I'm fucking done when it comes to how the parts work.
Could someone please explain it to me?
Input for the example:
4
-1 -1
-1 1
1 1
1 -1