5

enter image description here
I think the picture give the general idea and also i fell like i can smell the solution but definitely can't grasp it... I think that the counter may help me because i guess it allow to increment by one each time the ifthenelse find one candidate. Any help would be appreciated.

\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}
2
  • 3
    Don't know why \pgfmathaddtocount (I have never used it before) is not working, but if you include \usepackage{amsmath} (for the \text macro), declare the counters as \newcounter{checkzero} an \newcounter{checkuno} (note: no backslash), and simply use \stepcounter{checkuno} and \stepcounter{checkzero} the incrementing functionality should work. Sep 29, 2014 at 22:01
  • @PeterGrill Really thanks, your suggestions works!
    – yngabl
    Sep 29, 2014 at 22:43

3 Answers 3

6

Counter issue you are facing with is a TeX vs LaTeX counter settings. You can read more here What are the differences between TeX counts and LaTeX counters?

PGF/TikZ does the TeX way and a foreach loop sets up a group at each spin hence outside the group the modifications don't survive. You can use what Peter Grill suggested or declare the counting operations global.

\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}

enter image description here

6

The syntax of \pgfmathaddtocount is

\pgfmathaddtocount{<TeX counter>}{<expression>}

so, with

\newcount\checkuno

in the preamble,

\pgfmathaddtocount{\checkuno}{1}

works. But you still have to remember that \foreach cycles are performed in a group, so the setting to \checkuno will be forgotten as soon as the cycle ends.

Using \global\advance\checkuno by 1 will do, but it won't work with expressions. In this case you can use an indirect way:

\newcount\temporary
\newcount\checkuno

in the preamble and

\temporary=\checkuno
\pgfmathaddtocount{\temporary}{<expression>}
\global\checkuno=\temporary

in the cycle.

2

How about using the TikZ math library?. If provides a for loop that doesn't use a TeX scope, plus some other useful stuff. I does, however, require the latest release of 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}

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .