我认为这张图片给出了大概的想法,而且我觉得我可以闻到解决方案的味道,但绝对无法理解它...我认为计数器可能会对我有帮助,因为我猜它允许每次增加一如果不是找到一名候选人。任何帮助都将不胜感激。
\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}