绘制带圆角的骰子模拟

绘制带圆角的骰子模拟

我使用以下代码这个帖子,绘制骰子模拟。

\documentclass[12pt,a4paper]{article}
\usepackage{verbatim}
\usepackage{tikz}
\usetikzlibrary{calc,shapes}
\usepackage{tikz-3dplot}
\begin{document}
\tdplotsetmaincoords{70}{110}
\begin{tikzpicture}[tdplot_main_coords]
 \begin{scope}[canvas is xy plane at z=3]
  \path[draw=none, fill=brown!50!white] (-3,-3) rectangle (3,3);
  \path (0,0) [line cap=round, line join=round, line width=0.9cm, rounded corners=0.9cm, inner color=green!70!black, outer color=green!70!black, rotate=18+90,transform shape] circle (2.cm);
 \end{scope}
 \begin{scope}[canvas is xz plane at y=3]
  \path[draw=none, fill=brown!70!white] (-3,-3) rectangle (3,3);
  \path (0,0) [line cap=round, line join=round, line width=0.9cm, rounded corners=0.9cm, inner color=yellow, outer color=yellow, rotate=18,transform shape] circle (2.cm);
 \end{scope}
 \begin{scope}[canvas is yz plane at x=3]
  \path[draw=none, fill=brown!80!white] (-3,-3) rectangle (3,3);
  \path (0,0) [line cap=round, line join=round, line width=0.9cm, rounded corners=0.9cm, inner color=red, outer color=red, rotate=18,transform shape] circle (2.cm);
 \end{scope}
\end{tikzpicture}
\end{document}

在此处输入图片描述

我如何将此代码放入某种样式中,以便在文档的不同位置以不同的旋转角度重复。

另外,我怎样才能使这个立方体看起来像一个骰子,具有可控制的圆角。

在此处输入图片描述

答案1

我只是这样做了,以便角度与边以相同的顺序工作,但它可以根据角度进行条件依赖。

\documentclass[tikz, border=1cm]{standalone}
\usepackage{tikz-3dplot}
\begin{document}
\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);
\fill[green!70!black] circle[radius=2/3];
\end{scope}
\begin{scope}[canvas is xz plane at y=1]
\filldraw (-1,-1) rectangle (1,1);
\fill[yellow] circle[radius=2/3];
\end{scope}
\begin{scope}[canvas is yz plane at x=1]
\filldraw (-1,-1) rectangle (1,1);
\fill[red] circle[radius=2/3];
\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}

彩色骰子

答案2

这是使用包的方法pst-solides3d。与使用的方法相比tikz-3dplot,它以类似 CAD 的方式生成真正的实体,而不仅仅是将 6 个单面组合成非封闭表面。此外,它还提供光源和阴影。

注意:代码必须用 编译XeLaTeX

倒角边缘

您可以通过viewpoint=x y z改变输出的视角来trunccoeff=...控制倒角的大小。

\documentclass{standalone}
\usepackage[svgnames]{pstricks}
\usepackage{pst-solides3d}

\begin{document}

\psset{viewpoint=50 65 40 rtp2xyz, Decran=40, lightsrc=viewpoint, lightintensity=1.5}

\begin{pspicture}(-2.5,-2.75)(2.5,2.75)
\psset{unit=0.5}
\psset{solidmemory}
\psSolid[object=cube, a=8, action=draw**, trunccoeff=.22, trunc=all, name=dice, linecolor=black, fillcolor=Beige]

% define planes to draw circles on
\psSolid[object=plan, action=none, definition=solidface, args=dice 0, name=plane0]
\psSolid[object=plan, action=none, definition=solidface, args=dice 4, name=plane1]
\psSolid[object=plan, action=none, definition=solidface, args=dice 1, name=plane2]

% draw yellow circle
\psset{plan=plane0}
\psProjection[object=point, action=none, name=yellowPoint](0,0)
\psProjection[object=cercle, args=yellowPoint 2, linecolor=black, fillstyle=solid, fillcolor=Yellow]

% draw blue circle
\psset{plan=plane1}
\psProjection[object=point, name=bluePoint, action=none](0,0)
\psProjection[object=cercle, args=bluePoint 2, linecolor=black, fillstyle=solid, fillcolor=CornflowerBlue]

% draw red circle
\psset{plan=plane2}
\psProjection[object=point, action=none, name=redPoint](0,0)
\psProjection[object=cercle, args=redPoint 2, linecolor=black, fillstyle=solid, fillcolor=Red]

\end{pspicture}
\end{document}

在此处输入图片描述

圆边

这是我的第一个答案的补充,实现圆边而不是倒角边。应该注意的是,pst3d-solides基于基于网格的方法,这就是面的来源。它们的数量可以通过变量进行调整ngrid=x y。随着网格数量的增加,编译时间会大幅增加。

\documentclass{standalone}
\usepackage[svgnames]{pstricks}
\usepackage{pst-solides3d}
\psset{viewpoint=50 65 40 rtp2xyz, Decran=30, lightsrc=viewpoint, lightintensity=1.5}

\begin{document}
\begin{pspicture}(-2.0,-2.5)(2.5,2.5)
\psset{solidmemory, fillcolor=Beige, lightsrc=viewpoint}
\codejps{/coeff 0.75 def /rO 4 def /OH coeff rO mul neg def}

% create sphere object and cut with planes for each side of the dice
\psSolid[object=sphere, r=rO, ngrid=18 27, plansepare={[1 0 0 OH]}, name=sphere, action=none]
\psSolid[object=load, load=sphere1, plansepare={[-1 0 0 OH]}, action=none, name=sphere]
\psSolid[object=load, load=sphere1, plansepare={[0 1 0 OH]}, action=none, name=sphere]
\psSolid[object=load, load=sphere1, plansepare={[0 -1 0 OH]}, action=none, name=sphere]
\psSolid[object=load, load=sphere1, plansepare={[0 0 1 OH]}, action=none, name=sphere]
\psSolid[object=load, load=sphere1, plansepare={[0 0 -1 OH]}, action=none, name=sphere]
\psSolid[object=load, load=sphere1, name=dice](0,0,0)

% define planes to draw circles on
\psSolid[object=plan, definition=equation, args={[1 0 0 OH] 90}, action=none, name=plane0]
\psSolid[object=plan, definition=equation, args={[0 0 1 OH] 90}, action=none, name=plane1]
\psSolid[object=plan, definition=equation, args={[0 1 0 OH] 90}, action=none, name=plane2]

% draw blue circle
\psset{plan=plane0}
\psProjection[object=point, action=none, name=yellowPoint](0,0)
\psProjection[object=cercle, args=yellowPoint 1.5, linecolor=black, fillstyle=solid, fillcolor=CornflowerBlue]

% draw yellow circle
\psset{plan=plane1}
\psProjection[object=point, name=bluePoint, action=none](0,0)
\psProjection[object=cercle, args=bluePoint 1.5, linecolor=black, fillstyle=solid, fillcolor=Yellow]

% draw red circle
\psset{plan=plane2}
\psProjection[object=point, action=none, name=redPoint](0,0)
\psProjection[object=cercle, args=redPoint 1.5, linecolor=black, fillstyle=solid, fillcolor=Red]
\composeSolid
\end{pspicture}

\end{document}

在此处输入图片描述

相关内容