
3 次元空間に単純な立方体があり、4 つの面が赤で描かれ、上部と下部が緑で描かれています。
\documentclass[border=5,tikz]{standalone}
\usepackage{tikz-3dplot}
\begin{document}
\foreach\s in{2,4,...,360}{
\tdplotsetmaincoords{2.71828+\s}{2.71828+\s*2}
\tikz[tdplot_main_coords,scale=.1]{
\path(-15cm,-15cm)(15cm,15cm);
\draw[ultra thick, color=black, fill=green!80!black]
(0,0,0)--(20,0,0)--(20,20,0)--(0,20,0)--cycle % bottom
(0,0,20)--(20,0,20)--(20,20,20)--(0,20,20)--cycle; % top
\draw[ultra thick, color=black, fill=red!80!black]
(0,0,0)--(20,0,0)--(20,0,20)--(0,0,20)--cycle
(0,20,0)--(20,20,0)--(20,20,20)--(0,20,20)--cycle
(0,0,0)--(0,20,0)--(0,20,20)--(0,0,20)--cycle
(20,0,0)--(20,20,0)--(20,20,20)--(20,0,20)--cycle;
}
}
\end{document}
TikZ で特定の角度から見ると、予期しない投影が表示されます。特定の面が重なると、透明になるようです。
これはバグですか、それともキューブの構築方法に何か問題があるのでしょうか?
答え1
各四角形は独立して塗りつぶされるため、穴として扱われることはありません。ソリッドの場合は、遠い面から近い面に向かって面を描くか、法線ベクトルを割り当てて、この方向を向く面のみを描画する必要があります。
\documentclass[border=5,tikz]{standalone}
% Uses \nearx, \neary and \nearz
% #1=x, #2=y, #3=z, #4={code to be executed}
\def\ifnear(#1,#2,#3)#4
{\pgfmathparse{#1*\nearx+#2*\neary+#3*\nearz}%
\ifdim\pgfmathresult pt>0pt\relax #4\fi}
\usepackage{tikz-3dplot}
\begin{document}
\foreach\s in{2,4,...,360}{
\pgfmathsetmacro{\aTheta}{2.71828+\s}
\pgfmathsetmacro{\aPhi}{2.71828+\s*2}
\pgfmathsetmacro{\nearx}{sin(\aPhi)*sin(\aTheta)}
\pgfmathsetmacro{\neary}{-cos(\aPhi)*sin(\aTheta)}
\pgfmathsetmacro{\nearz}{cos(\aTheta)}
\begin{tikzpicture}[scale=.1]
\path(-15cm,-15cm)(15cm,15cm);
\tdplotsetmaincoords{\aTheta}{\aPhi}
\begin{scope}[tdplot_main_coords]
\ifnear(0,0,-1){\draw[fill=green!80!black]
(0,0,0)--(20,0,0)--(20,20,0)--(0,20,0)--cycle;} % bottom
\ifnear(0,0,1){\draw[fill=green!80!black]
(0,0,20)--(20,0,20)--(20,20,20)--(0,20,20)--cycle;} % top
\ifnear(0,-1,0){\draw[fill=red!80!black]
(0,0,0)--(20,0,0)--(20,0,20)--(0,0,20)--cycle;}
\ifnear(0,1,0){\draw[fill=red!80!black]
(0,20,0)--(20,20,0)--(20,20,20)--(0,20,20)--cycle;}
\ifnear(-1,0,0){\draw[fill=red!80!black]
(0,0,0)--(0,20,0)--(0,20,20)--(0,0,20)--cycle;}
\ifnear(1,0,0){\draw[fill=red!80!black]
(20,0,0)--(20,20,0)--(20,20,20)--(20,0,20)--cycle;}
\end{scope}
\end{tikzpicture}
}
\end{document}
の符号を逆にすると、\nearx
立方体の反対側が表示されます。\neary
\nearz
法線ベクトルは、面に対して垂直な線で、(ここでは)中心から遠ざかる方向を指します。正多面体の場合は、オブジェクトの中心から面の中心へのベクトルを使用できます。
立方体の中心は (10,10,10) にあります。 底面の中心は (10,10,0) にあったため、その面の (正規化された) 法線ベクトルは (0,0,-1) でした。