Introduction to 3D graphics

Math 213 - Math with Mathematica
Christopher Hanusa

Overview

In this tutorial, we start working with 3D graphics objects that are the analogues of their 2D counterparts.

Graphics3D and you’re good to go.

The major difference is that instead of using Graphics, you use Graphics3D to tell Mathematica that your object is a 3D object.
Once you change your (x,y)-coordinates to (x,y,z)-coordinates, you can re-use many of the 2D commands you learned last time, including Point, Line, BSplineCurve, Polygon.

0D Objects: Points

Points

{Graphics[Point[{0,0}]],Graphics3D[Point[{0,0,0}]]}

You can compare the following code with the 2D graphics code from the previous tutorial:

pointlist={{0,0,0},{1,1,0},{2,0,0},{3,1,0},{4,0,0},{5,1,0},{6,0,0},{7,1,0},{8,0,0}};
Graphics3D[Map[Point,pointlist]]

Spheres

To display a point in space, you can use the Point command, but instead you probably want to use the Sphere command.

?Sphere

Here is a bunch of spheres all lined up.

Graphics3D[Table[Sphere[{i,0,0},.2],{i,0,10}]]

Use the Map command to put a sphere at a given set of coordinates:

spherelist=Map[Sphere[#,.2]&,pointlist]
Graphics3D[spherelist]

1D Objects: Line Segments

The easiest way to take a 1D object like Line and make it 3-dimensional is to use the Tube command, which acts like a Line command, taking as input a sequence of points and creating a cylindrical path along the specified line segments that connect adjacent sets of points.

{Graphics[Line[{{0,0},{1,1}}],PlotLabel->"2D Line"],
Graphics3D[Line[{{0,0,0},{1,1,1}}],PlotLabel->"3D Line"],
Graphics3D[Tube[{{0,0,0},{1,1,1}},.05],PlotLabel->"3D Tube"]}

zigzag=Graphics3D[Tube[pointlist,.2]]

Comprehension Question

1. Create a grid of spheres.

2. Create a square with some thickness.  It should look something like this:

3. Put spheres on the corners of your square from Question 2 using the Map command, similar to this:

Solutions

Question 1

Graphics3D@Table[Sphere[{i,j,k},.2],{i,1,4},{j,1,4},{k,1,4}]

Question 2

Graphics3D[{
Tube[{{0,0,0},{0,1,0}},.1],
Tube[{{0,1,0},{1,1,0}},.1],
Tube[{{1,1,0},{1,0,0}},.1],
Tube[{{1,0,0},{0,0,0}},.1]
}]

Question 3

points={{0,0,0},{0,1,0},{1,1,0},{1,0,0}};
segments={{{0,0,0},{0,1,0}},{{0,1,0},{1,1,0}},{{1,1,0},{1,0,0}},{{1,0,0},{0,0,0}}};

Graphics3D[{
Map[Tube[#,.1]&,segments],
Map[Sphere[#,.15]&,points]
}]

Challenge Questions

4. Make your grid of spheres from Question 1 vary in size and color.

5. Create a 3D grid that looks like this, analogous to the 2D grid from the last tutorial:

1D Objects: Curves

To generate and 3D print curves in space, you will need to view them as vector-valued functions.  (Wikipedia Link: https://en.wikipedia.org/wiki/Vector-valued_function)  In other words, you think of them as functions of time, where as time changes, the tip of the vector sweeps out a curve in (x,y,z)-space.  We are going to use ParametricPlot3D to create a visualization of the curve.

?ParametricPlot3D

Here is a function that changes its x-coordinate, y-coordinate, and z-coordinate over time:
x(t)=sin(t)
y(t)=sin(2t)
z(t)=sin(3t)

{Plot[Sin[t],{t,0,2Pi},PlotLabel->"x-coordinate"],
Plot[Sin[2t],{t,0,2Pi},PlotLabel->"y-coordinate"],
Plot[Sin[3t],{t,0,2Pi},PlotLabel->"z-coordinate"]}

When we put them all together, we get this very interesting curve:

ParametricPlot3D[{Sin[t],Sin[2t],Sin[3t]},{t,0,2Pi},PlotRange->All]

To get a three-dimensional version of this curve, use the option PlotStyle->Tube[].

ParametricPlot3D[{Sin[t],Sin[2t],Sin[3t]},{t,0,2Pi},PlotStyle->Tube[.1],PlotRange->All]

To add some color, add it to the PlotStyle options.

ParametricPlot3D[{Sin[t],Sin[2t],Sin[3t]},{t,0,2Pi},PlotStyle->{Orange,Tube[.1]},PlotRange->All]

Comprehension Question

6. A helix is a three-dimensional spiral shape that has parametric equations
x(t)=sin(t), y(t)=cos(t), and z(t)=t.  Create a helix that spirals around multiple times and has some thickness, that looks something like this:

7. Create a torus using this technique.

8. Add some spheres that intersect this torus.

Important reminder: When you need to combine multiple Graphics objects, use the Show command.

Solutions

Question 6.

ParametricPlot3D[{Sin[t],Cos[t],t},{t,0,5Pi},PlotStyle->{Blue,Tube[.25]},PlotRange->All]

Question 7.

torus=ParametricPlot3D[{Sin[t],Cos[t],0},{t,0,2Pi},PlotStyle->{Yellow,Tube[.25]},PlotRange->All]

Question 8.

spherecenters=Table[{Sin[t],Cos[t],0},{t,0,2Pi,2Pi/5}];
spheres=Graphics3D[{Darker[Green],Map[Sphere[#,.4]&,spherecenters]}];

Show[torus,spheres]

Challenge Question

9. Create a model of DNA using a double helix with tubes connecting the two helices.

2D Objects: Polygons

Just like in 2D Graphics, you can create polygons in three dimensions.  Remember to wrap it in a Graphics3D command.

{Graphics[Polygon[{{0,0},{1,1},{0,1}}]],Graphics3D[Polygon[{{0,0,0},{1,1,1},{0,1,1/2}}]]}

Comprehension Question

10. Create three sides of a house using polygonal faces.

Hint 1: (expand to see)

First determine the coordinates of each of the corners of the faces.

Hint 2: (expand to see)

You can create one face of the tetrahedron in this way:

v1={0,0,0};
v2={0,0,1};
v3={0,1,1};
v4={0,1,0};

Graphics3D[Polygon[{v1,v2,v3,v4}]]

Solution

Question 10

v1={0,0,0};
v2={0,0,1};
v3={0,1,1};
v4={0,1,0};
v5={1,0,0};
v6={1,1,0};
v7={1,0,1};

Graphics3D[{Polygon[{v1,v2,v3,v4}],Polygon[{v1,v2,v7,v5}],Polygon[{v1,v5,v6,v4}]}]

2D Objects: Graphs of functions of two variables

Plot3D is used to plot the graph of a two-dimensional function z=f(x,y).  Increase the number of PlotPoints to make the surface smoother.

Plot3D[Sin[x+y^2],{x,-3,3},{y,-2,2},Mesh->None,PlotPoints->30]

There is a secret option called Extrusion that allows you to thicken surfaces in order to be able to 3D print them.

Plot3D[Sin[x+y^2],{x,-3,3},{y,-2,2},Mesh->None,PlotPoints->50,Extrusion->.2]

Comprehension Question

11. Create a bowl using the equation of a paraboloid.

Hint for people who never took Multivariable Calculus: (expand to see)

A paraboloid is a function of the form z=x^2+y^2.

Solution

Did you try it first?  If not, go try it.

Plot3D[x^2+y^2,{x,-3,3},{y,-2,2},Mesh->None,PlotPoints->50,Extrusion->.2]

Challenge Question

12. Add some spheres to the surface of your bowl from Question 11.

3D Objects: RegionPlot3D

Intersecting Regions

Another way to plot a closed region in 3D space is to use RegionPlot3D.  It gives all the points in the specified region that satisfy your given requirements.

What is the region inside two spheres?

spheres=Graphics3D[{Opacity[.3],Red,Sphere[{0,0,0},1],Blue,Sphere[{1,0,0},1]}]

intersection=RegionPlot3D[
(x^2+y^2+z^2≤1)&&((x-1)^2+y^2+z^2≤1),
{x,-1,2},{y,-1.5,1.5},{z,-1.5,1.5},PlotPoints->50]

Show[spheres,intersection]

Blocks with surfaces determined by functions

We can also use Mathematica to create objects that have a solid base and an interesting surface as its top face.  When the bounding function is z=f(x,y), we can use RegionPlot3D to take the set of points that lie below the curve, which are of the form z<f(x,y).  Here is an example of a parabolic bowl, and a nice bumpy surface.  Notice that you need to think about which x, y, and z bounds will make your block look the best.

bowlblock=RegionPlot3D[x^2+y^2>z,{x,-1,1},{y,-1,1},{z,-.5,3}]

block=RegionPlot3D[Sin[x]+Sin[y]>z,{x,0-Pi/2,6Pi-Pi/2},{y,0-Pi/2,6Pi-Pi/2},{z,-3,3},PlotPoints->30]

Comprehension Question

13. Create a rectangular prism by using RegionPlot3D.

Hint 1:

Determine the equations of the faces of the solid.

Hint 2:

Equations of faces look like x=a or y=b or z=c.

14. What does the 3D region look like that is the intersection of two perpendicular cylinders?  Three perpendicular cylinders?

Hint for those who did not take Multivariable Calculus: (expand to see)

The equations of some solid cylinders are x^2+y^2<=1, x^2+z^2<=1, y^2+z^2<=1.

15. Create a block with upper surface defined by f(x,y)=sin(x+y^2)

Solutions

Question 13

RegionPlot3D[1≤x≤4&&1≤y≤3&&2≤z≤4,{x,0,5},{y,0,5},{z,0,5},PlotPoints->30]

Question 14

RegionPlot3D[x^2+y^2≤1&&x^2+z^2≤1,{x,-1.5,1.5},{y,-1.5,1.5},{z,-1.5,1.5},PlotPoints->80,Mesh->None,Boxed->False,Axes->False]

RegionPlot3D[x^2+y^2≤1&&x^2+z^2≤1&&y^2+z^2≤1,{x,-1.5,1.5},{y,-1.5,1.5},{z,-1.5,1.5},PlotPoints->80,Mesh->None,Boxed->False,Axes->False]

Question 15

RegionPlot3D[Sin[x+y^2]>z,{x,0,3Pi},{y,0,3Pi},{z,-3,3},PlotPoints->30]

Challenge Question

16. Intersect some other interesting 3D objects in different ways.  What is the most interesting object you can create in this way?

17. Create a “cube” that has an interesting surface (defined by some equation) on each side.

3D Objects: Polyhedra

PolyhedronData

A polyhedron can be thought of as the three-dimensional analog of a polygon.  (And the plural of polyhedron is polyhedra!!)  The corners are called vertices, line segments connecting vertices are called edges, and the 2-D polygons are called faces.  Mathematica has a large number of built-in polyhedra, accessible using the command PolyhedronData.

Length[PolyhedronData[All]]

For example, here is a visualization of the 20-sided polyhedron called an icosohedron.

Graphics3D[{PolyhedronData["Icosahedron","Faces"]}]

Copied directly from the Mathematica Documentation Center, here is an explorer of the polyhedra that Mathematica has built in along with their properties:

Manipulate[Column[{PolyhedronData[g],PolyhedronData[g,p]}],{g,PolyhedronData[All]},{p,Complement@@PolyhedronData/@{"Properties","Classes"}}]

Navigate to the Documentation Center entry for PolyhedronData, and look at the “Neat Examples” at the bottom.

GraphicsComplex

If you want to create your own polyhedron, use GraphicsComplex by specifying the list of coordinates of all the vertices and then specifying which vertices connect to be a face of the object.
(Again, from the Documentation Center:)

vertices={{0,0,0},{2,0,0},{2,2,0},{0,2,0},{1,1,2}};
faces={{1,2,5},{2,3,5},{3,4,5},{4,1,5}};
Graphics3D[{Opacity[.8],Yellow,GraphicsComplex[vertices,Polygon[faces]]}]

Navigate to the Documentation Center entry for GraphicsComplex, and look at the “Properties & Relations” near the bottom.

Comprehension Question

18. Create a tetrahedron using PolyhedronData.  Use one command.

Hint: (expand to see)

Replace one word in how we created the icosahedron above.

Challenge Question

19. Create a square antiprism using GraphicsComplex.  To get you started, the coordinates of the vertices are given as well as the first three faces:

s=1/Sqrt[2];
APVertices={{1,0,0},{0,1,0},{-1,0,0},{0,-1,0},{s,s,1},{-s,s,1},{-s,-s,1},{s,-s,1}};
APFaces={{1,2,3,4},{5,6,7,8},{1,2,5}};
Graphics3D[{Opacity[.8],Yellow,GraphicsComplex[APVertices,Polygon[APFaces]]}]

Hint: (expand to see)

You may find it helpful to label the vertices:

s=1/Sqrt[2];
APVertices={{1,0,0},{0,1,0},{-1,0,0},{0,-1,0},{s,s,1},{-s,s,1},{-s,-s,1},{s,-s,1}};
APFaces={{1,2,3,4},{5,6,7,8},{1,2,5}};
Graphics3D[{Opacity[.8],Yellow,GraphicsComplex[APVertices,Polygon[APFaces]],{Black,MapThread[Style[Text[#1,#2],FontSize->20]&,{Range[8],APVertices}]}}]

Solution to Challenge Questions

Question 4

Graphics3D@Table[Sphere[{i,j,k},.2],{i,1,4},{j,1,4},{k,1,4}]

Question 5

Graphics3D[{
Table[Tube[{{i,j,0},{i,j,5}}],{i,0,5},{j,0,5}],
Table[Tube[{{0,i,j},{5,i,j}}],{i,0,5},{j,0,5}],
Table[Tube[{{i,0,j},{i,5,j}}],{i,0,5},{j,0,5}]
}]

Question 9

helix1=ParametricPlot3D[{Sin[t],Cos[t],t},{t,0,5Pi},PlotStyle->{Blue,Tube[.25]},PlotRange->All];
helix2=ParametricPlot3D[{Sin[t+Pi],Cos[t+Pi],t},{t,0,5Pi},PlotStyle->{Green,Tube[.25]},PlotRange->All];
lines=Graphics3D[{Yellow,Table[Tube[{{Sin[t],Cos[t],t},{Sin[t+Pi],Cos[t+Pi],t}},.2],{t,0,5Pi}]}];
Show[helix1,helix2,lines]

Question 12

bowl=Plot3D[x^2+y^2,{x,-3,3},{y,-3,3},Mesh->None,PlotPoints->50,Extrusion->.2];
bowlspheres=Graphics3D[Table[Sphere[{i,j,i^2+j^2},.3],{i,-2,2,.5},{j,-2,2,.5}]];
Show[bowlspheres,bowl,PlotRange->{-.5,8},Boxed->False]

Question 19

s=1/Sqrt[2];
APVertices={{1,0,0},{0,1,0},{-1,0,0},{0,-1,0},{s,s,1},{-s,s,1},{-s,-s,1},{s,-s,1}};
APFaces={{1,2,3,4},{5,6,7,8},{1,2,5},{5,6,2},{2,3,6},{6,7,3},{3,4,7},{7,8,4},{4,1,8},{8,5,1}};
Graphics3D[{Opacity[.8],Yellow,GraphicsComplex[APVertices,Polygon[APFaces]]}]