# Making a 3D colored pyramid in tikz

I am trying to recreate the following image in tikz

Sorry for not including a MWE, but I have problems getting started drawing this. I tried simply writing in the coordinates, but even that turned out wrong. (The point in the middle should have coordinates (2,3,0))

Does anyone have any hints or suggestions? My main problem is setting it up, and perhaps the coloring Here is a very crappy MWE... Did not include it before, because it is so bad

\documentclass[10pt,a4paper]{minimal}
\usepackage{tikz}
\begin{document}
\center
\begin{tikzpicture}
\draw[thick,-stealth] (0,0,0)--(0,0,6);
\draw[thick,-stealth] (0,0,0)--(0,6,0);
\draw[-stealth] (0,0,0)--(6,0,0);
\coordinate[label=$O$] (O) at (0,0,0);
\coordinate[label=$A1$] (A1) at (0,0,5);
\coordinate[label=$A2$] (A2) at (0,5,0);
\coordinate[label=$A3$] (A3) at (5,0,0);
\coordinate[label=$B1$] (B1) at (3,0,0);
\coordinate[label=$B2$] (B2) at (0,0,2);
\coordinate[label=$B3$] (B3) at (3,0,2);
\coordinate[label=$C2$] (C2) at (0,3,2);
\coordinate[label=$C3$] (C3) at (3,2,0);

\draw[fill=blue,opacity=0.3] (A1)--(C2)--(B3);
draw[fill=black,opacity=0.3] (O)--(B2)--(B3)--(B1)
\draw[fill=red,opacity=0.3] (A3)--(C3)--(B3);
\draw[fill=red,opacity=0.3] (B3)--(B1)--(C3);
\draw[fill=blue,opacity=0.3] (C2)--(B2)--(B3);
\draw[fill=brown,opacity=0.3] (B3)--(C2)--(A2)--(C3);
\end{tikzpicture}
\end{document}


-
Even if you example doesn’t compile it’s better to show what you tried so far … If the coordinates for the two blue points are right, then the “point in the middle“ of the x-y-plane (the one without a mark on it) can’t be (2,3,0). The filling can be done with transparency, I’d start with drawing the lines and then the filling. Maybe the intersections library can help you too. –  Tobi Mar 3 '12 at 22:29
Related: tex.stackexchange.com/questions/17204/… There was another similar question as well, but I couldn't find it now. –  Torbjørn T. Mar 3 '12 at 22:50
Something like this? –  N3buchadnezzar Mar 3 '12 at 22:55
Short question, how do I make the brown part look more 3D? (the labels are just there for clearification) and why is the x axis upwards? It is really confusing! –  N3buchadnezzar Mar 3 '12 at 23:23

Picture

Code

The first macro are used to set up the perspective. All the macros are not used in this case.

\documentclass[10pt,a4paper]{minimal}
\usepackage{tikz}
\begin{document}

\newcommand\pgfmathsinandcos[3]{%
\pgfmathsetmacro#1{sin(#3)}%
\pgfmathsetmacro#2{cos(#3)}%
}

\begin{tikzpicture}[scale=1.5]

\pgfmathsetmacro\AngleFuite{135}
\pgfmathsetmacro\coeffReduc{.8}
\pgfmathsetmacro\clen{2}
\pgfmathsinandcos\sint\cost{\AngleFuite}

\begin{scope} [x     = {(\coeffReduc*\cost,-\coeffReduc*\sint)},
y     = {(1cm,0cm)},
z     = {(0cm,1cm)}]

\path coordinate (O) at (0,0,0)
coordinate (B) at (0,5,0)
coordinate (A) at (5,0,0)
coordinate (S) at (0,0,5)
coordinate (O) at (0,0,0)
coordinate (I) at (2,0,3) coordinate (PI) at (2,0,0)
coordinate (J) at (0,3,2) coordinate (PJ) at (0,3,0)
coordinate (K) at (2,3,0);

\draw[thick,-stealth,blue]  (O)  -- (0,0,6);
\draw[thick,-stealth,red]   (O)  -- (6,0,0);
\draw[thick,-stealth,green] (O)  -- (0,6,0);

\draw (A) -- (B) -- (O) -- (A) -- (S) -- (O)
(B) -- (S);

\fill [blue!20, fill opacity = .5]   (A)--(I)--(K)--cycle;
\fill [blue!20, fill opacity = .5]   (B)--(J)--(K)--cycle;
\fill [brown!40,fill opacity = .5,
draw = brown,ultra thick] (S)--(I)--(PI)--(K) -- (PJ) --(J) --cycle;
\draw [brown,ultra thick] (I)  -- (K)  -- (J)  ;
\draw [blue,ultra thick]  (I)  -- (A)  -- (B) -- (J)  ;
\end{scope}

\foreach \v in {A,B,I,J,S}  \draw[fill=gray] (\v) circle (2pt);
\end{tikzpicture}

\end{document}

-
Accepted because of the vieweing thingy. Why do you not use TikzEuclide? ^^ –  N3buchadnezzar Mar 4 '12 at 0:21
tkz-euclide is for 2d representations. I work on a new package for 3D –  Alain Matthes Mar 4 '12 at 6:14
Very good! I will look forward to it! I use euclide a lot =D –  N3buchadnezzar Mar 4 '12 at 11:07

So I was intrigued by this question and found forth an old solution to one of my figures in my bachelor. Nearly three years time... At that time I had yet to discover the calc library which was not so full of features as it is now! :)

Ok, so i had the same idea of creating a diagram of a setup used to exemplify some calculations. That required some triangles some plane angles and some angle notations.

What, I really wanted was to denote the angle within its plane. This would allow the reader to immediately see which angle it belonged to (ok, so the few angles in this diagram does not justify that, but fun it was!).

Without further ado, here is the code:

% Toy with these to get some feel for alignment
\def\angEl{25} % elevation angle
\def\angAz{-125} % azimuth angle
\begin{tikzpicture}[>=latex,%
% Create the xy-plane
xyplane/.estyle={%
cm={%
cos(\angAz)  , sin(\angAz)*sin(\angEl),%
-sin(\angAz) , cos(\angAz)*sin(\angEl),%
(0,0)%
}%
},
% Create the yz-plane
yzplane/.estyle={%
cm={%
cos(\angAz+90) , sin(\angAz+90)*sin(\angEl),%
0              , cos(\angEl)            ,%
(0,0)%
}%
},
% Create the tilted-plane
tplane/.estyle={%
cm={%
cos(\angAz)             , -sin(\angAz),%
sin(\angAz)*sin(\angEl) , cos(\angAz)*sin(\angEl),%
(0,0)%
}%
}]
% Draw base rectangle, and its notation of current
\draw[xyplane,dashed] (-3,-3) rectangle (3,3);
\draw[xyplane,rounded corners,->] (-2.5,3.5) -| node[above] {$I$} (-3.5,2.5);
\draw[xyplane,rounded corners,->] (2.5,-3.5) -| node[below] {$I$} (3.5,-2.5);
% Denote coordinates
\draw[xyplane,<->] (4,0) node[below] {$y$} -| (0,4) node[right] {$x$};
% Create side lengths
\draw[xyplane,<->] (1.5,3)  -- node[below left=.5ex] {$a$} ++(0,-3);
% Coordinates in the plane
\path[xyplane] (0,3) coordinate (XO);
\path[xyplane] (3,3) coordinate (X1);
\path[xyplane] (-3,3) coordinate (X2);
\draw[xyplane,<->] (-3,3.2) -- node[below right=.5ex] {$a$} (0,3.2);
\draw[xyplane,<->] (3,3.2) -- node[below right=.5ex] {$a$} (0,3.2);
% Create coordinate
\path (0,3) coordinate (X) node[above left] {$P$};
\fill (X) circle (1.5pt);
\fill[fill opacity=.25,color=red] (X) -- (X1) -- (X2) -- cycle;
\draw (X) -- node[pos=0.6,right] {$s$} (XO);
\draw[<->] (-.2,0) -- node[below left=1ex] {$h$} ++(X);
\draw (X) -- (X1) (X) -- (X2);
\draw[->] (0,0) -- (X) -- ++(0,1) node[above] {$z$};
% Draw in the yzplane
\path[yzplane] (3,0)+(180:.5) coordinate (BETA1);
\path[yzplane] (3,0)+(180-48:.5) coordinate (BETA2);
% Create \beta angle, the notation of yzplane in the node makes the text subsequent to the coordinate transformation
\draw[yzplane] (BETA1) to[bend left=46] node[pos=0.5,left,yzplane] {$\beta$} (BETA2);
\draw[yzplane,-stealth] (X) -- ++(90-42:1.3) node[right,yzplane] {$\mathbf B$};
% Create the angle denotation, this could be made more explicit (it is currently just eye-measured)
\path[yzplane] (X)+(90:.5) coordinate (B Top 1)
(X)+(90-42:.5) coordinate (B Top 2);
\draw[yzplane] (B Top 1) to[bend left=46] node[pos=0.5,above,yzplane] {$\beta$} (B Top 2);
\fill[fill opacity=.25,color=black] (X) -- (XO) -- (0,0) -- cycle;
% Create the \theta angles, these are also approximated by the eye.
% It was easier at the time... :)
\path[tplane] (X)+(-.5,-.485) coordinate (Right);
\draw[tplane] (X)+(-.5,.577) to[out=-160,in=180] (Right);
% We need large as it is so much transformed that it looks smaller
\draw[tplane] (X)+(-1,.5) node[tplane,rotate=-90] {\large$\theta_1$};
\draw[tplane] (X)+(-1,-.5) node[tplane,rotate=-90] {\large$\theta_2$};
\end{tikzpicture}


Some of these could be made shorter with the help of calc, however not very much shorter. In order to get a transformation of text simply add the corresponding cm to that node description. But one should be weary about crude transformations as is noticed for the \theta angles, there I had to enlargen the font or it would look too small.

Notice in the output that, both \beta's, \theta_i's and the \mathrm B is transformed to its plane.

The output finally yields:

-
Fine result, very interesting –  Alain Matthes May 9 '12 at 12:56

If you want to have the z axis vertical, you can change the coordinate system of the image. x={(a,b)} as a parameter to the tikzpicture defines the unit vector for the xcoordinate in the image. Remember that TikZ works in 2D only, so the standard z component is just a projection (or something) in the xy plane.

The unit vectors in the example below could probably be fine tuned, but you'll get the point I think. (I borrowed cmhughes code, and just changed the coordinates.

\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[x={(-5mm,-3.85mm)},z={(0,1cm)},y={(1cm,-.3cm)}]
\draw [->] (0,0) -- (6,0,0) node [right] {$x$};
\draw [->] (0,0) -- (0,6,0) node [above] {$y$};
\draw [->] (0,0) -- (0,0,6) node [below left] {$z$};
\draw[fill=blue,opacity=.5] (0,5,0)--(0,3,3)--(2,3,0);
\draw[fill=blue,opacity=.5] (5,0,0)--(2,0,2)--(2,3,0);
\draw[fill=orange,opacity=.5] (0,3,3)--(0,0,5)--(2,0,2)--(2,3,0);
\draw[fill=red!50,opacity=.5] (2,0,2)--(2,3,0)--(2,0,0);
\draw[fill=red!50,opacity=.5] (0,3,3)--(2,3,0)--(0,3,0);
\end{tikzpicture}
\end{document}

-

Here's a first attempt, using section 70.4 of the pgf manual to guide (posted before MWE was available)

\documentclass{article}
\usepackage{tikz}

\begin{document}
\begin{tikzpicture}
\draw [->] (0,0) -- (6,0,0) node [right] {$x$};
\draw [->] (0,0) -- (0,6,0) node [above] {$y$};
\draw [->] (0,0) -- (0,0,6) node [below left] {$z$};
\draw[fill=blue,opacity=.5] (3,3,0)--(2.5,0,2.5)--(5,0,0) node [above right]{$(0,5,0)$};
\draw[fill=blue,opacity=.5] (0,2,2)--(2.5,0,2.5)--(0,0,5) node [below right]{$(5,0,0)$};
\draw[fill=orange,opacity=.5] (0,2,2)--(2.5,0,2.5)--(3,3,0)--(0,5,0) node[above right]{$(0,5,0)$};
\draw[fill=red!50,opacity=.5] (2.5,0,2.5)--(0,0,2)--(0,2,2) node [above left]{$(2,0,2)$};
\draw[fill=red!50,opacity=.5] (2.5,0,2.5)--(3,0,0)--(3,3,0) node [above right]{$(0,3,3)$};
\end{tikzpicture}
\end{document}

-
Tjhanks, this is very close to my attempt! =) –  N3buchadnezzar Mar 3 '12 at 23:20
@N3buchadnezzar you're welcome. Note that the pyramid looks a little off because I used the coordinates you provided- perhaps this was intentional?... –  cmhughes Mar 3 '12 at 23:23
Yes! I messed up! look at my edit! –  N3buchadnezzar Mar 3 '12 at 23:36
@N3buchadnezzar imho, your original attempt is good- it seems that the only part you might want to change is the viewing angle (which I don't know how to do) –  cmhughes Mar 3 '12 at 23:59

I think an approach with the tikz-3dplot package should not be missing here.

% !TeX program = pdflatex
\documentclass[11pt]{article}
\usepackage[T1]{fontenc}
\usepackage{tikz,tikz-3dplot}

\tdplotsetmaincoords{60}{120}

\begin{document}
\begin{tikzpicture}[>=stealth,tdplot_main_coords]
\coordinate (O) at (0,0,0);
\coordinate[label=above right:{$(0,0,5)$}] (A) at (0,0,5);
\coordinate[label=above left:{$(2,0,2)$}] (B) at (2,0,2);
\coordinate[label=above right:{$(0,3,3)$}] (C) at (0,3,3);
\coordinate[label=above left:{$(5,0,0)$}] (D) at (5,0,0);
\coordinate[label=above right:{$(0,5,0)$}] (E) at (0,5,0);

\draw[->] (O) -- (6,0,0);
\draw[->] (O) -- (0,6,0);
\draw[->] (O) -- (0,0,6);

\filldraw[draw=brown,fill=brown,fill opacity=0.1] (A) -- (B) -- (2,3,0) -- (C) -- cycle;
\filldraw[draw=brown,fill=brown,fill opacity=0.1] (B) -- (2,0,0) -- (2,3,0)-- cycle;
\filldraw[draw=brown,fill=brown,fill opacity=0.1] (C) -- (0,3,0) -- (2,3,0)-- cycle;
\fill[fill=blue,fill opacity=0.1] (B) -- (D) -- (2,3,0)-- cycle;
\fill[fill=blue,fill opacity=0.1] (C) -- (E) -- (2,3,0)-- cycle;
\draw[blue] (B) -- (D) -- (E) -- (C);
\end{tikzpicture}
\end{document}


-
This is not missing ! tikz-3dplot is based on the first commands of my answer like \tdplotsinandcos –  Alain Matthes Mar 17 '12 at 12:39
How am I supposed to know that? –  Thorsten Donig Mar 17 '12 at 13:11
yes it was difficult... it says at the beginning of the documentation. But it's a good idea to cite this package because it facilitates the drawing in 3D. –  Alain Matthes Mar 17 '12 at 14:00