我需要填写高于该线的圆圈。
\begin{tikzpicture}
\foreach \a in {0,...,5}{
\draw (0,\a)--(13,\a);
}
\foreach \a in {0,...,13}{
\draw (\a,0)--(\a,5);
}
\foreach \a in {0,...,5}{
\foreach \b in {0,...,13}{
\ifnum \a > \b
% \ifnum 13*\a > 5*\b
\draw[fill=black] (\b,\a) circle (0.1); \else
\draw[] (\b,\a) circle (0.1); \fi
}
}
\draw[dashed] (0,0)--(13,5);
\end{tikzpicture}
最终结果如下:
Tikz 中的条件不太简单,您不能将数字定义为仅仅是数字,并且操作需要特别小心。
答案1
TikZ/pgf
还支持计算,例如通过\pgfmathsetmacro
:
\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\draw[line cap=rect]
\foreach \a in {0,...,5}{
(0,\a)--(13,\a)
}
\foreach \a in {0,...,13}{
(\a,0)--(\a,5)
}
;
\foreach \a in {0,...,5}{
\draw (0,\a)--(13,\a);
}
\foreach \a in {0,...,13}{
\draw (\a,0)--(\a,5);
}
\foreach \a in {0,...,5}{
\foreach \b in {0,...,13}{
\pgfmathsetmacro\q{5*\b/13}
\ifdim\q pt<\a pt
\draw[fill=black] (\b,\a) circle (0.1);
\else
\draw[] (\b,\a) circle (0.1);
\fi
}
}
\draw[dashed] (0,0)--(13,5);
\end{tikzpicture}
\end{document}
通过 e-TeX 进行计算的变体,无需除法,以避免舍入问题。
\foreach \a in {0,...,5}{
\foreach \b in {0,...,13}{
\ifnum\numexpr 5 * \b\relax < \numexpr \a * 13\relax
\draw[fill=black] (\b,\a) circle (0.1);
\else
\draw[] (\b,\a) circle (0.1);
\fi
}
}
该选项line cap=rect
改进了外部水平线和垂直线在角落处的相交方式。
答案2
使用\pgfmathparse
:
\documentclass[tikz,border=4mm]{standalone}
\begin{document}
\begin{tikzpicture}
\foreach \a in {0,...,5}{
\draw (0,\a)--(13,\a);
}
\foreach \a in {0,...,13}{
\draw (\a,0)--(\a,5);
}
\foreach \a in {0,...,5}{
\foreach \b in {0,...,13}{
\pgfmathparse{13/5*\a}
\ifdim \pgfmathresult pt > \b pt
% \ifnum 13*\a > 5*\b
\draw[fill=black] (\b,\a) circle (0.1); \else
\draw[] (\b,\a) circle (0.1); \fi
}
}
\draw[dashed] (0,0)--(13,5);
\end{tikzpicture}
\end{document}