作为练习,我尝试绘制棱柱 [0,2] x [0,4] x [0,6] 与平面 x + y + z = 5 的交点。
我的结果是:
\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\begin{document}
\begin{tikzpicture}[x={(-0.45cm,-0.385cm)},y={(1cm,-0.1cm)},z={(0,1cm)}]
\draw [->] (0,0,0) -- (6,0,0) node [below left] {$x$};
\draw [->] (0,0,0) -- (0,6,0) node [right] {$y$};
\draw [->] (0,0,0) -- (0,0,6) node [right] {$z$};
\filldraw [thick, orange, fill opacity=0.3] (0,0,5) -- (0,4,1) -- (1,4,0) -- (2,3,0) -- (2,0,3) -- cycle;
\filldraw [thick, blue, fill opacity=0.2] (2,3,0) -- (2,0,3) -- (5,0,0) -- cycle;
\filldraw [thick, blue, fill opacity=0.2] (1,4,0) -- (0,5,0) -- (0,4,1) -- cycle;
\filldraw [thick, orange, fill opacity=0.3] (2,3,0) -- (2,0,0) -- (2,0,3) -- cycle;
\filldraw [thick, orange, fill opacity=0.3] (1,4,0) -- (0,4,0) -- (0,4,1) --cycle;
\end{tikzpicture}
\end{document}
我现在有几个问题:
- 我认为有很多代码只是为了将一个简单的数学体积表示为 [0,2] x [0,4] x [0,6]。有没有更有效的方法来绘制它?
- 我是否需要手动计算交点然后将其表示出来?或者有没有直接的方法?
- 如何使用
axis
环境和\addplot
命令而不是 来获得相同的结果\draw
?我试过了,但我是新手\addplot3
,而且我在轴位置 (view={}{}
) 方面遇到了麻烦,colormap
颜色不均匀,表面有一个网格,难以理解图片,我对交叉点有同样的疑问,我需要手动计算它们吗?
全棱镜是:
\draw [fill=orange, fill opacity=0.3] (0,0,6) -- (2,0,6) -- (2,4,6) -- (0,4,6) -- cycle ;
\draw [fill=orange, fill opacity=0.3] (2,0,0) -- (2,0,6) -- (2,4,6) -- (2,4,0) -- cycle ;
\draw [fill=orange, fill opacity=0.3] (2,4,0) -- (0,4,0) -- (0,4,6) -- (2,4,6) -- cycle ;
答案1
不管你做什么,请考虑以更系统的方式安装 3D 视图。实现此目的的最佳方式可能是使用asymptote
,它确实有工具来计算 3D 中的交点。如果你想使用pgfplots
,请使用patch plots
。但是,为此你仍然需要自己计算交点。这篇文章是提到一些实验钛钾Z 库这也允许我们计算3d中的交点。
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{3dtools}%https://github.com/marmotghost/tikz-3dtools
\begin{document}
\pgfdeclarelayer{background}
\pgfdeclarelayer{foreground}
\pgfdeclarelayer{behind}
\pgfsetlayers{behind,background,main,foreground}
\begin{tikzpicture}[>=stealth,
3d/install view={theta=70,phi=110},
line cap=round,line join=round,
visible/.style={draw,thick,solid},
hidden/.style={draw,very thin,cheating dash},
3d/polyhedron/.cd,fore/.style={visible,fill opacity=0.6},
back/.style={fill opacity=0.6,hidden,3d/polyhedron/complete dashes},
fore layer=foreground,
back layer=background
]
\draw [->] (0,0,0) coordinate (O) -- (6,0,0) coordinate (ex) node [below left] {$x$};
\draw [->] (0,0,0) -- (0,6,0) coordinate (ey) node [right] {$y$};
\draw [->] (0,0,0) -- (0,0,6) coordinate (ez) node [right] {$z$};
\path (5,0,0) coordinate (A) (0,5,0) coordinate (B) (0,0,5) coordinate (C)
(2.5,0,0) coordinate (a) (0,3.5,0) coordinate (b) (0,0,2) coordinate (c) ;
\path[3d/.cd,plane with normal={(ex) through (a) named px},
plane with normal={(ey) through (b) named py},
line through={(A) and (B) named lAB},
line through={(A) and (C) named lAC},
line through={(B) and (C) named lBC}];
\path[3d/intersection of={lAB with px}] coordinate (pABx)
[3d/intersection of={lAB with py}] coordinate (pABy)
[3d/intersection of={lAC with px}] coordinate (pACx)
[3d/intersection of={lBC with py}] coordinate (pBCy);
\pgfmathsetmacro{\mybarycenterA}{barycenter("(A),(pABx),(pACx),(a)")}
\pgfmathsetmacro{\mybarycenterB}{barycenter("(B),(pABy),(pBCy),(b)")}
\tikzset{3d/polyhedron/.cd,O={(\mybarycenterA)},color=blue,
draw face with corners={{(A)},{(pABx)},{(pACx)}},
draw face with corners={{(A)},{(pABx)},{(a)}},
draw face with corners={{(A)},{(a)},{(pACx)}},
O={(\mybarycenterB)},
draw face with corners={{(B)},{(pABy)},{(pBCy)}},
draw face with corners={{(B)},{(pABy)},{(b)}},
draw face with corners={{(B)},{(b)},{(pBCy)}},
color=orange,O={(1,1,1)},
draw face with corners={{(pABx)},{(pACx)},{(C)},{(pBCy)},{(pABy)}},
draw face with corners={{(a)},{(pACx)},{(C)},{(O)}},
draw face with corners={{(b)},{(pBCy)},{(C)},{(O)}},
draw face with corners={{(b)},{(pABy)},{(pABx)},{(a)},{(O)}}
}
\end{tikzpicture}
\end{document}
仍然需要付出很多努力。但是,有一个好处:您可以更改视图并仍然获得正确的结果。例如,3d/install view={theta=70,phi=60},
您将获得
当然,对于asymptote
和patch plot
解决方案来说这也是正确的(也许除了自动将隐藏线变为虚线的可能性之外)。