我正在使用 TikZ 创建一些 3D 图形,我需要将变换矩阵应用于我在 2D 中指定的一些坐标,以便它们最终变成 3D。我将在立方体的所有三个可见面上绘制图形,并且我定义了在 2D 中工作的宏,我希望能够在这些面上使用它们。有什么办法吗?我看到 PGF 中有一些原始变换,但它们都在 2D 中工作。
答案1
这个怎么样:
\documentclass[parskip]{scrartcl}
\usepackage[margin=15mm]{geometry}
\usepackage{tikz}
\usetikzlibrary{3d}
\begin{document}
\tikzset{xzplane/.style={canvas is xz plane at y=#1,very thin}}
\tikzset{yzplane/.style={canvas is yz plane at x=#1,very thin}}
\tikzset{xyplane/.style={canvas is xy plane at z=#1,very thin}}
\begin{tikzpicture}[x={(0.985cm,-0.174cm)},y={(-0.906cm,-0.423cm)},z={(0cm,1cm)}]
\draw[xzplane=5] (0,0) -- (5,0) -- (5,5) -- (0,5) -- cycle;
\draw[yzplane=5] (0,0) -- (5,0) -- (5,5) -- (0,5) -- cycle;
\draw[xyplane=5] (0,0) -- (5,0) -- (5,5) -- (0,5) -- cycle;
\foreach \x in {xzplane,yzplane,xyplane}
{ \draw[\x=5] (1,1) -- (4,1) -- (2.5,4) -- cycle;
\filldraw[\x=5,red,fill opacity=0.2,draw=black] (1.5,1.5) circle (0.5);
\filldraw[\x=5,green,fill opacity=0.2,draw=black] (3.5,1.5) circle (0.5);
\filldraw[\x=5,blue,fill opacity=0.2,draw=black] (1.5,3.5) circle (0.5);
\filldraw[\x=5,yellow,fill opacity=0.2,draw=black] (3.5,3.5) circle (0.5);
}
\end{tikzpicture}
\end{document}
答案2
这是一种方法,但通常需要旋转才能使文本“正确”。
需要“3d”TikZ 库。
像往常一样将其放在 LaTeX 文件的序言中
% We need an origin and two ORTHOGONAL vectors spanning the plane, defined
% by their END POINTS with respect to the chosen origin
% For the plane ax+by+cz+d=0, put for instance
% \def\tikz@plane@origin{\pgfpointxyz{-d/a}{0}{0}}%
% \def\tikz@plane@x{\pgfpointxyz{-d/a-b/a}{1}{0}}%
% \def\tikz@plane@y{\pgfpointxyz{-d/a-1}{-b/a}{(a*a+b*b)/(a*c)}}%
% For instance the [111] plane, i.e. x+y+z-1=0, named my_plane
\makeatletter
\tikzoption{canvas is my_plane}[]{%
\def\tikz@plane@origin{\pgfpointxyz{1}{0}{0}}%
\def\tikz@plane@x{\pgfpointxyz{0}{1}{0}}%
\def\tikz@plane@y{\pgfpointxyz{0}{-1}{2}}%
\tikz@canvas@is@plane
}
\makeatother
然后在你的 tikzpicture 环境中,类似
\begin{scope}[every node/.append style={transform shape},
canvas is my_plane]
\node at (A){$A$};
\end{scope}
我们可能可以使用参数进一步定制它。希望它能有所帮助。