Skip to content

Centroids

The objective of this section is to cover geometric constructions using points and vectors. In particular, this section focuses on computing geometric center points or centroids.

Parallelogram

Provided with a parallelogram ( a, b, c, d ) defined by its four points a, b, c, and d, determine the point o at its center.

""" Inputs
"""
a = Point3d( ax, ay, az )
b = Point3d( bx, by, bz )
c = Point3d( cx, cy, cz )
d = Point3d( dx, dy, dz )
Solution

The solution requires noticing that the sum of two vectors spans a parallelogram with direction along its diagonal. A vector with half that length points to the center of the parallelogram.

  1. Compute two vectors along the parallelogram's sides u = b - a and v = d - a.
  2. Compute the sum of the vectors and half it w = ( u + v ) / 2.
  3. Translate the point a by the vector o = a + w.
""" Outputs
"""
u = b - a
v = d - a
w = ( u + v ) / 2
o = a + w

Alternatively, notice that the point in the center of the parallelogram is where its diagonals cross. Moreover, the point at the center splits the diagonals in two equal parts. Thus a simpler solution involves only computing the mid-point of either diagonal points.

""" Middle of Diagonals
"""
o = ( a + c ) / 2
''' or '''
o = ( b + d ) / 2

Triangle

Provided with a triangle ( a, b, d ) defined by three points a, b, and c, determine the location of the point o at the triangle's centroid.

""" Inputs
"""
a = Point3d( ax, ay, az )
b = Point3d( bx, by, bz )
c = Point3d( cx, cy, cz )
Solution

Triangles have many centers from which the centroid is the simplest to compute. The centroid is situated at the point of intersection between its medians, that is the lines that connects its points with the middle of its opposite sides. It can be computed as the mean of its points.

""" Outputs
"""
o = ( a + b + c ) / 3

Point List

Provided with a list of n points in space, [p1, p2, ..., pn] determine the location of the point o at its center.

""" Inputs
"""
points = [Point3d( x1, y1, z1 ),
          Point3d( x2, y2, z2 ),
          #...
          Point3d( xn, yn, zn )]
Solution

The centroid for a list of points can be computed as the mean among its coordinates. The following implementation sets up a zero point and accumulates the coordinates using point addition. Finally, the centroid is computed by dividing the coordinates with the number of points.

""" Outputs
"""
o = Point3d( 0.0, 0.0, 0.0 )
for point in points:
    o += point
o *= 1.0 / len( points )