# Simplest equation for drawing a cube based on its center and/or other vertices

I'm looking for the simplest equation to draw a cube based on its center and/or other verticies specified.

For example, let's say I have the 3D column vector (as I'm using OpenGL to do this):

x_3
y_1
z_4


I would like to specify and fill in its remaining vertices from this coordinate. I know there is a formula to do this for a square, albeit I can't seem to find it. If this is simply not possible (due to too little information), what's the simplest way to perform this?

Mind you, I have taken a look at this, although it appears to be dealing with 2-dimensional vertices/vectors rather than 3-dimensional ones. I'm using 3D entirely to build what I'm trying to build.

Thus, is this possible? If not, what are the minimum amounts of vertices which need to be given, along with the simplest equation to "fill in the gaps", so to speak?

Update

Common sense has revealed to me that this is impossible (duh). However, something tells me that it's possible to specify the center of a cube, along with its floating point mass, and compute the verticies from there. What is the formula for this?

Update 2

Note: the edge length of a cube and its center point are givens.

• To be clear: you have the edge length of the cube and the center point as givens? Commented Feb 10, 2012 at 10:25
• by edge length are you referring to length as in l x w x h? Commented Feb 10, 2012 at 10:27
• I just looked it up. Yes, the edge length of a cube and the center point are givens. Commented Feb 10, 2012 at 10:31
• Well, a mass is not a length, but I guess your cube has a constant density of $1$, in which case the mass $m$ is indeed equal to the volume $V$ and you can determine the side length with a simple $s=\sqrt[3]{V}$. If not, then you need more information than just the mass to gain information about the cube's size. Commented Feb 10, 2012 at 10:32
• Consider a cube of unit edge length centered at the origin. What would be the coordinates of the corners of that cube? From there, you only need to do scaling and shifting for arbitrarily sized cubes centered on arbitrary points. Commented Feb 10, 2012 at 10:36

A cube (the one with sides parallel to coordinate axes) centered at the point $P=(x_0,y_0,z_0)$ of "radius" $r$ (and edge length $d=2r$) has vertices $V_n=(x_0\pm r,y_0\pm r,z_0\pm r)$, which could be enumerated for $0\leq n=(b_2b_1b_0)_2=\sum_{i=0}^22^ib_i\leq 7$, where $b_i=$(n>>i)&1 is the $i^\text{th}$ bit of $n$, using the bitwise shift >> and AND & operators (familiar in C-like languages & Java), as $$V_n=(x_0+(-1)^{b_0}r,y_0+(-1)^{b_1}r,z_0+(-1)^{b_2}r).$$ The number of edges between two vertices $V_j,V_k$ would then be the bit weight of XOR$(j,k)=j$^$k$, which is also known as the hamming distance. You could emulate cube rotation by making the opposite/inverse rotation in your perspective.

As column vectors, you could write $$P=\left[\begin{matrix}x_0\\y_0\\z_0\end{matrix}\right],\quad R_n=r\left[\begin{matrix}\pm1\\\pm1\\\pm1\end{matrix}\right] =r\left[\begin{matrix}(-1)^{b_0}\\(-1)^{b_1}\\(-1)^{b_2}\end{matrix}\right],\quad V_n=P+R_n$$ where $R_n$ are the radial vectors from the center $P$ to each vertex $V_n$. This would make it easier to rotate your cube. You'd just need a rotation matrix $M$, i.e. an orthogonal matrix (which means that its transpose equals its inverse: $M^T=M^{-1}$) with determinant $+1$ (also called special orthogonal), and then $V_n=P+M\cdot R_n$ would give you the rotated vertices.

To rotate it about an arbitrary axis, you would need a special orthogonal matrix $Q$ (for change of basis) with a unit vector parallel to the axis of rotation in say the $3^\text{rd}$ column (and the other two columns containing perpendicular unit vectors satisfying the right-hand rule), and a rotation matrix such as this, which rotates the $xy$-plane about the $z$-axis: $$S= \left[ \begin{matrix} \cos\theta & -\sin\theta & 0 \\ \sin\theta & \cos\theta & 0 \\ 0 & 0 & 1 \end{matrix} \right].$$ Then you could take $M=QSQ^T$ (cf. 3D graphics approach). Another excellent method for generating rotations, recommended by @J.M. and which you may prefer, uses Rodrigues' rotation formula.

The modern computer graphics approach is usually a bit different than the matrix math of 3D linear transformations, because it uses perspective projection, which uses a $4\times4$ matrix.

Since you mention OpenGL, for a cube you might want to look some place like here or here.

• Thank you! This is just what I needed. Detailed and thorough. Commented Feb 10, 2012 at 13:17
• One last question, however. I was curious to know if you had a link which you could provide for the +-looking symbol which exists in the Rn column vector? I have no idea what that means. Commented Feb 10, 2012 at 13:24
• @Holland: It means to just choose either of $+1$ or $-1$ as needed. Commented Feb 10, 2012 at 13:41
• "To rotate it about an arbitrary axis..." - that is, he can use the Rodrigues rotation formula for the purpose. Commented Feb 10, 2012 at 13:42
• @Holland: The symbol $\pm$ is pronounced "plus or minus", and literally means just that, either plus or minus, without clarifying whether the choice is determined or not. For example, you can simplify $x^2=1$ (for example for real-valued $x$) to obtain $x=\pm1$. However, depending on the context, this may mean that $x$ can be either $+1$ or $-1$, or maybe only one choice is possible. An example from the latter case would be if $x=\sqrt y$, which imposes the condition $z\geq 0$. Another example: for integers $n$, $(-1)^n=\pm1$. The sign will depend on whether $n$ is even or odd. Commented Feb 10, 2012 at 19:08

If you are given a center point $\mathbf{c}$ and a side length $s$, then computing the 8 vertices of an axis-aligned cube centred around $\mathbf{c}$ is as simple as e.g.

$$\mathbf{v}_1=\mathbf{c}+\frac{s}{2}\begin{pmatrix}1\\1\\1\end{pmatrix},\ldots,\mathbf{v}_i=\mathbf{c}+\frac{s}{2}\begin{pmatrix}1\\-1\\-1\end{pmatrix},\ldots,\mathbf{v}_8=\mathbf{c}+\frac{s}{2}\begin{pmatrix}-1\\-1\\-1\end{pmatrix}$$

just for each permutation of $1$ and $-1$ in the offset vectors (being 8 at a whole, of course).

If the cube is not axis aligned, then this information is of course not sufficient, but I guess it is.