我想将 3D 长方体的 2D 表面或面之一投影到平面上,如下图所示。请帮帮我。
\documentclass[border=5pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{quotes,arrows.meta}
\begin{document}
\begin{tikzpicture}[every edge quotes/.append style={auto, ultra thick, text=black}]
\pgfmathsetmacro{\cubex}{5}
\pgfmathsetmacro{\cubey}{5}
\pgfmathsetmacro{\cubez}{5}
\draw [draw=black, every edge/.append style={draw=black, thick, densely dashed,
opacity=.25},fill=blue!25,opacity=0.8]
(0,0,0) coordinate (o) -- ++(-\cubex,0,0) coordinate (a) -- ++(0,-\cubey,0) coordinate (b) edge
coordinate [pos=1] (g) ++(0,0,-\cubez) -- ++(\cubex,0,0) coordinate (c) -- cycle
(o) -- ++(0,0,-\cubez) coordinate (d) -- ++(0,-\cubey,0) coordinate (e) edge (g) -- (c) -- cycle
(o) -- (a) -- ++(0,0,-\cubez) coordinate (f) edge (g) -- (d) -- cycle;
\end{tikzpicture}
\begin{tikzpicture}
\draw (0,0) -- (5,5) -- (5,0) -- (0,-5) -- (0,0);
\end{tikzpicture}
\end{document}
答案1
如果你已经正确安装了坐标系,你可以使用
canvas is <planex planey> plane at <orth>=<coord>
来自3d
库。这里<planex planey>
有类似xy
或 的东西zx
,并且<orth>
表示正交方向(即,对于xy
它是z
,对于zx
它是y
等等),并且coord
是沿正交方向的平面坐标。如果你正在使用(诚然有点过度了)回答你上一个问题,添加内容很简单,只需说
\begin{scope}[canvas is xz plane at y=\cubey/2]
...
\end{scope}
但显然你不知道这个答案的所有细节,例如一张来自3d view
图书馆的图片perspective
就可以了。
\documentclass[tikz,border=5pt]{standalone}
\usepackage{tikzlings}
\usetikzlibrary{perspective,3d,fpu}
\makeatletter
\pgfmathdeclarefunction{screendepth}{3}{%
\begingroup%
\pgfkeys{/pgf/fpu,/pgf/fpu/output format=fixed}%
\pgfmathparse{%
((\the\pgf@yx/1cm)*(\the\pgf@zy/1cm)-(\the\pgf@yy/1cm)*(\the\pgf@zx/1cm))*(#1)+
((\the\pgf@zx/1cm)*(\the\pgf@xy/1cm)-(\the\pgf@xx/1cm)*(\the\pgf@zy/1cm))*(#2)+
((\the\pgf@xx/1cm)*(\the\pgf@yy/1cm)-(\the\pgf@yx/1cm)*(\the\pgf@xy/1cm))*(#3)}%
\pgfmathsmuggle\pgfmathresult\endgroup%
}%
\pgfmathdeclarefunction{totalthree}{3}{%
\pgfmathparse{#1+#2+#3}}
\pgfmathdeclarefunction{direction}{3}{%
\begingroup%
\pgfmathparse{int(#1==0)}%
\ifnum\pgfmathresult=1
\pgfmathparse{int(#2==0)}%
\ifnum\pgfmathresult=1
\edef\pgfmathresult{z}%
\else
\edef\pgfmathresult{y}%
\fi
\else
\edef\pgfmathresult{x}%
\fi
\pgfmathsmuggle\pgfmathresult\endgroup%
}
\makeatother
\begin{document}
\begin{tikzpicture}[3d view={120}{15},line join=round,
xy face/.style={fill=orange},yx face/.style={fill=yellow},
xz face/.style={fill=blue},zx face/.style={fill=cyan},
yz face/.style={fill=red},zy face/.style={fill=magenta}]
\pgfmathsetmacro{\cubex}{5}
\pgfmathsetmacro{\cubey}{5}
\pgfmathsetmacro{\cubez}{5}
\begin{scope}[fill opacity=0.8,]
\def\pft#1#2;{\edef\planex{\csname cube#1\endcsname}%
\edef\planey{\csname cube#2\endcsname}}
\foreach \X/\Y in {xy/{(0,0,1)},yx/{(0,0,-1)},xz/{(0,1,0)},zx/{(0,-1,0)},yz/{(1,0,0)},zy/{(-1,0,0)}}
{\pgfmathsetmacro{\myproj}{screendepth\Y}
\ifdim\myproj pt<0pt
\pgfmathsetmacro{\mytot}{totalthree\Y}
\pgfmathsetmacro{\mydir}{direction\Y}
\edef\myshift{\csname cube\mydir\endcsname}
\expandafter\pft\X;
\begin{scope}[style/.expanded={canvas is \X\space plane at \mydir={\mytot*0.5*\myshift}}]
\draw[dashed,style/.expanded=\X\space face]
(-\planex/2,-\planey/2) rectangle (\planex/2,\planey/2);
\end{scope}
\fi}
\foreach \X/\Y in {xy/{(0,0,1)},yx/{(0,0,-1)},xz/{(0,1,0)},zx/{(0,-1,0)},yz/{(1,0,0)},zy/{(-1,0,0)}}
{\pgfmathsetmacro{\myproj}{screendepth\Y}
\ifdim\myproj pt>0pt
\pgfmathsetmacro{\mytot}{totalthree\Y}
\pgfmathsetmacro{\mydir}{direction\Y}
\edef\myshift{\csname cube\mydir\endcsname}
\expandafter\pft\X;
\begin{scope}[style/.expanded={canvas is \X\space plane at \mydir={\mytot*0.5*\myshift}}]
\draw[style/.expanded=\X\space face]
(-\planex/2,-\planey/2) rectangle (\planex/2,\planey/2);
\end{scope}
\fi}
\end{scope}
\begin{scope}[canvas is xz plane at y=\cubey/2]
\draw (-2,-2) rectangle (2,2);
\draw[thick] (0,0) circle[radius=1.5cm]
(-0.8,-0.3) arc[start angle=-120,end angle=-60,radius={0.8/sin(30)}];
\fill (-0.6,0.5) circle[radius=0.1] (0.6,0.5) circle[radius=0.1];
\end{scope}
\begin{scope}[canvas is yz plane at x=\cubex/2]
\koala[scale=1.5,yshift=-1cm,fill opacity=1]
\end{scope}
\end{tikzpicture}
\end{document}
考拉只是为了好玩,为了表明你可以通过这种方式投射几乎任何东西,也可以投射外部图形或文本,但transform shape
如果你要这样做,请确保拨入相应的节点(或范围)。