![Uso correcto de \pgfmathaddtocount \pgfmathsetcount dentro de un ciclo \foreach](https://rvso.com/image/298818/Uso%20correcto%20de%20%5Cpgfmathaddtocount%20%5Cpgfmathsetcount%20dentro%20de%20un%20ciclo%20%5Cforeach.png)
Creo que la imagen da una idea general y también siento que puedo oler la solución pero definitivamente no puedo captarla... Creo que el contador puede ayudarme porque supongo que permite incrementar en uno cada vez quesi no lo esencontrar un candidato. Cualquier ayuda sería apreciada.
\documentclass{article}
\usepackage{tikz}
\usepackage{xcolor}
\usepackage{xifthen}
\begin{document}
\begin{tikzpicture}
\tikzstyle{titmarg}=[yshift=4cm]
\tikzstyle{textmarg}=[yshift=-5cm]
\tikzstyle{questmarg}=[yshift=-6cm]
\tikzstyle{descmarg}=[xshift=0cm,yshift=3.5cm]
\tikzstyle{lefmarg}=[xshift=-1.5cm]
\tikzstyle{rigmarg}=[xshift=1.5cm]
\newcommand*{\margine}{4}%
\newcommand*{\scala}{.1}%
\newcommand*{\prove}{200}%
\newcommand*{\side}{1.5}%
%\newcommand*{\checkzero}{0};
%\newcommand*{\checkuno}{0};
\pgfmathsetmacro{\scaleside}{\side*.1}%
\path[] (-\scaleside,-\scaleside) rectangle (\scaleside,\scaleside);
\shadedraw[thick,inner color=white,outer color=red!10] (-\side,-\side) rectangle (\side, \side) node [midway] (centro) {};
\node at (centro) [above,titmarg,draw,scale=2] {\textbf{Area hit and miss}};
\node at (centro) [above,textmarg,text width=9cm] {I want to approximate $\pi$ with $\frac{{\color{green}\# \text{hit}}}{{\color{green}\# \text{hit}}+{\color{red}\# \text{miss}}}$ as $N\to\infty$, where $N={\color{green}\# \text{hit}}+{\color{red}\# \text{miss}}$.};
\node at (centro) [above,questmarg] {What is the correct using of \emph{pgfmathaddtocount} or similar inside a \emph{foreach} cycle?};
\node at (centro) [descmarg,draw,fill=yellow!50] {$N=\prove$ trials};
\shade[inner color=white,outer color=green!20] (0,0) circle (1);
\foreach \k in {0,...,\prove}
{
\pgfmathsetmacro{\x}{\side*rand}%
\pgfmathsetmacro{\y}{\side*rand}%
\pgfmathsetmacro{\distance}{sqrt(\x*\x+\y*\y)}%
\ifthenelse{1 > \distance}
{
\shade[inner color=green,outer color=white] (\x,\y) {} circle (2pt);
%\pgfmathaddtocount{\the\checkuno}{1}
}
{
\shade[inner color=red,outer color=white] (\x,\y) {} circle (2pt);
%\pgfmathaddtocount{\the\checkzero}{1}
}
\fill[] (\x,\y) {} circle (.1pt);
}
%\draw[] (0,0) circle (1);
%
\end{tikzpicture}
\end{document}
Respuesta1
El problema del contador al que se enfrenta es la configuración del contador TeX vs LaTeX. Puede leer más aquí¿Cuáles son las diferencias entre los conteos TeX y los contadores LaTeX?
PGF/TikZ funciona a la manera TeX y un bucle foreach configura un grupo en cada giro, por lo que fuera del grupo las modificaciones no sobreviven. Puede utilizar lo que sugirió Peter Grill o declarar las operaciones de conteo globales.
\documentclass[tikz]{standalone}
% TikZ is loaded so is xcolor already.
\newcount\checkzero
\newcount\checkuno
\begin{document}
\begin{tikzpicture}[titmarg/.style={yshift=4cm},
textmarg/.style={yshift=-5cm},
questmarg/.style={yshift=-6cm},
descmarg/.style={xshift=0cm,yshift=3.5cm},
lefmarg/.style={xshift=-1.5cm},
rigmarg/.style={xshift=1.5cm}]
\newcommand*{\margine}{4}%
\newcommand*{\scala}{.1}%
\newcommand*{\prove}{200}%
\newcommand*{\side}{1.5}%
\pgfmathsetmacro{\scaleside}{\side*.1}%
\path[] (-\scaleside,-\scaleside) rectangle (\scaleside,\scaleside);
\shadedraw[thick,inner color=white,outer color=red!10] (-\side,-\side) rectangle (\side,\side) node [midway] (centro) {};
\node at (centro) [above,titmarg,draw,scale=2] {\textbf{Area hit and miss}};
\node at (centro) [above,textmarg,text width=9cm] {I want to approximate $\pi$ with $\frac{{\color{green}\# \text{hit}}}{{\color{green}\# \text{hit}}+{\color{red}\# \text{miss}}}$ as $N\to\infty$, where $N={\color{green}\# \text{hit}}+{\color{red}\# \text{miss}}$.};
\node at (centro) [above,questmarg] {What is the correct using of \emph{pgfmathaddtocount} or similar inside a \emph{foreach} cycle?};
\node at (centro) [descmarg,draw,fill=yellow!50] {$N=\prove$ trials};
\shade[inner color=white,outer color=green!20] (0,0) circle (1);
\foreach \k in {1,...,\prove}% Starting from 0 makes N+1 samples
{
\pgfmathsetmacro{\x}{\side*rand}%
\pgfmathsetmacro{\y}{\side*rand}%
\pgfmathsetmacro{\distance}{sqrt(\x*\x+\y*\y)}%
\pgfmathparse{1 > \distance?int(1):int(0)}
\ifnum\pgfmathresult>0\relax% You can test with PGF ifthenelse as above
\shade[inner color=green,outer color=white] (\x,\y) {} circle (2pt);
\global\advance\checkuno by1\relax
\else
\shade[inner color=red,outer color=white] (\x,\y) {} circle (2pt);
\global\advance\checkzero by1\relax
\fi
\fill[] (\x,\y) {} circle (.1pt);
}
\node[align=left] (a) at (0,-3) {0 : \the\checkzero\\1 : \the\checkuno};
\end{tikzpicture}
\end{document}
Respuesta2
La sintaxis de \pgfmathaddtocount
es
\pgfmathaddtocount{<TeX counter>}{<expression>}
entonces, con
\newcount\checkuno
en el preámbulo,
\pgfmathaddtocount{\checkuno}{1}
obras. Pero aún debe recordar que \foreach
los ciclos se realizan en grupo, por lo que la configuración \checkuno
se olvidará tan pronto como finalice el ciclo.
Usarlo \global\advance\checkuno by 1
servirá, pero no funcionará con expresiones. En este caso puedes utilizar una forma indirecta:
\newcount\temporary
\newcount\checkuno
en el preámbulo y
\temporary=\checkuno
\pgfmathaddtocount{\temporary}{<expression>}
\global\checkuno=\temporary
en el ciclo.
Respuesta3
¿Qué tal usar la math
biblioteca TikZ? Proporciona un bucle for que no utiliza un alcance TeX, además de algunas otras cosas útiles. Sin embargo, necesito la última versión de PGF:
\documentclass[tikz,border=5]{standalone}
\usetikzlibrary{math}
\begin{document}
\begin{tikzpicture}[%
titmarg/.style={yshift=4cm},
textmarg/.style={yshift=-5cm},
questmarg/.style={yshift=-6cm},
descmarg/.style={xshift=0cm,yshift=3.5cm},
lefmarg/.style={xshift=-1.5cm},
rigmarg/.style={xshift=1.5cm}
]
\tikzmath{%
\margine = 4;
\scala = .1;
\prove = 200;
\side = 1.5;
\scaleside = \side * .1;
}
\path (-\scaleside,-\scaleside) rectangle (\scaleside,\scaleside);
\shadedraw [thick,inner color=white,outer color=red!10]
(-\side,-\side) rectangle (\side, \side) node [midway] (centro) {};
\node at (centro) [above, titmarg,draw,scale=2] {\textbf{Area hit and miss}};
\node at (centro) [descmarg,draw,fill=yellow!50] {$N=\prove$ trials};
\shade[inner color=white,outer color=green!20] (0,0) circle (1);
\tikzmath{%
integer \checkuno, \checkzero;
\checkuno = 0;
\checkzero = 0;
for \k in {1,...,\prove}{
\x = \side * rand;
\y = \side * rand;
\distance = veclen(\x, \y);
if (1 > \distance) then {
\checkuno = \checkuno + 1;
{
\shade[inner color=green,outer color=white] (\x,\y) {} circle (2pt);
};
} else {
\checkzero = \checkzero + 1;
{
\shade[inner color=red,outer color=white] (\x,\y) {} circle (2pt);
};
};
{
\fill[] (\x,\y) {} circle (.1pt);
};
};
}
\node[align=left] (a) at (0,-3) {0 : \checkzero\\1 : \checkuno};
\end{tikzpicture}
\end{document}