如何为长方体的每个面/表面获得具有不透明度的独特颜色(并集区域/重叠区域中的颜色混合)?

如何为长方体的每个面/表面获得具有不透明度的独特颜色(并集区域/重叠区域中的颜色混合)?

如何为长方体的每个面/表面获得具有不透明度的独特颜色(并集区域/重叠区域中的颜色混合)?

下面的屏幕截图并不描述我的代码的结果,而是期望的结果。

首选结果

\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}]
(0,0,0) [fill=green!40,opacity=0.8] coordinate (o) -- ++(-\cubex,0,0) coordinate (a) -- ++(0,- 
\cubey,0) [fill=blue!25,opacity=0.8] 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}
\end{document}

答案1

这当然是一个过度的答案。然而,这是独立于用于获取正交投影的包实现 3d 排序的尝试。填充颜色存储在类似 的键中xy face/.style={fill=orange}

\documentclass[tikz,border=5pt]{standalone}
\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,fill opacity=0.8,
    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}
\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{tikzpicture}
\end{document}

在此处输入图片描述

您可以随意更改视图和维度。例如,对于3d view={30}{15}和,\pgfmathsetmacro{\cubey}{3}得到

在此处输入图片描述

当然也存在更简单的可能性,例如此主题,其中一些不是正交投影,或者这里,它是特定于的tikz-3dplot

相关内容