[ home / board list / faq / random / create / bans / search / manage / irc ] [ ]

/prog/ - Programming

Programming board

Catalog

Name
Email
Subject
Comment *
File
* = required field[▶ Show post options & limits]
Confused? See the FAQ.
Embed
(replaces files and can be used instead)
Options
Password (For file and post deletion.)

Allowed file types:jpg, jpeg, gif, png, webm, mp4, pdf
Max filesize is 8 MB.
Max image dimensions are 10000 x 10000.
You may upload 1 per post.


4baaa0 No.3995

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

4baaa0 No.3996

>posted too soon

Should return:


4.000
0.000 0.000


a333c6 No.3997

You're lucky I'm ok with doing your CS homework for you OP

I'm assuming you know basic stuff like arrays that you would have hopefully learned with javascript. If you don't then fucking learn the basics first then come back.


// READ UP ON YOUR ALGORITHMS
// https://en.wikipedia.org/wiki/Centroid#Centroid_of_polygon
// https://en.wikipedia.org/wiki/Graham_scan

// (0): Tells the preprocessor to prepend the stdio & math libraries to file
#include <stdio.h>
#include <math.h>

// (1): Defines (but doesn't create) a grouping of variables called POINT
// Models the x and y coordinates of a single point.
typedef struct
{
double x,y;
} POINT;

// (2): Creates 2 POINTs and an Array of POINTS:
// c: For holding centroid (or center of mass) point coordinates
// o: For holding a reference point to create triangles from
// p: For holding the points the user puts into the program
POINT c, o, p[10000];

// (3): Creates 2 double-precision floating point number variables:
// a: For holding the accumulations of the areas of the triangles used to
// calculate area (basically builds shape out of triangles).
// q: Holds calculated distance from point to centroid
double a, q=0.0;

// (4): Integer variable for holding number of points the current shape has
int n;

// (5): Part of Graham Scan algorithm, used here to find area of triangle
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): Set o to a far away coordinate, is ref point.
while (scanf("%d", &n) > 0) // (7): Loops and gets user input in integer form,
{ // which is placed into memory address of n.
if(!n) // (8): Breaks out of loop if n is not set due to error or something
break;
for (i = 0; i < n; i++) // (9): Gets all shape points and puts in p
scanf("%lf %lf", &p[i].x, &p[i].y);
a=0; // (10): Resets accumulated area variable to 0
for(i=0; i<n; i++)
a += ccw(o, p[i], p[(i+1)%n]); // (11):sums triangles formed from
// variable c and every line into a
c.x = c.y = 0.0; // (12): Resets centroid varable to 0
for(i=0; i<n; i++)
{
q = ccw(o, p[i], p[(i+1)%n])/(3.0*a); // (13): Gets Distance from centroid
c.x += q*(o.x + p[i].x + p[(i+1)%n].x); // (14): Finds Centroid coordinates
c.y += q*(o.y + p[i].y + p[(i+1)%n].y);
}

printf("%.3lf\n", fabs(a) / 2.0); // (15): Gets and prints area
printf("%.3lf %.3lf\n", c.x, c.y); // (16): Prints the centroid coords
}

return 0; //Tells operating system that program ended properly
}




[Return][Go to top][Catalog][Post a Reply]
Delete Post [ ]
[]
[ home / board list / faq / random / create / bans / search / manage / irc ] [ ]