在 \foreach 迴圈中正確使用 \pgfmathaddtocount \pgfmathsetcount

在 \foreach 迴圈中正確使用 \pgfmathaddtocount \pgfmathsetcount

在此輸入影像描述
我認為圖片給出了總體思路,而且我覺得我可以聞到解決方案,但絕對無法掌握它......我認為計數器可能會幫助我,因為我猜它允許每次增加一如果塞爾斯找到一名候選人。任何幫助,將不勝感激。

\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庫怎麼樣? If 提供了一個不使用 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}

在此輸入影像描述

相關內容