1

this is the first time i code with tikz and i'm stuck in trying to delete everything outside the path drawn in black. Can anyone help me?

\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}

enter image description here

  • search inverse clip – Symbol 1 Feb 1 '18 at 13:39
  • @Symbol1 The OP says delete everything outside the path, maybe an ordinary clip is enough. – CarLaTeX Feb 1 '18 at 13:48
  • @CarLaTeX you are right... – Symbol 1 Feb 1 '18 at 14:01
  • @Symbol1 The title is misleading :):):) – CarLaTeX Feb 1 '18 at 14:01
2

There you go :)

enter image description here

I took the liberty to simplify your code a little.

When using \foreach, you can use the following syntax:

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

The loop will iterate from start to end with the step given by the difference of start and start+1.

To remove everything outside the path you use the \clip commmand.

\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}

(Nice picture! :))

| improve this answer | |
  • 2
    You beat me by a minute :) – CarLaTeX Feb 1 '18 at 13:57
  • @CarLaTeX It happens :) – Phelype Oleinik Feb 1 '18 at 14:01
  • You are very fast! – CarLaTeX Feb 1 '18 at 14:02
  • Sometimes... Grazie :) – Phelype Oleinik Feb 1 '18 at 14:05
  • thanks! Didn't know that loop syntax would have worked also for non integer so i generate the sequence with bash seq... Now i need to place that figure in a tex file (my theses :P) and i copied the code in the tex instead a \begin{figure}\centering ... \end{figure}. Do you suggest any smartest way also to manage figure dimension? – user153952 Feb 1 '18 at 16:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.