How to draw a pyramid with height(altitude) and slant heigth with TikZ. According to the figure
[Tex/LaTex] How to draw a pyramid with height and slant heigth with TikZ?
tikz-3dplot
Related Solutions
Consider three points on the square, say B
, C
and D
. Without loss of generality we can assume that they have the coordinates
C = (r \cos \phi,r \sin\phi,h)
D = (r \cos (180-\phi),r \sin(180-\phi),h)
B = (r \cos \phi,-r \sin\phi,0)
The conditions that they form a square mean that
CD = BC
where
CD = 2r \cos \phi
BC = \sqrt{(2r \sin\phi)^2+h^2}
This yields
\phi = \acos(h^2/(4r^2))/2
This confirms hpechristiansens numerical result:
CD = 2 r \cos \left(\frac{1}{2} \acos\left(\frac{h^2}{4 r^2}\right)\right)
MWE
\documentclass[border=2mm,12pt,tikz]{standalone}
\usepackage{tikz-3dplot}
\usetikzlibrary{3d,calc,backgrounds,patterns}
\begin{document}
\pgfmathsetmacro{\myr}{3}
\pgfmathsetmacro{\h}{2}
\pgfmathsetmacro{\angA}{acos(\h*\h/(4*\myr*\myr))/2}
\tdplotsetmaincoords{65}{100}
\begin{tikzpicture}[tdplot_main_coords,scale=1,line cap=butt,line join=round]
\begin{scope}[canvas is xy plane at z=0]
\draw[dashed] (\tdplotmainphi:\myr) arc(\tdplotmainphi:\tdplotmainphi+180:\myr);
\coordinate (O) at (0,0);
\coordinate (A) at (\angA:\myr);
\coordinate (B) at (180-\angA:\myr);
\draw[thick] (\tdplotmainphi:\myr) coordinate(BR) arc(\tdplotmainphi:\tdplotmainphi-180:\myr)
coordinate(BL);
\end{scope}
\begin{scope}[canvas is xy plane at z=\h]
\coordinate (O') at (0,0);
\coordinate (C) at (-180+\angA:\myr);
\coordinate (D) at (-\angA:\myr);
\draw[thick] (O') circle[radius=\myr];
\draw [thick](BR) -- (\tdplotmainphi:\myr) (BL) -- (\tdplotmainphi-180:\myr);
\end{scope}
\fill (A) circle[radius=1pt] node[above] {$A$};
\fill (B) circle[radius=1pt] node[above] {$B$};
\fill (C) circle[radius=1pt] node[above] {$C$};
\fill (D) circle[radius=1pt] node[above] {$D$};
\draw[dashed] (C) -- (B) -- (A);
\draw[thick] (A) -- (D) -- (C);
\end{tikzpicture}
\end{document}
You were almost there. Drawing a plane is as simple as saying
\draw[canvas is xy plane at z=2,fill=blue,fill opacity=0.6] (-4,-4) rectangle (4,4);
Other than that you need to draw the parts of the cone below and above the planes separately, which is why I added a macro for the truncated cone, \conetruncfront
. I also replaced the hardcoded 45
with \tdplotmainphi
.
\documentclass[tikz, border=3pt]{standalone}
\usepackage{tikz,tikz-3dplot}
\tdplotsetmaincoords{80}{45}
%% style for surfaces
\tikzset{surface/.style={draw=black, fill=white, fill opacity=.6}}
%% macros to draw back and front of cones
%% optional first argument is styling; others are z, radius, side offset (in degrees)
\newcommand{\coneback}[4][]{
%% start at the correct point on the circle, draw the arc, then draw to the origin of the diagram, then close the path
\draw[canvas is xy plane at z=#2, #1] (\tdplotmainphi-#4:#3)
arc(\tdplotmainphi-#4:\tdplotmainphi+180+#4:#3) -- (O) --cycle;
}
\newcommand{\conefront}[4][]{
\draw[canvas is xy plane at z=#2, #1] (\tdplotmainphi-#4:#3) arc
(\tdplotmainphi-#4:\tdplotmainphi-180+#4:#3) -- (O) --cycle;
}
\newcommand{\conetruncfront}[6][]{
\draw[line join=round,#1] plot[variable=\t,domain=\tdplotmainphi-#4:\tdplotmainphi-180+#4]
({#3*cos(\t)},{#3*sin(\t)},#2)
-- plot[variable=\t,domain=\tdplotmainphi-180-#4:\tdplotmainphi+#4]
({#6*cos(\t)},{#6*sin(\t)},#5)
--cycle;
}
\begin{document}
\begin{tikzpicture}[tdplot_main_coords]
\coordinate (O) at (0,0,0);
%% make sure to draw everything from back to front
\coneback[surface]{-3}{2}{-10}
\draw (0,0,-5) -- (O);
\conefront[surface]{-3}{2}{-10}
\draw[canvas is xy plane at z=-2,fill=green!60!black,fill opacity=0.6] (-4,-4) rectangle (4,4);
\coneback[surface]{-2}{4/3}{-10}
\draw (0,0,-2) -- (O);
\conefront[surface]{-2}{4/3}{-10}
\draw[canvas is xy plane at z=0,fill=blue,fill opacity=0.6] (-4,-4) rectangle (4,4);
\draw[->] (-6,0,0) -- (6,0,0) node[right] {$x$};
\draw[->] (0,-6,0) -- (0,6,0) node[right] {$y$};
\coneback[surface]{3}{2}{10}
\draw[->] (O) -- (0,0,5) node[above] {$z$};
\conefront[surface]{3}{2}{10}
\draw[canvas is xy plane at z=2,fill=purple,fill opacity=0.6] (-4,-4) rectangle (4,4);
\draw[->] (0,0,2) -- (0,0,5) node[above] {$z$};
\conetruncfront[surface]{2}{4/3}{0}{3}{2}
\end{tikzpicture}
\end{document}
However, I'd slightly change things to get
\documentclass[tikz, border=3pt]{standalone}
\usepackage{tikz,tikz-3dplot}
\tdplotsetmaincoords{80}{45}
%% style for surfaces
\tikzset{surface/.style={draw=black, left color=yellow,right color=yellow,middle
color=yellow!60!#1, fill opacity=.6},surface/.default=white}
%% macros to draw back and front of cones
%% optional first argument is styling; others are z, radius, side offset (in degrees)
\newcommand{\coneback}[4][]{
%% start at the correct point on the circle, draw the arc, then draw to the origin of the diagram, then close the path
\draw[canvas is xy plane at z=#2, #1] (\tdplotmainphi-#4:#3)
arc(\tdplotmainphi-#4:\tdplotmainphi+180+#4:#3) -- (O) --cycle;
}
\newcommand{\conefront}[4][]{
\draw[canvas is xy plane at z=#2, #1] (\tdplotmainphi-#4:#3) arc
(\tdplotmainphi-#4:\tdplotmainphi-180+#4:#3) -- (O) --cycle;
}
\newcommand{\conetruncback}[6][]{
\draw[line join=round,#1] plot[variable=\t,domain=\tdplotmainphi-#4:\tdplotmainphi+180+#4]
({#3*cos(\t)},{#3*sin(\t)},#2)
-- plot[variable=\t,domain=\tdplotmainphi+180-#4:\tdplotmainphi+#4]
({#6*cos(\t)},{#6*sin(\t)},#5)
--cycle;
}
\newcommand{\conetruncfront}[6][]{
\draw[line join=round,#1] plot[variable=\t,domain=\tdplotmainphi-#4:\tdplotmainphi-180+#4]
({#3*cos(\t)},{#3*sin(\t)},#2)
-- plot[variable=\t,domain=\tdplotmainphi-180-#4:\tdplotmainphi+#4]
({#6*cos(\t)},{#6*sin(\t)},#5)
--cycle;
}
\begin{document}
\begin{tikzpicture}[tdplot_main_coords]
\coordinate (O) at (0,0,0);
\conetruncback[surface=black]{-2}{4/3}{0}{-3}{2}
\draw (0,0,-5) -- (0,0,-2);
\conetruncfront[surface]{-2}{4/3}{0}{-3}{2}
\draw[canvas is xy plane at z=-2,fill=green!60!black,fill opacity=0.6] (-4,-4) rectangle (4,4);
\coneback[surface=black]{-2}{4/3}{-10}
\draw (0,0,-2) -- (O);
\conefront[surface]{-2}{4/3}{-10}
\draw[canvas is xy plane at z=0,fill=blue,fill opacity=0.6] (-4,-4) rectangle (4,4);
\draw[->] (-6,0,0) -- (6,0,0) node[right] {$x$};
\draw[->] (0,-6,0) -- (0,6,0) node[right] {$y$};
\coneback[surface=white]{2}{4/3}{10}
\draw[-] (O) -- (0,0,2);
\conefront[surface=black]{2}{4/3}{10}
\draw[canvas is xy plane at z=2,fill=purple,fill opacity=0.6] (-4,-4) rectangle (4,4);
\conetruncback[surface=white]{2}{4/3}{0}{3}{2}
\draw[->] (0,0,2) -- (0,0,5) node[above] {$z$};
\conetruncfront[surface=black]{2}{4/3}{0}{3}{2}
\end{tikzpicture}
\end{document}
Or with slightly different view angles and opacity set to 1, and adjustments suggested by minhthien_2016.
\documentclass[tikz, border=3pt]{standalone}
\usepackage{tikz,tikz-3dplot}
\tdplotsetmaincoords{80}{60}
%% style for surfaces
\tikzset{surface/.style={draw=black, left color=yellow,right color=yellow,middle
color=yellow!60!#1, fill opacity=1},surface/.default=white}
%% macros to draw back and front of cones
%% optional first argument is styling; others are z, radius, side offset (in degrees)
\newcommand{\coneback}[4][]{
%% start at the correct point on the circle, draw the arc, then draw to the origin of the diagram, then close the path
\draw[canvas is xy plane at z=#2, #1] (\tdplotmainphi-#4:#3)
arc(\tdplotmainphi-#4:\tdplotmainphi+180+#4:#3) -- (O) --cycle;
}
\newcommand{\conefront}[4][]{
\draw[canvas is xy plane at z=#2, #1] (\tdplotmainphi-#4:#3) arc
(\tdplotmainphi-#4:\tdplotmainphi-180+#4:#3) -- (O) --cycle;
}
\newcommand{\conetruncback}[7][]{
\draw[line join=round,#1] plot[variable=\t,domain=\tdplotmainphi-#4:\tdplotmainphi+180+#4]
({#3*cos(\t)},{#3*sin(\t)},#2)
-- plot[variable=\t,domain=\tdplotmainphi+180-#7:\tdplotmainphi+#7]
({#6*cos(\t)},{#6*sin(\t)},#5)
--cycle;
}
\newcommand{\conetruncfront}[7][]{
\draw[line join=round,#1] plot[variable=\t,domain=\tdplotmainphi-#4:\tdplotmainphi-180+#4]
({#3*cos(\t)},{#3*sin(\t)},#2)
-- plot[variable=\t,domain=\tdplotmainphi-180-#7:\tdplotmainphi+#7]
({#6*cos(\t)},{#6*sin(\t)},#5)
--cycle;
}
\begin{document}
\begin{tikzpicture}[tdplot_main_coords]
\coordinate (O) at (0,0,0);
\conetruncback[surface=black]{-2}{4/3}{-5}{-3}{2}{5}
\draw (0,0,-5) -- (0,0,-2);
\conetruncfront[surface]{-2}{4/3}{-5}{-3}{2}{5}
\draw[canvas is xy plane at z=-2,fill=green!60!black,fill opacity=1] (-4,-4) rectangle (4,4);
\coneback[surface=black]{-2}{4/3}{-10}
\draw (0,0,-2) -- (O);
\conefront[surface]{-2}{4/3}{-10}
\draw[canvas is xy plane at z=0,fill=blue,fill opacity=1] (-4,-4) rectangle (4,4);
\draw[->] (-6,0,0) -- (6,0,0) node[right] {$x$};
\draw[->] (0,-6,0) -- (0,6,0) node[right] {$y$};
\coneback[surface=white]{2}{4/3}{10}
\draw[-] (O) -- (0,0,2);
\conefront[surface=black]{2}{4/3}{10}
\draw[canvas is xy plane at z=2,fill=purple,fill opacity=1] (-4,-4) rectangle (4,4);
\conetruncback[surface=white]{2}{4/3}{5}{3}{2}{-5}
\draw[->] (0,0,2) -- (0,0,5) node[above] {$z$};
\conetruncfront[surface=black]{2}{4/3}{5}{3}{2}{-5}
\end{tikzpicture}
\end{document}
Best Answer
You can see at the link Pyramid Commutative Diagram for Cones or you can use
sketch-lib
**
http://alexdu.github.io/sketch-lib/
** or Drawing polyhedra using TikZ with semi-transparent and shading effect.