\foreach サイクル内での \pgfmathaddtocount \pgfmathsetcount の正しい使用

\foreach サイクル内での \pgfmathaddtocount \pgfmathsetcount の正しい使用

ここに画像の説明を入力してください
写真を見ると大体のことは分かると思いますが、解決策の匂いはするのですが、はっきりとはわかりません...カウンターが役に立つと思います。もしそうでなければ候補者を 1 人見つけてください。ご協力いただければ幸いです。

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

答え1

あなたが直面しているカウンターの問題は、TeXとLaTeXのカウンター設定です。詳細はこちらをご覧ください。TeX カウントと LaTeX カウンターの違いは何ですか?

PGF/TikZ は TeX 方式で実行され、foreach ループは各スピンごとにグループを設定するため、グループ外の変更は保持されません。Peter Grill の提案を使用するか、カウント操作をグローバルに宣言することができます。

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

ここに画像の説明を入力してください

答え2

の構文\pgfmathaddtocount

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

だから、

\newcount\checkuno

前文では、

\pgfmathaddtocount{\checkuno}{1}

動作します。ただし、サイクルはグループで実行されるため、サイクルが終了するとすぐに\foreach設定が忘れられてしまうことを覚えておく必要があります。\checkuno

を使用する\global\advance\checkuno by 1とうまくいきますが、式では機能しません。この場合は間接的な方法を使用できます。

\newcount\temporary
\newcount\checkuno

前文と

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

サイクルの中で。

答え3

TikZmathライブラリの使用はいかがでしょうか。TeX スコープを使用しない for ループと、その他の便利な機能を提供します。ただし、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}

ここに画像の説明を入力してください

関連情報