\documentclass[12pt]{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.15}
\usepackage{tikz, tkz-euclide,tikz-3dplot,ifthen}
\usetikzlibrary{arrows,decorations.markings,calc,fadings,decorations.pathreplacing, patterns, decorations.pathmorphing, positioning}
\usepgfplotslibrary{fillbetween}
\begin{document}
\centering
\begin{tikzpicture}
\begin{axis}[
axis lines=middle,
view={135}{30},
ticks=none,
zmin=0,zmax=5,
xmin=-3,xmax=3,
ymin=-3,ymax=5,
axis line style={draw=none},
style={draw=none}
]
\draw[fill=gray!50,opacity=0.5](2,0,4)--(0,0,4)--(0,3,4)--(2,3,4)--cycle;
\draw[fill=gray!50,opacity=0.5](2,3,4)--(0,3,4)--(0,3,0)--(2,3,0)--cycle;
\draw[fill=gray!50,opacity=0.5](2,0,0)--(2,0,4)--(2,3,4)--(2,3,0)--cycle;
\draw[dashed](0,0,0)--(0,0,4);
\draw[-latex](0,0,4)--(0,0,5)node[right]{$z$};
\draw(-2.3,0,0)--(-3,0,0);
\draw[dashed](2,0,0)--(-2.3,0,0);
\draw[-latex](2,0,0)--(3,0,0)node[above]{$x$};
\draw(0,-2.65,0)--(0,-4,0);
\draw[dashed](0,-2.65,0)--(0,3,0);
\draw[-latex](0,3,0)--(0,5,0)node[above]{$y$};
\end{axis}
\end{tikzpicture}
\end{document}
我想知道是否有一种方法可以让虚线在表面后面时自动绘制。我是否应该找到这些线并像上面一样手动绘制它们?
答案1
您可以使用3dtools
这里。非常感谢marmot对这个答案的支持。
\documentclass[tikz,border=3mm]{standalone}
\usetikzlibrary{calc,3dtools}% https://github.com/marmotghost/tikz-3dtools
\begin{document}
\pgfdeclarelayer{background}
\pgfdeclarelayer{foreground}
\pgfsetlayers{background,main,foreground}
\foreach \Angle in {60}
{\begin{tikzpicture}[line cap=round,line join=round,declare function={a=3;h=5;}, c/.style={circle,fill,inner sep=1pt},
3d/install view={phi=\Angle,psi=0,theta=70},
same bounding box=A]
\path
(0,0,0) coordinate (O)
(a,0,0) coordinate (A)
(a,a,0) coordinate (B)
(0,a,0) coordinate (C)
(0,0,h) coordinate (O')
(a,0,h) coordinate (A')
(a,a,h) coordinate (B')
(0,a,h) coordinate (C')
(a/2,a/2,h/2) coordinate (T);
\tikzset{3d/polyhedron/.cd,O={(T)},
fore layer=foreground,back layer=background,
fore/.append style={fill=gray!50,opacity=0.5,thick},
back/.append style={3d/polyhedron/complete dashes,fill=gray!50,opacity=0.5},
%edges have complete dashes,
draw face with corners={{(O)},{(A)},{(B)},{(C)}},
draw face with corners={{(O')},{(A')},{(B')},{(C')}},
draw face with corners={{(A)},{(B)},{(B')},{(A')}},
draw face with corners={{(O)},{(C)},{(C')},{(O')}},
draw face with corners={{(O)},{(A)},{(A')},{(O')}},
draw face with corners={{(B)},{(C)},{(C')},{(B')}}
}
%\path foreach \p/\g in {A/-90,B/-90,C/-90,O/-90}{(\p)node[c]{}+(\g:2.5mm) node{$\p$}};
\path[save named path=hull,convex hull of={O,A,B,C,O',A',B',C'}];
\path[save named path=lx] (A) -- (a+2,0,0) node [pos=1.15] {$x$};
\path[save named path=ly] (C) -- (0,a+1,0) node [pos=1.15] {$y$};
\path[save named path=lz] (O') -- (0,0,h+1) node [pos=1.15] {$z$};
\tikzset{3d/ordered paths/.cd,
hull/.style={draw=none},
lx/.style={-latex},ly/.style={-latex},lz/.style={-latex}}
\foreach \Dir/\Line in {{1,0,0}/x,{0,1,0}/y,{0,0,1}/z}
{
\pgfmathtruncatemacro{\itest}{screendepth(\Dir)<0?0:1}
\ifnum\itest=0
\tikzset{3d/draw ordered paths={l\Line,hull}}
\else
\tikzset{3d/draw ordered paths={hull,l\Line}}
\fi}
\end{tikzpicture}}
\end{document}
或者你可以使用
\documentclass[tikz,border=3mm]{standalone}
\usetikzlibrary{calc,3dtools}% https://github.com/marmotghost/tikz-3dtools
\begin{document}
\pgfdeclarelayer{background}
\pgfdeclarelayer{foreground}
\pgfsetlayers{background,main,foreground}
\foreach \Angle in {10,20,...,350}
{\begin{tikzpicture}[line cap=round,line join=round,declare function={a=3;h=5;}, c/.style={circle,fill,inner sep=1pt},
3d/install view={phi=\Angle,psi=0,theta=70},
same bounding box=A]
\path
(0,0,0) coordinate (O)
(a,0,0) coordinate (A)
(a,a,0) coordinate (B)
(0,a,0) coordinate (C)
(0,0,h) coordinate (O')
(a,0,h) coordinate (A')
(a,a,h) coordinate (B')
(0,a,h) coordinate (C')
(a/2,a/2,h/2) coordinate (T);
\tikzset{3d/polyhedron/.cd,O={(T)},
fore layer=foreground,back layer=background,
fore/.append style={fill=gray!50,opacity=0.5,thick},
back/.append style={3d/polyhedron/complete dashes,fill=gray!50,opacity=0.5},
%edges have complete dashes,
draw face with corners={{(O)},{(A)},{(B)},{(C)}},
draw face with corners={{(O')},{(A')},{(B')},{(C')}},
draw face with corners={{(A)},{(B)},{(B')},{(A')}},
draw face with corners={{(O)},{(C)},{(C')},{(O')}},
draw face with corners={{(O)},{(A)},{(A')},{(O')}},
draw face with corners={{(B)},{(C)},{(C')},{(B')}}
}
%\path foreach \p/\g in {A/-90,B/-90,C/-90,O/-90}{(\p)node[c]{}+(\g:2.5mm) node{$\p$}};
\path[save named path=hull,convex hull of={O,A,B,C,O',A',B',C'}];
\path[save named path=lx] (A) -- (a+2,0,0) node [pos=1.15] {$x$};
\path[save named path=ly] (C) -- (0,a+1,0) node [pos=1.15] {$y$};
\path[save named path=lz] (O') -- (0,0,h+1) node [pos=1.15] {$z$};
\tikzset{3d/ordered paths/.cd,
hull/.style={draw=none},
lx/.style={-latex},ly/.style={-latex},lz/.style={-latex}}
\foreach \Dir/\Line in {{1,0,0}/x,{0,1,0}/y,{0,0,1}/z}
{
\pgfmathtruncatemacro{\itest}{screendepth(\Dir)<0?0:1}
\ifnum\itest=0
\tikzset{3d/draw ordered paths={l\Line,hull}}
\else
\tikzset{3d/draw ordered paths={hull,l\Line}}
\fi}
\end{tikzpicture}}
\end{document}
或者更长的轴
\documentclass[tikz,border=3mm]{standalone}
\usetikzlibrary{calc,3dtools}% https://github.com/marmotghost/tikz-3dtools
\begin{document}
\pgfdeclarelayer{background}
\pgfdeclarelayer{foreground}
\pgfsetlayers{background,main,foreground}
\foreach \Angle in {10,20,...,350}
{\begin{tikzpicture}[line cap=round,line join=round,declare function={a=3;h=5;}, c/.style={circle,fill,inner sep=1pt},
3d/install view={phi=\Angle,psi=0,theta=70},
same bounding box=A]
\path
(0,0,0) coordinate (O)
(a,0,0) coordinate (A)
(a,a,0) coordinate (B)
(0,a,0) coordinate (C)
(0,0,h) coordinate (O')
(a,0,h) coordinate (A')
(a,a,h) coordinate (B')
(0,a,h) coordinate (C')
(a/2,a/2,h/2) coordinate (T);
\tikzset{3d/polyhedron/.cd,O={(T)},
fore layer=foreground,back layer=background,
fore/.append style={fill=gray!50,opacity=0.5,thick},
back/.append style={3d/polyhedron/complete dashes,fill=gray!50,opacity=0.5},
%edges have complete dashes,
draw face with corners={{(O)},{(A)},{(B)},{(C)}},
draw face with corners={{(O')},{(A')},{(B')},{(C')}},
draw face with corners={{(A)},{(B)},{(B')},{(A')}},
draw face with corners={{(O)},{(C)},{(C')},{(O')}},
draw face with corners={{(O)},{(A)},{(A')},{(O')}},
draw face with corners={{(B)},{(C)},{(C')},{(B')}}
}
%\path foreach \p/\g in {A/-90,B/-90,C/-90,O/-90}{(\p)node[c]{}+(\g:2.5mm) node{$\p$}};
\path[save named path=hull,convex hull of={O,A,B,C,O',A',B',C'}];
\path[save named path=lx] (A) -- (a+2,0,0) node [pos=1.15] {$x$};
\path[save named path=lx'] (O) -- (-a-2,0,0);
\path[save named path=ly] (C) -- (0,a+1,0) node [pos=1.15] {$y$};
\path[save named path=ly'] (O) -- (0,-a-2,0);
\path[save named path=lz] (O') -- (0,0,h+1) node [pos=1.15] {$z$};
\path[save named path=lz'] (O) -- (O);
\tikzset{3d/ordered paths/.cd,
hull/.style={draw=none},
lx/.style={-latex},ly/.style={-latex},lz/.style={-latex}}
\foreach \Dir/\Line in {{1,0,0}/x,{0,1,0}/y,{0,0,1}/z}
{
\pgfmathtruncatemacro{\itest}{screendepth(\Dir)<0?0:1}
\ifnum\itest=0
\tikzset{3d/draw ordered paths={l\Line,hull,l\Line'}}
\else
\tikzset{3d/draw ordered paths={l\Line',hull,l\Line}}
\fi}
\end{tikzpicture}}
\end{document}