绘制 3D 骰子

绘制 3D 骰子

我想在 LaTeX 中创建 3D 骰子。我熟悉出色的软件包“customdice”,但不幸的是它只能生成 2D 图像。

我也受到了 hpekristiansen 的回答的启发这个问题。它可以制作出漂亮的骰子,但我无法将彩色圆圈换成熟悉的 1 至 6 数字圆圈符号。

我有一个想法,就是尝试将 customdice 中的图像放在每个表面上,但我无法让它工作。我认为 customdice 包与上面链接的问题中所需的包不太一致。

有人能帮我在上面链接的答案的表面上创建“正常”的骰子面吗?

答案1

您可以在 3D 画布上自己绘制骰子面,作为半径为 1/6 的小圆圈,使用以下坐标:

( 0.5, 0.5) top left
(-0.5, 0.5) top right
( 0.5, 0.0) middle left
( 0.0, 0.0) center
(-0.5, 0.0) middle right
( 0.5,-0.5) bottom left
(-0.5,-0.5) bottom right

然后您可以进行一些算术运算来找出哪些数字需要哪些点。

下面的代码使用计数器重复计算 1-6 的数字,当然您也可以使用常规数字作为参数\dicenum

\documentclass[tikz, border=1cm]{standalone}
\usepackage{tikz-3dplot}
\begin{document}
\newcommand{\dicenum}[1]{%
\pgfmathparse{#1==2 || #1==4 || #1==5 || #1==6}\ifnum\pgfmathresult>0\relax%
\fill[black] (0.5,0.5) circle[radius=1/6];    % top left
\fill[black] (-0.5,-0.5) circle[radius=1/6];\fi % bottom right
\pgfmathparse{#1==3 || #1==4 || #1==5 || #1==6}\ifnum\pgfmathresult>0\relax%
\fill[black] (-0.5,0.5) circle[radius=1/6];    % top right
\fill[black] (0.5,-0.5) circle[radius=1/6];\fi % bottom left
\pgfmathparse{#1==1 || #1==3 || #1==5}\ifnum\pgfmathresult>0\relax%
\fill[black] (0,0) circle[radius=1/6]; \fi % center
\ifnum#1=6\relax%
\fill[black] (0.5,0) circle[radius=1/6];     % middle left
\fill[black] (-0.5,0) circle[radius=1/6];\fi % middle right
}
\newcounter{currnum}
\setcounter{currnum}{1}
\begin{tikzpicture}
\newcommand{\dice}[5]{
\tdplotsetmaincoords{#3}{#4}
\begin{scope}[shift={(#1,#2)}, tdplot_main_coords, rounded corners=#5, fill=brown!50!white]
\begin{scope}[canvas is xy plane at z=-1]
\filldraw (-1,-1) rectangle (1,1);
\end{scope}
\begin{scope}[canvas is xz plane at y=-1]
\filldraw (-1,-1) rectangle (1,1);
\end{scope}
\begin{scope}[canvas is yz plane at x=-1]
\filldraw (-1,-1) rectangle (1,1);
\end{scope}
\begin{scope}[canvas is xy plane at z=1]
\filldraw (-1,-1) rectangle (1,1);
\dicenum{\value{currnum}}
\stepcounter{currnum}
\ifnum\value{currnum}>6\relax\setcounter{currnum}{1}\fi
\end{scope}
\begin{scope}[canvas is xz plane at y=1]
\filldraw (-1,-1) rectangle (1,1);
\dicenum{\value{currnum}}
\stepcounter{currnum}
\ifnum\value{currnum}>6\relax\setcounter{currnum}{1}\fi
\end{scope}
\begin{scope}[canvas is yz plane at x=1]
\filldraw (-1,-1) rectangle (1,1);
\dicenum{\value{currnum}}
\stepcounter{currnum}
\ifnum\value{currnum}>6\relax\setcounter{currnum}{1}\fi
\end{scope}
\end{scope}
}
\dice{0}{0}{70}{110}{0.3cm};
\dice{2}{2}{70}{110}{0.5cm};
\dice{5}{3}{40}{130}{0.3cm};
\dice{5}{-1}{40}{160}{0.6cm};
\end{tikzpicture}
\end{document}

结果:

在此处输入图片描述

相关内容