![\foreach 주기 내에서 \pgfmathaddtocount \pgfmathsetcount의 올바른 사용](https://rvso.com/image/298818/%5Cforeach%20%EC%A3%BC%EA%B8%B0%20%EB%82%B4%EC%97%90%EC%84%9C%20%5Cpgfmathaddtocount%20%5Cpgfmathsetcount%EC%9D%98%20%EC%98%AC%EB%B0%94%EB%A5%B8%20%EC%82%AC%EC%9A%A9.png)
그림이 일반적인 아이디어를 제공한다고 생각하며 솔루션 냄새를 맡을 수는 있지만 확실히 이해할 수는 없는 것 같습니다. 카운터가 매번 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
TikZ math
라이브러리를 사용하는 것은 어떻습니까?. 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}