Natural Resources Canada
Symbol of the Government of Canada

Equation of the Center of Mass in C language

The C implementation of the equation of the Center of Mass is divided in three parts:


Example of main program


/**************************************************************************\
| Program : something
| Description : Program that...
|**************************************************************************
| Variables:  dim : Number of coordinates in the plane region
|             xy  : X and Y coordinates of the plane region
|             ctr : X and Y coordinates of the center of mass
\**************************************************************************/
main () {
	int n;
	double xy[2] [18000], ctr[2];
	 
	n = 4;
	xy[0] [0] = 543564;
	xy[1] [0] = 5678654;
	xy[0] [1] = 543735;
	xy[1] [1] = 5678943;
	xy[0] [2] = 543423;
	xy[1] [2] = 5678679;
	xy[0] [3] = 543564;
	xy[1] [3] = 5678654;
	 
	centroid (n-1, xy, ctr);
	 
	printf("%f\n",ctr[0]); /* Display the X coordinate of the center of mass */
	printf("%f\n",ctr[1]); /* Display the Y coordinate of the center of mass */
}
    

Subroutine that calculates the X and Y coordinates of the center of mass of a plane region


/*****************************************************************************\
| Routine : centroid
| Description : This subroutine returns the center of mass of a plane region
|               using vectors (SLOPE), y-interceps on the x-axis (YINTER),
|               X-beginning and X-ending coordinates (X1, X2).
|*****************************************************************************
| Input variables:   dim    : Number of coordinates in the plane region
|                    xy     : X and Y coordinates of the plane region
| Output variables:  ctr    : X and Y coordinates of the center of mass
| Arguments :        slope  : Vector of slopes
|                    yinter : Vector of y-intercepts
|                    x1     : X coordinate of the beginning of a linear segment
|                    x2     : X coordinate of the end of a linear segment
\*****************************************************************************/
centroid (int dim, double xy[2] [18000], double ctr[2]) {
 
	int    i;
	double x1[18000], x2[18000];
	double slope[18000], yinter[18000];
	double m1, m2, m3;
	double a, mx, my, suma, summx, summy;
	 
	/* Beginning of process */
	 
	suma  = 0;
	summx = 0;
	summy = 0;
	 
	slop_yin (dim, xy, slope, yinter, x1, x2);
	 
	for (i = 0; i < dim; i++) {
	   m3 = (x2[i]*x2[i]*x2[i])-(x1[i]*x1[i]*x1[i]);
	   m2 = (x2[i]*x2[i])-(x1[i]*x1[i]);
	   m1 = x2[i]-x1[i];
	   mx = ((slope[i]*slope[i])*m3/6)+(((yinter[i]*slope[i])*m2)/2) 
	                + (((yinter[i]*yinter[i])*m1)/2);
	   my = ((slope[i]*m3)/3)+((yinter[i]*m2)/2);
	   a  = ((slope[i]/2)*m2)+(yinter[i]*m1);
	   suma = suma + a;
	   summx = summx + mx;
	   summy = summy + my;
	   }
	 
	/* Calculation of the center of mass */
	 
	ctr[0] = summy/suma;    /* x coordinate of the center of mass */
	ctr[1] = summx/suma;    /* y coordinate of the center of mass */
}
    

Subroutine that calculates the slopes and y-intercepts of a plane region


/*****************************************************************************\
| Routine: slop_yin
| Description: This subroutine returns a vector that contains the slopes and
|              y-intercepts on the x-axis for all the segments of a plane region.
|*****************************************************************************
| Input variables:   dim    : Number of coordinates in the plane region
|                    xy     : X and Y coordinates of the plane region
| Output variables:  slope  : Vector of slopes
|                    yinter : Vector of y-intercepts
|                    x1     : X coordinate of the beginning of a linear segment
|                    x2     : X coordinate of the end of a linear segment
| Arguments:         i      : Counter of coordinates
|                    x1     : X coordinate of the beginning of a linear segment
|                    x2     : X coordinate of the end of a linear segment
\*****************************************************************************/
slope_yin ( int dim, double xy[2] [18000], double slope[18000], 
            double yinter[18000], double x1[18000], double x2[18000]) {
	int i;
	double y1[18000], y2[18000];
	 
	/* Extracting the coordinates of the plane region */	 
	for (i = 0; i < dim-1; i++) {
	   x1[i] = xy[0] [i];
	   y1[i] = xy[1] [i];
	   x2[i] = xy[0] [i+1];
	   y2[i] = xy[1] [i+1];
	}
	 
	x1[dim-1] = xy[0] [dim-1];
	y1[dim-1] = xy[1] [dim-1];
	x2[dim-1] = xy[0] [0];
	y2[dim-1] = xy[1] [0];
	 
	/* Calculating slopes and y-intercepts */	 
	for (i = 0; i < dim; i++) {
	   if (x1[i] == x2[i]) {
	      slope[i] = 0;
	      yinter[i] = 0;
	   }
	   else {
	      slope[i] = (y2[i] - y1[i]) / (x2[i] - x1[i]);
	      yinter[i]   = ((x1[i] * y2[i]) - (x2[i] * y1[i])) / (x1[i] - x2[i]);
	   }
	}
}