裁剪路径外的区域

裁剪路径外的区域

这是我第一次使用 tikz 编码,我尝试删除黑色绘制的路径之外的所有内容,但遇到了困难。有人能帮助我吗?

\begin{tikzpicture}
        \foreach \x in {-15,-14.5,-14,-13.5,-13,-12.5,-12,-11.5,-11,-10.5,-10,-9.5,-9,-8.5,-8,-7.5,-7,-6.5,-6,-5.5,-5,-4.5,-4,-3.5,-3,-2.5,-2,-1.5,-1,-0.5,0,0.5,1,1.5,2,2.5,3,3.5,4,4.5,5,5.5,6,6.5,7,7.5,8,8.5,9,9.5,10,10.5,11,11.5,12,12.5,13,13.5,14,14.5}
        {
            \foreach \y in {-15,-14.5,-14,-13.5,-13,-12.5,-12,-11.5,-11,-10.5,-10,-9.5,-9,-8.5,-8,-7.5,-7,-6.5,-6,-5.5,-5,-4.5,-4,-3.5,-3,-2.5,-2,-1.5,-1,-0.5,0,0.5,1,1.5,2,2.5,3,3.5,4,4.5,5,5.5,6,6.5,7,7.5,8,8.5,9,9.5,10,10.5,11,11.5,12,12.5,13,13.5,14,14.5}
            {
                   \node[draw,circle,gray,inner sep=1.5pt,fill] at (.56002*\x,.56002*\y) {};
             }  
        }
    \begin{scope}[rotate=1]
        \foreach \x in {-13.5,-13,-12.5,-12,-11.5,-11,-10.5,-10,-9.5,-9,-8.5,-8,-7.5,-7,-6.5,-6,-5.5,-5,-4.5,-4,-3.5,-3,-2.5,-2,-1.5,-1,-0.5,0,0.5,1,1.5,2,2.5,3,3.5,4,4.5,5,5.5,6,6.5,7,7.5,8,8.5,9,9.5,10,10.5,11,11.5,12,12.5,13}
        {
             \foreach \y in {-13.5,-13,-12.5,-12,-11.5,-11,-10.5,-10,-9.5,-9,-8.5,-8,-7.5,-7,-6.5,-6,-5.5,-5,-4.5,-4,-3.5,-3,-2.5,-2,-1.5,-1,-0.5,0,0.5,1,1.5,2,2.5,3,3.5,4,4.5,5,5.5,6,6.5,7,7.5,8,8.5,9,9.5,10,10.5,11,11.5,12,12.5,13}
                {
                   \node[draw,circle,red,inner sep=1.5pt,fill] at (.600*\x,.6000*\y) {};
             }  
        }
    \end{scope}
    \path [draw,ultra thick, black] (-4,-8) -- (4,-3.0717) -- (4,6.92) -- (-4,2) -- (-4,-8);  
\end{tikzpicture}

在此处输入图片描述

答案1

就这样:)

在此处输入图片描述

我冒昧地稍微简化了你的代码。

使用时\foreach,可以使用以下语法:

\foreach \x in {start,start+1,...,end}

循环将从 到 进行迭代,start步长由和 的end差值给出。startstart+1

要删除路径之外的所有内容,请使用该\clip命令。

\documentclass{article}

\usepackage{tikz}

\begin{document}
\begin{tikzpicture}
        \clip (-4,-8) -- (4,-3.0717) -- (4,6.92) -- (-4,2) -- cycle;
        \foreach \x in {-15,-14.5,...,14.5}
        {
            \foreach \y in {-15,-14.5,...,14.5}
            {
                   \node [draw,circle,gray,inner sep=1.5pt,fill] at (.56002*\x,.56002*\y) {};
             }  
        }
    \begin{scope}[rotate=1]
        \foreach \x in {-13.5,-13,...,13}
        {
             \foreach \y in {-13.5,-13,...,13}
                {
                   \node[draw,circle,red,inner sep=1.5pt,fill] at (.600*\x,.6000*\y) {};
             }  
        }
    \end{scope}
    \path [draw,ultra thick, black] (-4,-8) -- (4,-3.0717) -- (4,6.92) -- (-4,2) -- (-4,-8);
\end{tikzpicture}

\end{document}

(漂亮的图片!:)

相关内容