source("http://macosa.dima.unige.it/r.R")    # If I have not already loaded the library
---------------------------------------------------------------------------------------
If I have a surface described by a parametric or a polar definition I can use
centreParam(X,Y, a,b) or  centrePolar(R, a,b)  to find the centroid (or barycenter).
In  centreParamx, centreParamy  and  centrePolarx, centrePolary  you can find the
coordinates.

# semiellipse
HF=3; BF=3
PLANE(-1,1.5, -1,1.5)
X=function(t) cos(t)+1/2; Y=function(t) sin(t)*2/3
para(X,Y,0,2*pi,"red")
param(X,Y,0,pi,"black"); segm(-0.5,0, 1.5,0, "black")
centreParam(X,Y, 0,pi)
# 0.5000000 0.2829421
POINT(centreParamx,centreParamy, "blue")
# If we take:
fraction(centreParamy*pi)
# 8/9
# we find that y = 8/9/pi
# It is possible to prove it is (if B is the semiaxis) 4B/(3*π)
# In this case B = 2/3

   

# cardioid
R = function(t) 1-sin(t); PLANE(-2,2, -3,1); polar(R,0,2*pi, "red")
centrePolar(R, 0,2*pi)
#  1.013796e-15 -8.333333e-01
POINT(0,centrePolary, "brown")
fraction(centrePolary)
# -5/6
                                                     
# The third figure is a more complex case. We use centrePol (see here)
PLANE(-1,1.5, -1,1.5)                  # above also in another scale
A=function(t) t;  polar(A,0,5*pi,"red")
B=function(t) 1;  polar(B,0,2*pi,"blue")
X=function(t) t; Y=function(t) t-t; param(X,Y,0,2, "black")
# circle and spiral intersect in the point rho=1, theta=1
POINT(cos(1),sin(1),"black"); text(0.3,0.75,"P")
POINT(0,0,"seagreen"); text(-0.2,-0.2,"O")
POINT(1,0,"red"); text(1.2,0.2,"Q")
# I describe the curve OQPO putting the coordinates in X and Y
X=NULL; Y=NULL
# OQ:
X[1]=0; Y[1]=0; X[2]=1; Y[2]=0
# QP:
i=seq(0,1,5000); da=1/5000; for(i in 1:5000) {X[i+2]=cos(da*i); Y[i+2]=sin(da*i)}
# PO:
for(i in 1:5000) {d=1-da*i; X[i+5002]=d*cos(d); Y[i+5002]=d*sin(d)}
# I verify (I get the brown chart that overlaps the previous one):
polyline(X,Y,"brown")
POINT(cos(1),sin(1),"green"); POINT(0,0,"green"); POINT(1,0,"green")
# The centroid
centerPol(X,Y)
#  0.6697328  0.2825991
POINT(centerPol(X,Y)[1], centerPol(X,Y)[2], "blue"
# It is non easy to find the exact form. It is:
3*(cos(1)+2*sin(1)-2); 1+3*sin(1)-6*cos(1)
#  0.6697328  0.2825991                    OK!

Other examples of use