Natural Resources Canada
Symbol of the Government of Canada

Equation of the Center of Mass in Fortran language

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


Example of main program


C!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
C
C PROGRAM : something
C
C Description : Program that...
C
C!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
C
C Variables:  SIZE : Number of coordinates in the plane region
C             XY   : X and Y coordinates of the plane region
C             CTR  : X and Y coordinates of the center of mass
C
C!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
 
      PROGRAM something
 
      IMPLICIT NONE
 
      INTEGER*2 SIZE
      REAL*4 XY(2,18000), CTR(2)
 
      SIZE = 4
 
      XY(1,1) = 543564
      XY(2,1) = 5678654
      XY(1,2) = 543735
      XY(2,2) = 5678943
      XY(1,3) = 543423
      XY(2,3) = 5678679
      XY(1,4) = 543564
      XY(2,4) = 5678654
 
      CALL CENTROID(SIZE, XY, CTR)
 
      PRINT *,CTR(1)  ! Display the X coordinate of the center of mass
      PRINT *,CTR(2)  ! Display the Y coordinate of the center of mass
 
      STOP
      END
    

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


C!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
C
C ROUTINE : CENTROID
C
C Description : This subroutine returns the center of mass of a plane region
C               using vectors (SLOPE), y-interceps (YINTER),
C               X-beginning and X-ending coordinates (X1, X2).
C
C!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
C
C Input variables:   SIZE   : Number of coordinates in the plane region
C                    XY     : X and Y coordinates of the plane region
C
C Output variables:  CTR    : X and Y coordinates of the center of mass
C
C Arguments :        SLOPE  : Vector of slopes
C                    YINTER : Vector of y-intercepts
C                    X1     : X coordinate of the beginning of a linear segment
C                    X2     : X coordinate of the end of a linear segment
C
C!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
 
      SUBROUTINE CENTROID (SIZE, XY, CTR)
 
      IMPLICIT NONE
 
      INTEGER*2 SIZE, I
      REAL*4 XY(2,18000), CTR(2)
      REAL*8 X1(18000), X2(18000)
      REAL*8 SLOPE(18000), YINTER(18000)
      REAL*8 M1, M2, M3, MX, MY, A, SUMA, SUMMX, SUMMY
 
C Beginning of process
 
      SUMA  = 0.0D0
      SUMMX = 0.0D0
      SUMMY = 0.0D0
 
      CALL SLOPE_YINTER ( SIZE, XY, SLOPE, YINTER, X1, X2)
 
      DO I = 1,SIZE-1
         M3   = (X2(I)**3)-(X1(I)**3)
         M2   = (X2(I)**2)-(X1(I)**2)
         M1   = X2(I)-X1(I)
         MX   = ((SLOPE(I)**2)*M3/6) + (((YINTER(I)*SLOPE(I))*M2)/2) + (((YINTER(I)**2)*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
      END DO
 
C Calculation of the center of mass
 
      CTR(1) = SUMMY/SUMA    ! X coordinate of the center of mass
      CTR(2) = SUMMX/SUMA    ! Y coordinate of the center of mass
 
      RETURN
      END
    

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


C!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
C
C ROUTINE: SLOPE_YINTER
C
C Description: This subroutine returns a vector that contains the slopes and
C              y-intercepts on the x-axis for all the segments of a plane region.
C
C!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
C
C Input variables:   SIZE   : Number of coordinates in the plane region
C                    XY     : X and Y coordinates of the plane region
C
C Output variables:  SLOPE  : Vector of slopes
C                    YINTER : Vector of y-intercepts
C                    X1     : X coordinate of the beginning of a linear segment
C                    X2     : X coordinate of the end of a linear segment
C
C Arguments:         I      : Counter of coordinates
C                    X1     : X coordinate of the beginning of a linear segment
C                    X2     : X coordinate of the end of a linearsegment
C
C!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
 
      SUBROUTINE SLOPE_YINTER ( SIZE, XY, SLOPE, YINTER, X1, X2 )
 
      IMPLICIT NONE
 
      INTEGER*2 SIZE, I
      REAL*4 XY(2,18000)
      REAL*8 X1(18000), X2(18000), Y1(18000), Y2(18000), SLOPE(18000), YINTER(18000)
 
C Extracting the coordinates of the plane region
 
      DO I = 1, SIZE-2
         X1(I) = XY(1,I)
         Y1(I) = XY(2,I)
         X2(I) = XY(1,I+1)
         Y2(I) = XY(2,I+1)
      END DO
 
      X1(SIZE-1) = XY(1,SIZE-1)
      Y1(SIZE-1) = XY(2,SIZE-1)
      X2(SIZE-1) = XY(1,1)
      Y2(SIZE-1) = XY(2,1)
 
C Calculating slopes and y-intercepts
 
      DO I = 1, SIZE-1
         IF (X1(I) .EQ. X2(I)) THEN
            SLOPE(I) = 0.
            ORD(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))
         END IF
      END DO
 
      RETURN
      END