The centroid of a triangle is that point on which a triangle can be simply supported.
The centroid can be found by drawing a line from one corner to the midpoint of the opposite side, and another line from another corner to the midpoint of the opposite side. Where those two lines intersect is the centroid.
Googling "intersection of two lines" gives several examples for the intersection of two lines in 2-space (X and Y coordinates). The following example provides an intersection in 3-space (Z, Y and Z coordinates).
Code:
double corner[3][3], d, centroid[3]; // x, y, z coordinates of each corner
// C is the vector from corner to midpoint of opposite side
// the centroid is intersection of C[0] and C[1] (or C2 and 33 or C3 and C4)
C[0][0] = (corner[1][0] +corner[2][0])/2 -corner[0][0];
C[0][1] = (corner[1][1] +corner[2][1])/2 -corner[0][1];
C[0][2] = (corner[1][2] +corner[2][2])/2 -corner[0][2];
d = sqrt(C[0][0]*C[0][0] +C[0][1]*C[0][1] +C[0][2]*C[0][2]);
C[0][0] /= d; C[0][1] /= d; C[0][2] /= d; // normalize vector
C[1][0] = (corner[2][0] +corner[0][0])/2 -corner[1][0];
C[1][1] = (corner[2][1] +corner[0][1])/2 -corner[1][1];
C[1][2] = (corner[2][2] +corner[0][2])/2 -corner[1][2];
d = sqrt(C[1][0]*C[1][0] +C[1][1]*C[1][1] +C[1][2]*C[1][2]);
C[1][0] /= d; C[1][1] /= d; C[1][2] /= d; // normalize vector
intersection(corner[0], C[0], corner[1], C[1], centroid);
// find the intersection of two lines thru points p0 & p1, having vectors v0 & v1
void intersection(double *p0, double *v0, double *p1, double *v1, double *intersect){
double a, b, c, d, v2[3];
// v2 is the vector from p0 to p1
v2[0] = p1[0] -p0[0];
v2[1] = p1[1] -p0[1];
v2[2] = p1[2] -p0[2];
a = sqrt(v2[0]*v2[0] +v2[1]*v2[1] +v2[2]*v2[2]); // = length (p1 -p0)
v2[0] /= a; v2[1] /= a; v2[2] /= a;
// angle between v0 and v1
b = dotcos(v0, v1);
b = sqrt(1 -b*b); // sine of angle at p2
c = dotcos(v0, v2);
c = sqrt(1 -c*c); // sine of angle at p0
// d is the distance from p1 to p2
// from the law of sines, a/b = d/c
d = a/b *c; // distance from p1 to p2
intersect[0] = p1[0] +v1[0]*d;
intersect[1] = p1[1] +v1[1]*d;
intersect[2] = p1[2] +v1[2]*d;
}
// return the cosine (dot product of two matrices) of the angle between A & B
double dotcos(double *A, double *B){
double product;
product = (A[0]*B[0] +A[1]*B[1] +A[2]*B[2]) // this is the dot product
/ (sqrt(A[0]*A[0] + A[1]*A[1] +A[2]*A[2]) *sqrt(B[0]*B[0] + B[1]*B[1] +B[2]*B[2]));
return(product);
}