[Math] Coordinates of the centroid in a 3D rectangle

3dcentroidrectangles

I have a 3D rectangle and I have to find the 3D coordinates of its centroid. I tried to take 4 vertices, one let's say the origin $o$ and the three adjacent vertices $a, b, c$.

I computed the distances between the pairs "origin-vertex" using this $\sqrt{(o_x – a_x)^2+(o_y – a_y)^2+(o_z – a_z)^2}$ to compute the length, this $\sqrt{(o_x – b_x)^2+(o_y – b_y)^2+(o_z – b_z)^2}$ to compute the width and this $\sqrt{(o_x – c_x)^2+(o_y – c_y)^2+(o_z – c_z)^2}$ to compute the height.

(I'm doing this because my coordinates are not coplanar, otherwise, I could consider just the $x$ coordinate for the length, …)

Then I added the coordinates of the origin, respectively $o_x$ to the $\frac{length}{2}$, $o_y$ to the $\frac{width}{2}$ and $o_z$ to the $\frac{height}{2}$.

But what I'm getting is this
image

So what's the problem with my solution?

Best Answer

Let's say you know one vertex of the cuboid, $\vec{o}$, and three adjacent vertices $\vec{a}$, $\vec{b}$, and $\vec{c}$: $$\bbox{\vec{o} = \left[\begin{matrix} o_x \\ o_y \\ o_z \end{matrix}\right]} ,\quad \bbox{\vec{a} = \left[\begin{matrix} a_x \\ a_y \\ a_z \end{matrix}\right]} ,\quad \bbox{\vec{b} = \left[\begin{matrix} b_x \\ b_y \\ b_z \end{matrix}\right]} ,\quad \bbox{\vec{c} = \left[\begin{matrix} c_x \\ c_y \\ c_z \end{matrix}\right]}$$ All edges of the cuboid (where two faces meet) match one of the vectors $$\bbox{\vec{e}_a = \vec{a} - \vec{o}} , \quad \bbox{\vec{e}_b = \vec{b} - \vec{o}} , \quad \bbox{\vec{e}_c = \vec{c} - \vec{o}}$$ and the eight vertices of the cuboid are at $$\begin{array}{rll} \vec{v}_{000} =& \vec{o} & = \vec{o} \\ \vec{v}_{001} =& \vec{o} + \vec{e}_a & = \vec{a} \\ \vec{v}_{010} =& \vec{o} + \vec{e}_b & = \vec{b} \\ \vec{v}_{011} =& \vec{o} + \vec{e}_a + \vec{e}_b & = \vec{a} + \vec{b} - \vec{o} \\ \vec{v}_{100} =& \vec{o} + \vec{e}_c & = \vec{c} \\ \vec{v}_{101} =& \vec{o} + \vec{e}_a + \vec{e}_c & = \vec{a} + \vec{c} - \vec{o} \\ \vec{v}_{110} =& \vec{o} + \vec{e}_b + \vec{e}_c & = \vec{b} + \vec{c} - \vec{o} \\ \vec{v}_{111} =& \vec{o} + \vec{e}_a + \vec{e}_b + \vec{e}_c & = \vec{a} + \vec{b} + \vec{c} - 2 \vec{o} \\ \end{array}$$ The centroid for a cuboid (or a parallelepiped; the three edges do not need to be perpendicular to each other) is the sum of the vertex coordinates, divided by eight. If you sum the above, you'll find out that the centroid is at $\vec{p}$, $$\bbox{\vec{p} = \frac{1}{2}\left( \vec{a} + \vec{b} + \vec{c} - \vec{o} \right) = \left[\begin{matrix} \frac{a_x + b_x + c_x - o_x}{2} \\ \frac{a_y + b_y + c_y - o_y}{2} \\ \frac{a_z + b_z + c_z - o_z}{2} \\ \end{matrix}\right]}$$