If you're seeing this message, it means we're having trouble loading external resources on our website.

If you're behind a web filter, please make sure that the domains *.kastatic.org and *.kasandbox.org are unblocked.

### Course: Computer programming - JavaScript and the web>Unit 4

Lesson 7: 3D shapes

# Describing 3D shapes

A simple shape to start working with is a cube. Although a tetrahedron has fewer sides, its sides aren't orthogonal, which makes things trickier. Let's start by creating a 200×200×200 pixel cube, centred at the origin (0, 0, 0).
We will start not with actually drawing anything, but with coming up with arrays of numbers that describe our shapes in 3D shape - specifically, arrays that describe our nodes and our edges.

### Nodes

We start by defining an array of nodes, where each node is an array of three numbers, the x, y and z coordinates of that node:
``````var node0 = [-100, -100, -100];
var node1 = [-100, -100, 100];
var node2 = [-100, 100, -100];
var node3 = [-100, 100, 100];
var node4 = [ 100, -100, -100];
var node5 = [ 100, -100, 100];
var node6 = [ 100, 100, -100];
var node7 = [ 100, 100, 100];
var nodes = [node0, node1, node2, node3, node4, node5, node6, node7];``````
As you may have noticed, the nodes are all 8 ways of arranging three lots of positive or negative 100.
You can see the nodes of a 2x2x2 cube centered at the origin in the visualization below. Rotate using the mouse:

### Edges

Next we define an array of edges, where each edge is an array of two numbers. For example, `edge0` defines an edge between `node0` and `node1`. We start counting at 0 because arrays are indexed starting at zero (To get the value of the first node we type `nodes[0]`).
``````var edge0 = [0, 1];
var edge1 = [1, 3];
var edge2 = [3, 2];
var edge3 = [2, 0];
var edge4 = [4, 5];
var edge5 = [5, 7];
var edge6 = [7, 6];
var edge7 = [6, 4];
var edge8 = [0, 4];
var edge9 = [1, 5];
var edge10 = [2, 6];
var edge11 = [3, 7];
var edges = [edge0, edge1, edge2, edge3, edge4, edge5, edge6, edge7, edge8, edge9, edge10, edge11];``````
The tricky part is making sure you join the right edges together. Here's a visualization of the edges we're connecting for a cube:

## Want to join the conversation?

• How would you go about making a 3-D ellipse?
• How come there are so many functions just to build 1 cube in a code? Is it because of the volume, width, and height, or they are trying to see what we can do? Otherwise, this is great!
• We have to draw the cube manually which is why there's so much code - if you drew a rectangle manually for example, you would have to list all 4 x, y coordinates (like when drawing a quad) & also label all the lines connecting them, then finally fill in the space between the lines, but all that stuff is done automatically when using the "rect" function. Plus with 3-D you add that 3rd dimension which just amounts to even more points to keep track of - once you get the basic "engine" down though, you can create your own function to draw 3D shapes more easily using your own "automatic" function...
• how do you make a sphere?
• It shall be reaveled a few lessons after this
:DD
• Instead of using an array to store the location of each node, could we use an object with properties x y and z?
• I have seen people use "isoBlock" ,but I'm not quite sure how to use it.
If anyone could help me, it would be great.
:)
• If you saw someone using `isoBlock` it was possibly a variable or function they created for rendering isometric graphics.

You could ask the creator of the program if you have questions about how to wrote the function.
• how cau you make a 3-D elipse and how can you make a 3-D moon
• How might one make the nodes for a tetrahedron?
• maybe bye changeing thenumbers in the middle or the end.
• x is the horizontal axis and y is the vertical axis. Similarly, how would we describe the z axis?
If I use "depth axis", it won't really be the same, it would be similar to "height" and "width". But height and width are different than horizontal and vertical, hence depth shall be different than the answer to this question too.
Another way to think about this is x is between left and right, making it the horizontal axis; y is between up and down, making it the horizontal axis; and z is between front and back. So how do I describe z in a single word?