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, Translate, Scale.

The easiest way to take a 1D object like Line or BSplineCurve 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.  (Or in the case of BSplineCurve, it generates a cylindrical path along the specified curve.)

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

{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"]}

{Graphics[BSplineCurve[{{0,0},{1,1},{0,2}}],PlotLabel->"2D Spline"],
Graphics3D[BSplineCurve[{{0,0,0},{1,1,1},{0,2,1}}],PlotLabel->"3D Spline"],
Graphics3D[Tube[BSplineCurve[{{0,0,0},{1,1,1},{0,2,1}}],.05],PlotLabel->"3D Spline with thickness"]}
(*Notice the Tube command applying to the BSplineCurve command *)

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

Tube also allows you to specify the radius of the cylinder, and even the radius of the cylinder at each of the specified points.
The method by which the tube segments are joined is determined by JoinForm and the shape of the endcaps is defined by CapForm.

Graphics3D[{CapForm["Butt"],JoinForm["Round"],Tube[{{0,0,0},{1,1,1},{0,2,0}},{.1,.03,.1}]}]

pts={{1,1,-1},{2,2,1},{3,3,-1},{3,4,1}};
Graphics3D[{Tube[BSplineCurve[pts],{.01,.5,.1,.01}],Green,Line[pts],Red,Point[pts]}]

Comprehension Questions

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

Basic 3D objects

The building blocks for 3D objects are Sphere, Cuboid

Sphere gives a sphere centered a specified point with specified radius.

Graphics3D[{Opacity[.8],Sphere[{1,2,2},.5]},Axes->True,PlotRange->{{0,3},{0,3},{0,3}}]

Cuboid create a rectangular prism defined by two opposite corners.

Graphics3D[{
{Red,Sphere[{0,0,0},.03]},
{Green,Sphere[{1,.5,.1},.03]},
Cuboid[{0,0,0},{1,.5,.1}]}]

Cylinder creates a cylinder by taking as input the line segment between the centers of the end circles, along with the radius of the cylinder.  This can also be accomplished by using Tube.

{Manipulate[Graphics3D[{
{Red,Sphere[{0,0,0},.3]},
{Green,Sphere[{1,1,1},.3]},
{Opacity[.8],Cylinder[{{0,0,0},{1,1,1}},rad]}},PlotRange->3],{{rad,1},0,2}],
Manipulate[Graphics3D[{
{Red,Sphere[{0,0,0},.3]},
{Green,Sphere[{1,1,1},.3]},
{Opacity[.8],CapForm["Butt"],Tube[{{0,0,0},{1,1,1}},rad]}},PlotRange->3],{{rad,1},0,2}]}

Cone creates a cone by taking as input the line segment between the center of the circle and the cone’s vertex, along with its radius.  This can also be accomplished by using Tube.

{Manipulate[Graphics3D[{
{Red,Sphere[{0,0,0},.3]},
{Green,Sphere[{1,1,1},.3]},
{Opacity[.8],Cone[{{0,0,0},{1,1,1}},rad]}},PlotRange->3],{{rad,1},0,2}],
Manipulate[Graphics3D[{
{Red,Sphere[{0,0,0},.3]},
{Green,Sphere[{1,1,1},.3]},
{Opacity[.8],CapForm["Butt"],Tube[{{0,0,0},{1,1,1}},{rad,0}]}},PlotRange->3],{{rad,1},0,2}]}

Comprension Questions

2. Create a 3D model of a yurt, or circular house, which is a cylinder topped by a cone.

3. Take your model from Question 2 and insert it in a Manipulate command that modifies the height and radius of the yurt.

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 Questions

4. 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]]}]

Rotate.

There are 3D versions of some of the other commands too. Rotate now needs to know the axis of rotation.

{Manipulate[Graphics3D[{Tube[{{0,0,-.4},{0,0,1.4}},.05],Rotate[Cuboid[],theta,{0,0,1}]},PlotRange->{{-2,2},{-2,2},{-0.5,1.5}}],{{theta,Pi/6},0,2Pi}],Manipulate[Graphics3D[{Tube[{{-.4,-.4,-.4},{1.4,1.4,1.4}},.05],Rotate[Cuboid[],theta,{1,1,1}]},PlotRange->{{-1,2},{-1,2},{-1,2}}],{{theta,Pi/6},0,2Pi}]}

Comprehension Questions

5. Create a baseball bat using Tube.  Use a Manipulate command to rotate it like someone is swinging it.

Plot3D

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]

ParametricPlot3D

ParametricPlot3D is used for two types of plots.  It will create one-dimensional curves through 3D if you supply one varying parameter.  It will create two-dimensional surfaces if you supply two varying parameters.  This is useful when the surface you want to understand is not the graph of some function z=f(x,y).

The Documentation Center shows how to apply a Tube to give a 1D spiral some girth.

{
ParametricPlot3D[{Sin[u],Cos[u],u/10},{u,0,20},PlotRange->All],ParametricPlot3D[{Sin[u],Cos[u],u/10},{u,0,20},PlotStyle->Orange,PlotRange->All]/.Line[pts_,rest___]:>Tube[pts,0.1,rest]
}

Again from the Documentation Center:

ParametricPlot3D[{8+(3+Cos[v]) Cos[u],3+Sin[v],4+(3+Cos[v]) Sin[u]},{u,0,2Pi},{v,0,2Pi},PlotStyle->Green,Mesh->None,PlotPoints->30]

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

Comprehension Questions

6. Create a Manipulate command that draws the parametric curve y=(sin(t),cos(t),1/t) from t=2 to t=30 and moves a point (sphere) that travels along the curve.

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

7. Create a Manipulate command that draws the surface z=x^2+y^2 and moves a point (sphere) along the surface in all directions.

RegionPlot3D

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]

Comprehension Questions

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

Solutions to selected questions

Question 1

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 4

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]]}]