Plane Sections of 3-D Pyramids and Prisms

By:  Joyce McGiles

Drawing Pyramids

Have trouble drawing 3D figures? This will help you visualize.

Let's say you want a triangular pyramid.  All you need to do is give the base points in appropriate order and the apex of the pyramid.

Specify the base and apex of a pyramid

Clear[h,bpts]
apx = {4,3,9};
bpts = {{0,3,2},{6,5,2},{4,0,2}};

Mathematica will plot a pyramid with the base and apex specified.
Simply highlight the blank cell immediately below:

Clear[pyr]
pyr[basepts_,apex_]:=
 Append[
 Table[Line[{apex,basepts[[i]],basepts[[i+1]],apex}],
 {i,1,Length[basepts]-1}],Line[{apex,First[basepts],
 Last[basepts],apex}]];
 
 pyr2=pyr[bpts,apx];
 
 Show[Graphics3D[pyr2],Boxed-> False];

Notice that all base pts have equal z values, this will become
vital as we move to more complex bases.
      bpts = {{0,3,2},{6,5,2},{4,0,2}};
Try one on your own, enter a new apex and base points-- you
can enter as many base points as you want!
Wow!  Much easier than drawing by hand and notice the 
variety of pyramids you can draw.

Drawing Prisms

Now let's switch to prisms, all you need to do is enter
the base points of the bottom base in order and choose a
height for your prism

Note:  these will all be right prisms with equal z values
       for the base points

Specify the base and height of a prism

Clear[h,bpts]
h = 7;
bpts = {{0,0,0},{7,0,0},{7,7,0},{0,7,0}};

Mathematica will do the rest.
Simply highlight the blank cell immediately below:

Clear[k,basepts,toppts,con,tbase,tpts,f,x,y,z,poly,pts]
poly[pts_]:=
  Append[Table[Line[{pts[[i]],pts[[i+1]]}],
  {i,1,Length[pts]-1}],
  Line[{First[pts],Last[pts]}]]

bbase=poly[bpts];
f[{x_,y_,z_}] = {x,y,z}+{0,0,h};
tpts = Map[f,bpts];
tbase = poly[tpts];
con[basepts_,toppts_] :=
Table[Line[{basepts[[k]],toppts[[k]]}],{k,1,Length[bpts]}]
sides=con[bpts,tpts];

Show[Graphics3D[{bbase,tbase,sides}],Boxed->False];

Wanta try?!  Sure, go ahead and change base points 
remember they must have the same z-values  and be in order
Also, choose any height you want!
Pretty neat-- I bet you could do a more interesting base!  
Try again!

Plane Sections in Pyramids

Let's draw plane sections parallel to the base of a pyramid now.  We will need a pyramid to start with, let's use this one.

Clear[h,bpts]
apx = {8,9,10};
bpts = {{0,3,2},{6,5,2},{4,0,2},{1,-2,2}};

Simply highlight the blank cell immediately below to see your pyramid:

Clear[pyr]
pyr[basepts_,apex_]:=
 Append[
 Table[Line[{apex,basepts[[i]],basepts[[i+1]],apex}],
 {i,1,Length[basepts]-1}],Line[{apex,First[basepts],
 Last[basepts],apex}]];
 
 pyr2=pyr[bpts,apx];
 
 Show[Graphics3D[pyr2],Boxed-> False];

Now let's draw a plane section parallel to the base.

You will need to choose a value for k, the ratio of the length 
from the apex to the plane and the length of the plane to
the base.  Suppose you choose k=0.5 this would give the plane
half way between the apex and base.

Clear[k]
 k= 0.50;

Simply highlight the blank cell immediately below:

Clear[mdpt]
 mdpt[{x_,y_,z_}]:= k {x,y,z} + (1-k) apx;
 midplane:=Map[mdpt,bpts];
cotf = Table[First[Sort[Append[
Table[bpts[[w,c]],{w,1,Length[bpts]}],
apx[[c]]]]],{c,1,3}];
cotl = Table[Last[Sort[Append[
Table[bpts[[w,c]],{w,1,Length[bpts]}],
apx[[c]]]]],{c,1,3}];
Show[Graphics3D[{Polygon[midplane],pyr2}],Boxed->False,
    PlotRange->{{cotf[[1]]-.5,cotl[[1]]+.5},
    {cotf[[2]]-.5,cotl[[2]]+.5},
{cotf[[3]]-.5,cotl[[3]]+.5}},
     AspectRatio->Automatic];

Neat, now you can enter in your own points and execute the code to see what your pyramid and plane section will look like.  Try 
several different ones to see the variety of things you can do.

Plane Sections in Prisms

Now let's find parallel plane sections of prisms.
 
We will need to have a prism, for now let's use the one defined below.  We will need to enter a value t that tells how many units above the bottom base the plane will be.

Clear[h,bpts,t]
h = 5;
bpts = {{0,0,0},{3.5,-1.5,0},{7,0,0},{7,3,0},{3.5,4.5,0},
        {0,3,0}};
t = 2;

To see the prism execute the cell above and the blank
cell below.

Clear[k,basepts,toppts,con,tbase,tpts,f,x,y,z,poly,pts]
poly[pts_]:=
  Append[Table[Line[{pts[[i]],pts[[i+1]]}],
  {i,1,Length[pts]-1}],
  Line[{First[pts],Last[pts]}]]

bbase=poly[bpts];
f[{x_,y_,z_}] = {x,y,z}+{0,0,h};
tpts = Map[f,bpts];
tbase = poly[tpts];
con[basepts_,toppts_] :=
Table[Line[{basepts[[k]],toppts[[k]]}],{k,1,Length[bpts]}]
sides=con[bpts,tpts];

Show[Graphics3D[{bbase,tbase,sides}],Boxed->False,
     ViewPoint->{1.300, -2.400, 2.000}];

Good, now let's look at the prism and plane section
with the given t from above.
Simply highlight the blank cell immediately below:

Clear[p,x,y,z,planepts]

p[{x_,y_,z_}]={x,y,z}+{0,0,t};
planepts = Map[p,bpts];
Show[Graphics3D[{bbase,tbase,sides,Polygon[planepts]}],
     Boxed->False];

You can now try this with different t-values or you can 
enter in your own base points, height and t-value.  Just
keep in mind that you need to have the z-values in your
base points the same and in order when you enter them.

Animation of the Plane Section

How would the plane section look in different areas?  You
can use animation to show the plane section in different 
places on the same figure. The following code combined with the
code for the prism and plane section code in the Plane Sections of 
Prisms  section will move the plane sections by 0.5 units each time
for the height of your prism. To try this example simply highlight the far right cell next to the graph below by clicking once and then press command-y to animate.  You can adjust the speed and direction using the keys in the bottom left of the screen.
If you enter in a different base in the previous section you must first run the code listed below before you can animate.

Clear[p,x,y,z,planepts]
Do[
p[{x_,y_,z_}]={x,y,z}+{0,0,t};
planepts = Map[p,bpts];
Show[Graphics3D[{bbase,tbase,sides,Polygon[planepts]}],
     Boxed->False],{t,0,h,0.5}];