Korrekte Verwendung von \pgfmathaddtocount \pgfmathsetcount innerhalb eines \foreach-Zyklus

Korrekte Verwendung von \pgfmathaddtocount \pgfmathsetcount innerhalb eines \foreach-Zyklus

Bildbeschreibung hier eingeben
Ich denke, das Bild vermittelt eine allgemeine Vorstellung und ich habe auch das Gefühl, dass ich die Lösung riechen kann, aber sie definitiv nicht begreifen kann ... Ich denke, dass der Zähler mir helfen könnte, weil er es mir erlaubt, jedes Mal um eins zu erhöhen, wennwenndannsonstfinde einen Kandidaten. Jede Hilfe ist willkommen.

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

Antwort1

Das Zählerproblem, mit dem Sie konfrontiert sind, sind TeX vs. LaTeX-Zählereinstellungen. Weitere Informationen finden Sie hierWas sind die Unterschiede zwischen TeX-Zählern und LaTeX-Zählern?

PGF/TikZ macht es wie TeX und eine foreach-Schleife richtet bei jedem Spin eine Gruppe ein, sodass die Änderungen außerhalb der Gruppe nicht erhalten bleiben. Sie können den Vorschlag von Peter Grill verwenden oder die Zähloperationen als global deklarieren.

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

Bildbeschreibung hier eingeben

Antwort2

Die Syntax von \pgfmathaddtocountist

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

also mit

\newcount\checkuno

in der Präambel

\pgfmathaddtocount{\checkuno}{1}

funktioniert. Sie müssen jedoch bedenken, dass \foreachZyklen in einer Gruppe ausgeführt werden, sodass die Einstellung \checkunovergessen wird, sobald der Zyklus beendet ist.

Die Verwendung von \global\advance\checkuno by 1funktioniert, funktioniert aber nicht mit Ausdrücken. In diesem Fall können Sie einen indirekten Weg verwenden:

\newcount\temporary
\newcount\checkuno

in der Präambel und

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

im Zyklus.

Antwort3

Wie wäre es mit der Verwendung der TikZ- mathBibliothek? Sie bietet eine For-Schleife, die keinen TeX-Bereich verwendet, sowie einige andere nützliche Dinge. Allerdings ist dafür die neueste Version von PGF erforderlich:

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

Bildbeschreibung hier eingeben

verwandte Informationen