图中黑点组成字母 A。它仅使用 for 循环创建。 \ifnum 不允许两个或更多条件。所以我使用五次嵌套 for 循环来绘制所需的结果。这样做效率很低……有没有更好的方法?
以下是代码:
\documentclass{article}
\usepackage{tikz}
\usepackage{amssymb}
\usepackage{xifthen}
\begin{document}
\begin{figure}
\begin{tikzpicture}[scale=1]
\foreach \x in {0,...,11}
\foreach \y in {0,...,14}
{
\draw (\x,\y) circle(2pt);
}
\foreach \x in {1,...,3}
\foreach \y in {1,...,11}
{
\fill (\x,\y) circle(2pt);
}
\foreach \x in {8,...,10}
\foreach \y in {1,...,11}
{
\fill (\x,\y) circle(2pt);
}
\foreach \x in {4,...,8}
\foreach \y in {4,...,6}
{
\fill (\x,\y) circle(2pt);
}
\foreach \x in {4,...,8}
\foreach \y in {9,...,11}
{
\fill (\x,\y) circle(2pt);
}
\end{tikzpicture}
\end{figure}
\end{document}
答案1
你可以改为打穿。
\documentclass[tikz]{standalone}
\begin{document}
\begin{tikzpicture}
\clip[postaction=draw] foreach\x in{0,...,11}{\foreach\y in{0,...,14}{(\x,\y)circle(2pt)}};
\fill[even odd rule] (0.5,0.5) |- (10.5,11.5) |- (7.5,0.5) |- (3.5,3.5) |- (0.5,0.5)
(3.5,6.5) rectangle (7.5,8.5) ;
\end{tikzpicture}
\end{document}
答案2
像这样吗?
\documentclass[border=10pt]{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\foreach \x in {0,...,11}
{
\foreach \y [evaluate=\y as \s using {((\y==0)||(\y>11)) ? "" : ( ( (((\x>0)&&(\x<4))||(\x>7)&&(\x<11)) || ( ( (\x>3)&&(\x<9) ) && ( ( (\y>3)&&(\y<7) ) || (\y>8) ) ) ) ? "fill" : "" ) } ] in {0,...,14}
{
\draw [\s] (\x,\y) circle(2pt);
}
}
\end{tikzpicture}
\end{document}
答案3
使用 luaLaTeX。
对于使用独立类,我还需要这个
输出
代码
\RequirePackage{luatex85}
\documentclass[tikz,border=2pt]{standalone}
\usepackage{luacode}
\begin{luacode*}
function a(i,j)
middle = (j > 4 and j < 9) and (i>11 or i==7 or i==8)
square = (i < 15) and (i > 3) and (j>1) and (j<12)
return square and not middle
end
function drawOrFill(i,j,letter)
tex.print(letter(i,j) and [=[\fill[blue]]=] or [[\draw]])
end
\end{luacode*}
\begin{document}
\begin{tikzpicture}
\foreach \i in {1,...,15}
{
\foreach \j in {1,...,12}
{
\directlua{drawOrFill(\i,\j,a)} (\j,-\i) circle (4pt);
}
}
\end{tikzpicture}
\end{document}