4

I use the following code from the answers to this post, and this post to insert dice-simulation code in different locations.

How can I put the dice code in a style to be inserted in different locations in the grid!

\documentclass[12pt,a4paper]{article}
\usepackage{verbatim}
\usepackage{tikz}
\usetikzlibrary{calc,shapes}
\usepackage{tikz-3dplot}
\begin{document}
%begin defining shapes ==========================
\pgfmathsetmacro{\lwdth}{.3mm}% for line width
\tikzset{%
filrd/.style={%
fill=red},
filgr/.style={%
fill=green},
filyw/.style={%
fill=yellow},
filbl/.style={%
fill=blue},
}
%end defining shapes ==========================
%begin defining grid ==========================
\newcommand\sqw{1}
\tikzset{
pics/square/.default={\sqw},
pics/square/.style = {
code = {
\draw[pic actions, draw=none] (0,0) rectangle (#1,#1);
}}}%draw=none is to avoid displaying the little squares outline

\renewcommand\sqw{1.3} %end defining grid ==========================

\begin{tikzpicture} \newcommand{\dice}[5]{ \tdplotsetmaincoords{#3}{#4} \begin{scope}[shift={(#1,#2)}, tdplot_main_coords, rounded corners=#5, fill=brown!30!white] %bottom face \begin{scope}[canvas is xy plane at z=-1] \filldraw[draw=brown!30!white, fill=brown!30!white] (-1,-1) rectangle (1,1); \end{scope} %left face \begin{scope}[canvas is xz plane at y=-1] \filldraw[draw=brown!30!white, fill=brown!30!white] (-1,-1) rectangle (1,1); \end{scope} %back face \begin{scope}[canvas is yz plane at x=-1] \filldraw[draw=brown!30!white, fill=brown!30!white] (-1,-1) rectangle (1,1); \end{scope} %top face \begin{scope}[canvas is xy plane at z=1] \filldraw[draw=brown!30!white, fill=brown!30!white] (-1,-1) rectangle (1,1); \fill[green!70!black] circle[radius=2/3]; \end{scope} %right face \begin{scope}[canvas is xz plane at y=1] \filldraw[draw=brown!50!white, fill=brown!50!white] (-1,-1) rectangle (1,1); \fill[blue] circle[radius=2/3]; \end{scope} %front face \begin{scope}[canvas is yz plane at x=1] \filldraw[draw=brown!40!white, fill=brown!40!white] (-1,-1) rectangle (1,1); \fill[red] circle[radius=2/3]; \end{scope} \end{scope} } \dice{0}{0}{40}{130}{0.3cm}; \end{tikzpicture}

\begin{tikzpicture}[scale=1, transform shape] \draw[step=\sqw] (\sqw,\sqw) grid (2\sqw, 10\sqw); \pic[filbl] at (1\sqw,9\sqw) {square}; \node at (1.5\sqw,8.5\sqw) {\Huge insert dice here}; \pic[filgr] at (1\sqw,6\sqw) {square}; \node at (1.5\sqw,5.5\sqw) {\Huge insert dice here}; \pic[filgr] at (1\sqw,4\sqw) {square}; \node at (1.5\sqw,3.5\sqw) {\Huge insert dice here}; \pic[filrd] at (1\sqw,1\sqw) {square}; \end{tikzpicture} \end{document}

enter image description here

Hany
  • 4,709

1 Answers1

2

The left image puts the dice into a savebox and put the saveboxes into nodes. The right image uses a different \dice for each location.

I modified \dice to make named coordinates easier and to add a scale factor. However, the current version cannot handle large rotations accurately. See here

\documentclass[12pt,a4paper]{article}
\usepackage{verbatim}
\usepackage{tikz}
\usetikzlibrary{calc,shapes}
\usepackage{tikz-3dplot}

\newsavebox{\tempboxA} \newcommand{\dice}[5]{% #1=shift coordinates, #2=scale, #3,#4=view angles, #5=rounded coner radius \tdplotsetmaincoords{#3}{#4} \begin{scope}[shift={(#1)},scale=#2, tdplot_main_coords, rounded corners=#5, fill=brown!30!white] %bottom face \begin{scope}[canvas is xy plane at z=-1] \filldraw[draw=brown!30!white, fill=brown!30!white] (-1,-1) rectangle (1,1); \end{scope} %left face \begin{scope}[canvas is xz plane at y=-1] \filldraw[draw=brown!30!white, fill=brown!30!white] (-1,-1) rectangle (1,1); \end{scope} %back face \begin{scope}[canvas is yz plane at x=-1] \filldraw[draw=brown!30!white, fill=brown!30!white] (-1,-1) rectangle (1,1); \end{scope} %top face \begin{scope}[canvas is xy plane at z=1] \filldraw[draw=brown!30!white, fill=brown!30!white] (-1,-1) rectangle (1,1); \fill[green!70!black] circle[radius=2/3]; \end{scope} %right face \begin{scope}[canvas is xz plane at y=1] \filldraw[draw=brown!50!white, fill=brown!50!white] (-1,-1) rectangle (1,1); \fill[blue] circle[radius=2/3]; \end{scope} %front face \begin{scope}[canvas is yz plane at x=1] \filldraw[draw=brown!40!white, fill=brown!40!white] (-1,-1) rectangle (1,1); \fill[red] circle[radius=2/3]; \end{scope} \end{scope} }

\begin{document} %begin defining shapes ========================== \pgfmathsetmacro{\lwdth}{.3mm}% for line width \tikzset{% filrd/.style={% fill=red}, filgr/.style={% fill=green}, filyw/.style={% fill=yellow}, filbl/.style={% fill=blue}, } %end defining shapes ========================== %begin defining grid ========================== \newcommand\sqw{1} \tikzset{ pics/square/.default={\sqw}, pics/square/.style = { code = { \draw[pic actions, draw=none] (0,0) rectangle (#1,#1); }}}%draw=none is to avoid displaying the little squares outline

\renewcommand\sqw{1.3} %end defining grid ==========================

\savebox{\tempboxA}{\begin{tikzpicture} \dice{(0,0)}{0.4}{40}{130}{0.3cm}; \end{tikzpicture}}

\begin{tikzpicture}[scale=1, transform shape] \draw[step=\sqw] (\sqw,\sqw) grid (2\sqw, 10\sqw); \pic[filbl] at (1\sqw,9\sqw) {square}; \node at (1.5\sqw,8.5\sqw) {\usebox\tempboxA}; \pic[filgr] at (1\sqw,6\sqw) {square}; \node at (1.5\sqw,5.5\sqw) {\usebox\tempboxA}; \pic[filgr] at (1\sqw,4\sqw) {square}; \node at (1.5\sqw,3.5\sqw) {\usebox\tempboxA}; \pic[filrd] at (1\sqw,1\sqw) {square}; \end{tikzpicture} \quad \begin{tikzpicture}[scale=1, transform shape] \draw[step=\sqw] (\sqw,\sqw) grid (2\sqw, 10\sqw); \pic[filbl] at (1\sqw,9\sqw) {square}; \dice{(1.5\sqw,8.5\sqw)}{0.4}{40}{130}{0.3cm}; \pic[filgr] at (1\sqw,6\sqw) {square}; \dice{(1.5\sqw,5.5\sqw)}{0.4}{40}{130}{0.3cm}; \pic[filgr] at (1\sqw,4\sqw) {square}; \dice{(1.5\sqw,3.5\sqw)}{0.4}{40}{130}{0.3cm}; \pic[filrd] at (1\sqw,1\sqw) {square}; \end{tikzpicture} \end{document}

demo

John Kormylo
  • 79,712
  • 3
  • 50
  • 120
  • Thank you for your answer. I would like to add to your code a background colour [, background rectangle/.style={fill=violet!90!white}, show background rectangle, rounded corners=1.cm]. How and where can I insert it? – Hany Jan 15 '22 at 04:29
  • This is what I have in mind https://i.stack.imgur.com/E910a.png I could add the background colour when using \node[scale=.5, fill=gray!80!white] at (1.5\sqw,8.5\sqw) {\usebox\tempboxA}; But I could not add it when using \dice{(1.5\sqw,5.5\sqw)}. Could the background colour option be added to the original \newsavebox{\tempboxA} code! – Hany Jan 15 '22 at 13:54
  • Just add a normal \pic before \dice. The only difference between background and foreground is the order in which they are drawn. – John Kormylo Jan 15 '22 at 19:27
  • @– John Kormylo Thank you. But it will be easier to include the background colour option in the original \newsavebox{\tempboxA} code! to avoid repeating putting \pic before every \dice! – Hany Jan 16 '22 at 05:15