# How to calculate the centroid of an area

In this article we are going to explain how to calculate the centroid of an area through numerical integration. The area will be defined as the zone under a curve, delimited by data points.

The centroid of an area is equivalent with the centre of gravity of a body.

Let’s imagine a curve delimited by k data points, (xk, yk). We now want to find the coordinates of the centroid of the area under the curve.

Image: Centroid of an area defined by data points

To calculate the x-y coordinates of the Centroid we’ll follow the steps:

Step 1. Separate the total area into smaller rectangular areas Ai, where i = 0 … k. Each area consists of rectangles defined by the coordinates of the data points.

Step 2. Calculate the coordinates (xm, ym) for the Centroid of each area Ai, for each i > 0.

$\begin{split} x_{m} &= x_{i-1} + \frac{x_{i}-x_{i-1}}{2}\\ y_{m} &= \frac{y_{i}+y_{i+1}}{4} \end{split}$

Step 3. Calculate the overall Centroid x,y-coordinates as the average of all xm and ym coordinates.

$\begin{split} x &= \frac{\sum_{j=1}^{k-1} x_{m_{j}}}{k-1}\\ y &= \frac{\sum_{j=1}^{k-1} y_{m_{j}}}{k-1} \end{split}$

The higher the number of data points the better the accuracy of the algorithm. Notice that every individual area is considered to be a rectangle even if in reality they are trapezoids.

The algorithm can be encoded into a Scilab function as:

function [x,y] = Centroid(xi,yi)
for i=1:length(xi)
if i>1
xCG(i-1) = xi(i-1)+(xi(i)-xi(i-1))/2;
yCG(i-1) = ((yi(i)+yi(i-1))/2)/2;
end
end
x = (sum(xCG))/(length(xi)-1);
y = (sum(yCG))/(length(xi)-1);
endfunction


We’ll run the function for 3 sets of data points, also using Scilab instructions:

clc()
clf()
x1 = [1000 1500 2000 2500 3000 3500 4000 4500 5000 5500 6000 6500 7000];
y1 = [60 77 93 104 115 124 128 130 127 121 115 105 90];
x2 = [1000 1500 2000 2500 3000 3500 4000 4500 5000 5500 6000];
y2 = [90 121 140 144 145 145 145 143 137 128 115];
x3 = [1000 7000];
y3 = [40 40];
plot(x1,y1,"b-")
plot(x2,y2,"r-")
plot(x3,y3,"m-")
hf=gcf();
ha=gca();
ha.data_bounds = [min(x1),0;max(x1),200];
xgrid()
xlabel("x")
ylabel("y")
title("x-engineer.org","Color","blue")
[x,y] = Centroid(x1,y1);
plot(x,y,"db")
[x,y] = Centroid(x2,y2);
plot(x,y,"dr")
[x,y] = Centroid(x3,y3);
plot(x,y,"dm")
legend("x1","x2","x3","Centroid-x1","Centroid-x2","Centroid-x3")


Running the above Scilab instruction, will output the following plot:

Image: Centroid of an area defined by data points – example

To try out several data points you can use the calculator below. Make sure that the data points are separated by commas and the number of data points are the same for the x and y axis.

 x = y =

For more tutorials click the links below.