Precisely Rendering N-Dimensional Shapes Like Cubes




Introduction: Precisely Rendering N-Dimensional Shapes Like Cubes

In this tutorial, you will learn how to render a 3D cube with mathematically perfect proportions and depth of field.

This tutorial requires a working knowledge of high school algebra and trigonometry, or the ability to follow mathematical instructions. It is assumed that you already understand Pythagoras' Theorem. It should take approximately 20 minutes.

You will need a pencil, graph paper, protractor, and computer. Before beginning this tutorial, have a browser open to Wolfram Alpha, or have a graphing calculator that you know how to use. The commands that you must use to render a cube include arithmetic, square, square root, sin, cos, tan, arcsin, arccos, and arctan.

Teacher Notes

Teachers! Did you use this instructable in your classroom?
Add a Teacher Note to share how you incorporated it into your lesson.

Step 1: Plotting an Angle

Rendering shapes requires a basic knowledge of geometry. Fortunately, most line-based geometric math is very simple. For this example, you need only use right triangles. Figure 1a shows the relationship between an angle in a right triangle and the nearby sides. As you can see, knowing any two pieces of information allows one to find the other two as well.

Say that you want to find the angle from point (0,0) on your graph to point (2,4). This means that you want to find the left hand angle on a triangle which stretches out 2 units right and then 4 units up. The opposite side "O" is 4 and the adjacent side "A" is 2. To find the angle we simply look for an equation that has both "O" and "A," then put our numbers into it. Since the number we want is the angle inside of the tan function, we press the "shift" key on our calculator, or type "arctan" into Wolfram, to do the function backwards. Taking the arctan of 2, we get an angle of 63 degrees. You can see from your protractor that this is correct.

Step 2: Placing the First Corner and Line

The first thing to do is settle on where your shape is going to begin. Mark a table on your paper for listing the coordinates of each vertex, or corner, as you place it. These will have two columns, one for the coordinates of the vertex, and one for a name that shows what other vertices each is linked to. Since you are drawing a shape based on squares, these names will consist of three letters signifying the three pairs of faces/vertices the cube has. Each letter will be either "a" or "b" to signify which side its vertex is on.

For this demonstration, place your first vertex at the origin point (0,0,0). This will be called vertex "aaa." You will be drawing a cube which is pressed edge-on against the viewer. In order to demonstrate how to handle angles, the cube will be rotated 30 degrees to the right, and laid flat on the ground.

Make your second dot at position (sqrt(3),1,0), This gives an edge which is 2 units long, and turned 30 degrees away from you on the right side. Call this one "baa."

Step 3: Number of Corners

Before you can place any more vertices, you need to decide how many there are, and what angles they branch off at. In general, the number of vertices in a square-sided shape is equal to 2^D where D is the number of dimensions being rendered. Squares have 4 vertices, cubes have 8, and tesseracts have 16. You will only need 8 for this tutorial.

You can see this visually represented in Figure 3B.

Step 4: Solve for a Square

Now it is time to find the remaining vertices. Cubes are not overly difficult to do this for. Start by finding the remaining two vertices of the base square. (Fig 4a)

A right triangle along the xy plane can have the existing line as a side. If the opposite side is of the same length, it will terminate at (-1,sqrt(3),0), which is vertex "aba." The remaining vertex will be the sum of these two, minus the starting vertex.

With this solved, the four bottom vertices are:

(0,0,0) aaa

(-1, sqrt(3),0) aba

(sqrt(3),1,0) baa

(sqrt(3)-1,sqrt(3)+1,0) bba

Step 5: Solve for a Cube

Now that you have a square sitting on the ground, it is time to expand it in a new dimension. Since the square is flat against the ground, no complicated math is required to extend it. Simply add four new vertices 2 units above the originals. You should now have the following eight vertices.

(0,0,0) aaa

(-1, sqrt(3),0) aba

(sqrt(3),1,0) baa

(sqrt(3)-1,sqrt(3)+1,0) bba

(0,0,2) aab
(-1, sqrt(3),2) abb

(sqrt(3),1,2) bab

(sqrt(3)-1,sqrt(3)+1,2) bbb

Step 6: Render Without Depth

Now comes the hard part. You currently have a graph of a cube. This means that you have a set of vertices that, when graphed, will portray a 2X2X2 cube turned slightly sideways on a 3D graph. This is all well and good for simulation and modelling, but it lacks quantifiable visible depth. In order to properly graph a picture of a cube with the correct depth, you must interpret the points and lines of the cube as shadows on a 2D screen.

Once you have decided on a direction, you must find the distance and angle from a line drawn by the center of the camera to any given vertex. In this case, you need to find where each point is compared to the Y axis by finding the longest side of a right triangle whose shorter sides are the vertex's x and z coordinates. Assume that the positive x axis represents a horizontal angle of 0 degrees.

Taking bbb for an example, you would make a triangle with an "O" side equal to 2 and an "A" side equal to sqrt(3)-1. This gives an angle of about 70 degrees, and a distance of 2.13. Performing similiar operations on each vertex gives you a new table that looks like:

aaa: 0 units at 0 degrees

aba: 1 unit at 180 degrees

baa: 1.73 units at 0 degrees

bba: .73 units at 0 degrees

aab: 2 units at 90 degrees

abb: 2.24 units at 153 degrees

(sqrt(3),1,2) bab: 2.65 units at 50 degrees

(sqrt(3)-1,sqrt(3)+1,2) bbb: 2.13 units at 70 degrees

Step 7: Add Depth

Now that you have your image rendered without depth, all that is required is to figure out how distance equates to position on the screen. This relationship will not affect the angle of the vertices, but will generally move them towards the center of the screen.

For each vertex, set up one more triangle with "A" equal to the y coordinate and "O" equal to the distance you found in the previous step. The angle you find for these triangles will be divided by the chosen field of view to determine what fraction of the way from center to edge of the screen the vertex is.

To do an example, bbb is a triangle with "A" equal to 2.73 and "O" equal to 2.13. It produces an angle of 47 degrees, which means that it is at 47/90ths of the screen's width away from the center.This means that the vertex is slightly beyond the line which marks the edge of vision.

For this step, your table should end up looking like:

aaa: 0 units at 0 degrees

aba: 60/120 at 180 degrees

baa: 60/120 at 0 degrees

bba: 15/120 at 0 degrees

aab: infinite at 90 degrees

abb: 52/120 at 153 degrees (sqrt(3),1,2)

bab: 69/120 units at 50 degrees (sqrt(3)-1,sqrt(3)+1,2)

bbb: 38/120 at 70 degrees

Step 8: Rendering Points

Now that you have all eight points reduced to a distance and angle, it is time to put that protractor to work. Mark out a "screen" of whatever size you choose on your paper. Place a cross at the center, and be sure that you know where the horizon is.

Use your protractor to measure out the correct angle for each point, and place each vertex at a distance equal to the width of the screen times the fraction you have written down for it. Be sure to label each vertex as you plot it.

Step 9: Render Edges

Every shape needs edges. This is the purpose of the names you assigned to your vertices. Go through your list and connect any vertices which have only one mismatched letter. In other words, you will connect abb and bbb, as well as aba and bba, but will not connect aaa and abb.

One of your vertices is exactly perpendicular to your camera. This vertex will connect with its partners through a vertical line coming from directly overhead. Though this may look odd, it is how a cube would look if you were able to occupy the exact same space as one of its corners.

Step 10: Try a Tesseract

In this tutorial, you have learned how to graph in 3D. You have learned how to expand a line into a square and a square into a cube.

If you want a challenge, try extending this method to create a hypercube, or tesseract. If you assume that the tesseract is flat along its fourth dimension, as you assumed that the cube would be flat along its third, you will find that the vertices simply duplicate once more along a new axis. There will be more operations to do mapping these new vertices, but most of the rules will remain constant. When rendering edges, remember to still connect any vertices that share at least two letters. If done correctly, you will appear to have drawn a cube within a cube.

1 Person Made This Project!


  • Heart Contest

    Heart Contest
  • Fiber Arts Contest

    Fiber Arts Contest
  • Paper Contest

    Paper Contest