Tikz: Drawing a line inside of certain shape2

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
5
down vote

favorite












This is a related (more general) question to the previous post



Tikz: Drawing a line inside of certain shape



The answer given in the above question works perfectly if the given shape can be described by a single path. However, it doesn't work with any shape containing 2x2 block.



Let me illustrate my problem with the following example.



enter image description here



Like the picture above, I have a connected shape which is a union of square blocks. Now, I want to draw red lines so that these red lines form a "shrinked" version of the shape. However, I can not simply scale the shape as the new shape fomed by red lines is not exactly similar to the original shape.



Here is my code:



documentclassarticle
usepackagetikz
begindocument
begintikzpicture
draw (-3, -3) grid (5, 5);
draw[dashed, color=red] (0+0.1, 0-0.1) -- (1+0.1, 0-0.1) -- (1+0.1, 2-0.1)-- (2+0.1, 2-0.1) -- (2+0.1, 4-0.1) -- (3-0.1, 4-0.1) -- (3-0.1, 1+0.1) -- (3-0.1, -1+0.1) -- (-0+0.1, -1+0.1) -- (0+0.1, 0-0.1);
endtikzpicture
enddocument


Although I can manually add or substract a small number to every vertex of the original shape to create red lines, but I am looking for a simpler solution. Thanks in advance.







share|improve this question
























    up vote
    5
    down vote

    favorite












    This is a related (more general) question to the previous post



    Tikz: Drawing a line inside of certain shape



    The answer given in the above question works perfectly if the given shape can be described by a single path. However, it doesn't work with any shape containing 2x2 block.



    Let me illustrate my problem with the following example.



    enter image description here



    Like the picture above, I have a connected shape which is a union of square blocks. Now, I want to draw red lines so that these red lines form a "shrinked" version of the shape. However, I can not simply scale the shape as the new shape fomed by red lines is not exactly similar to the original shape.



    Here is my code:



    documentclassarticle
    usepackagetikz
    begindocument
    begintikzpicture
    draw (-3, -3) grid (5, 5);
    draw[dashed, color=red] (0+0.1, 0-0.1) -- (1+0.1, 0-0.1) -- (1+0.1, 2-0.1)-- (2+0.1, 2-0.1) -- (2+0.1, 4-0.1) -- (3-0.1, 4-0.1) -- (3-0.1, 1+0.1) -- (3-0.1, -1+0.1) -- (-0+0.1, -1+0.1) -- (0+0.1, 0-0.1);
    endtikzpicture
    enddocument


    Although I can manually add or substract a small number to every vertex of the original shape to create red lines, but I am looking for a simpler solution. Thanks in advance.







    share|improve this question






















      up vote
      5
      down vote

      favorite









      up vote
      5
      down vote

      favorite











      This is a related (more general) question to the previous post



      Tikz: Drawing a line inside of certain shape



      The answer given in the above question works perfectly if the given shape can be described by a single path. However, it doesn't work with any shape containing 2x2 block.



      Let me illustrate my problem with the following example.



      enter image description here



      Like the picture above, I have a connected shape which is a union of square blocks. Now, I want to draw red lines so that these red lines form a "shrinked" version of the shape. However, I can not simply scale the shape as the new shape fomed by red lines is not exactly similar to the original shape.



      Here is my code:



      documentclassarticle
      usepackagetikz
      begindocument
      begintikzpicture
      draw (-3, -3) grid (5, 5);
      draw[dashed, color=red] (0+0.1, 0-0.1) -- (1+0.1, 0-0.1) -- (1+0.1, 2-0.1)-- (2+0.1, 2-0.1) -- (2+0.1, 4-0.1) -- (3-0.1, 4-0.1) -- (3-0.1, 1+0.1) -- (3-0.1, -1+0.1) -- (-0+0.1, -1+0.1) -- (0+0.1, 0-0.1);
      endtikzpicture
      enddocument


      Although I can manually add or substract a small number to every vertex of the original shape to create red lines, but I am looking for a simpler solution. Thanks in advance.







      share|improve this question












      This is a related (more general) question to the previous post



      Tikz: Drawing a line inside of certain shape



      The answer given in the above question works perfectly if the given shape can be described by a single path. However, it doesn't work with any shape containing 2x2 block.



      Let me illustrate my problem with the following example.



      enter image description here



      Like the picture above, I have a connected shape which is a union of square blocks. Now, I want to draw red lines so that these red lines form a "shrinked" version of the shape. However, I can not simply scale the shape as the new shape fomed by red lines is not exactly similar to the original shape.



      Here is my code:



      documentclassarticle
      usepackagetikz
      begindocument
      begintikzpicture
      draw (-3, -3) grid (5, 5);
      draw[dashed, color=red] (0+0.1, 0-0.1) -- (1+0.1, 0-0.1) -- (1+0.1, 2-0.1)-- (2+0.1, 2-0.1) -- (2+0.1, 4-0.1) -- (3-0.1, 4-0.1) -- (3-0.1, 1+0.1) -- (3-0.1, -1+0.1) -- (-0+0.1, -1+0.1) -- (0+0.1, 0-0.1);
      endtikzpicture
      enddocument


      Although I can manually add or substract a small number to every vertex of the original shape to create red lines, but I am looking for a simpler solution. Thanks in advance.









      share|improve this question











      share|improve this question




      share|improve this question










      asked Aug 9 at 19:42









      Juyoung Jeong

      403




      403




















          3 Answers
          3






          active

          oldest

          votes

















          up vote
          6
          down vote



          accepted










          An option using some fake shape, using a line inside other with less thicknes that not allow to draw like dashed style, whatever is posible with manual drawing helped using node names for desired shapes and without style problems.



          RESULT:
          enter image description here



          MWE:



          documentclassstandalone
          usepackagetikz
          usetikzlibrarybackgrounds,fit
          begindocument
          begintikzpicture[
          Myline/.style=
          draw,
          red,
          line width=0.8cm,
          shorten >=-0.4cm,
          shorten <=-0.4cm,
          postaction=
          -,draw,white, line width=0.75cm, shorten >=-0.375cm,
          shorten <=-0.375cm,


          ]
          draw (0,0) grid (8, 8);
          foreach x in 1,2,...,8
          foreach y in 1,2,...,8
          draw (x-0.5,y-0.5) coordinate (x-y);
          node[rectangle, inner sep=12pt](N-x-y) at (x-0.5,y-0.5);


          %Draw as a line % dasheed is not posible
          beginscope[on background layer]
          draw[Myline] (1-1) |- (2-3) |- (1-5);
          draw[Myline] (4-3) -| (5-5) -| (6-7);
          draw[Myline,blue] (4-1) -| (5-2) -| (6-1) -| (8-2) -| (7-3);
          endscope
          %Manual drawing using node coordinates
          draw[dashed,red,thick,fill=red,fill opacity=0.1](N-1-8.135)
          -| (N-5-6.-45)
          -| (N-4-7.-135)
          -| (N-3-7.135)
          -|(N-4-6.45)
          -| (N-5-8.-135)
          -| cycle;
          draw[dashed,blue,thick,fill=blue,fill opacity=0.1](N-4-3.-135)
          -| (N-6-7.45)
          -| (N-6-5.135)
          -| (N-5-3.135)
          -| cycle;
          endtikzpicture
          enddocument





          share|improve this answer



























            up vote
            7
            down vote













            This is really just a fun proposal, which may or may not be useful. It seems to me that you want to draw a boundary around certain points of the grid. We could represent the grid by a matrix and say that the boundary should be drawn around the entries 1, all other entries are 0. Here is a very first version. It can and will be improved if that way turns out to make at least a tiny bit of sense. At the moment, the dimensions are hard coded and the shape cannot extend to the boundary of the grid, two things that can easily be fixed.



            documentclass[border=3.14mm]standalone
            usepackagetikz
            usepackagepgfplotstable
            usepackagefilecontents

            beginfilecontents*grid.csv
            0,0,0,0,0,0,0,0
            0,0,0,0,0,1,0,0
            0,0,0,0,0,1,0,0
            0,0,0,0,1,1,0,0
            0,0,0,0,1,1,0,0
            0,0,0,1,1,1,0,0
            0,0,0,0,0,0,0,0
            0,0,0,0,0,0,0,0
            endfilecontents*
            newcommand*ReadOutElement[4]%
            pgfplotstablegetelem#2[index]#3of#1%
            let#4pgfplotsretval

            begindocument
            pgfplotstableread[header=false,col sep=comma]grid.csvdatatable
            tikzsetmystyle/.style=red,dashed
            begintikzpicture
            foreach Y [evaluate=Y as PrevY using int(Y-1),
            evaluate=Y as NextY using int(Y+1)] in 1,...,6
            foreach X [evaluate=X as PrevX using int(X-1),
            evaluate=X as NextX using int(X+1)] in 1,...,6

            ReadOutElementdatatableYXCurrent
            ReadOutElementdatatablePrevYXAbove
            ReadOutElementdatatableNextYXBelow
            ReadOutElementdatatableYPrevXLeft
            ReadOutElementdatatableYNextXRight
            ifnumCurrent=1
            typeoutY,X,Above
            ifnumLeft=0
            draw[mystyle] (1+X-0.95,8-Y-0.95) -- (1+X-0.95,8-Y-0.05);
            fi
            ifnumRight=0
            draw[mystyle] (1+X-0.05,8-Y-0.95) -- (1+X-0.05,8-Y-0.05);
            fi
            ifnumAbove=0
            draw[mystyle] (1+X-0.95,8-Y-0.05) -- (1+X-0.05,8-Y-0.05);
            fi
            ifnumBelow=0
            draw[mystyle] (1+X-0.95,8-Y-0.95) -- (1+X-0.05,8-Y-0.95);
            fi
            fi


            draw (0,0) grid (8,8);
            endtikzpicture
            enddocument


            enter image description here






            share|improve this answer



























              up vote
              2
              down vote













              I don't know if this answer will fit the problem, but it shows another way for referencig cells and filling areas.



              logicpuzzle is package which can be used to prepare what it's name means, logic puzzles loke sudokus, kakuros, ... But it also provides an easy way of drawing grids and some commands to frame and fill areas in these grids.



              Environment beginlogicpuzzle[rows=8,columns=8]...endlogicpuzzle will draw a 8x8 grid.



              Commands for drawing/filling areas inside the grid are frameareacolortikzpath,
              fillareacolortikzpath, colorareacolortikzpath. In this commands the tikzpath is path where coordinates identifiy the lower left corner of each cell and where (1,1) is the lower left cell. Linear (--) or perpendicular (-|, |-) paths are allowed.



              Another syntax is provided by xtikzpath which can be used inside tikzpath argument in previous commands.



              fillareared!20xtikzpath616/1,8/1,4/1,2/1


              fills cell (6,1). First ,two arguments represent the bottom left corner of starting position. Following pairs like 6/1 mean move right (6=right) 1 cell. The first number of the pair represents a direction according its position in numeric keyboard, while the second is the distance to move.



              A complete code with some examples looks like:



              documentclassarticle 
              usepackagelogicpuzzle
              begindocument
              beginlogicpuzzle[rows=8,columns=8]
              frameareared(7,8)-
              fillareabrown!40(3,6)-
              colorareablue!50(1,9)--(3,9)--(3,8)--(5,8)--(5,6)--(2,6)--(2,8)--(1,8)--cycle
              fillareared!20xtikzpath616/2,8/2,6/1,8/3,4/1,2/2,4/1,2/2,4/1,2/1
              endlogicpuzzle
              enddocument


              enter image description here






              share|improve this answer




















                Your Answer







                StackExchange.ready(function()
                var channelOptions =
                tags: "".split(" "),
                id: "85"
                ;
                initTagRenderer("".split(" "), "".split(" "), channelOptions);

                StackExchange.using("externalEditor", function()
                // Have to fire editor after snippets, if snippets enabled
                if (StackExchange.settings.snippets.snippetsEnabled)
                StackExchange.using("snippets", function()
                createEditor();
                );

                else
                createEditor();

                );

                function createEditor()
                StackExchange.prepareEditor(
                heartbeatType: 'answer',
                convertImagesToLinks: false,
                noModals: false,
                showLowRepImageUploadWarning: true,
                reputationToPostImages: null,
                bindNavPrevention: true,
                postfix: "",
                onDemand: true,
                discardSelector: ".discard-answer"
                ,immediatelyShowMarkdownHelp:true
                );



                );








                 

                draft saved


                draft discarded


















                StackExchange.ready(
                function ()
                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2ftex.stackexchange.com%2fquestions%2f445375%2ftikz-drawing-a-line-inside-of-certain-shape2%23new-answer', 'question_page');

                );

                Post as a guest






























                3 Answers
                3






                active

                oldest

                votes








                3 Answers
                3






                active

                oldest

                votes









                active

                oldest

                votes






                active

                oldest

                votes








                up vote
                6
                down vote



                accepted










                An option using some fake shape, using a line inside other with less thicknes that not allow to draw like dashed style, whatever is posible with manual drawing helped using node names for desired shapes and without style problems.



                RESULT:
                enter image description here



                MWE:



                documentclassstandalone
                usepackagetikz
                usetikzlibrarybackgrounds,fit
                begindocument
                begintikzpicture[
                Myline/.style=
                draw,
                red,
                line width=0.8cm,
                shorten >=-0.4cm,
                shorten <=-0.4cm,
                postaction=
                -,draw,white, line width=0.75cm, shorten >=-0.375cm,
                shorten <=-0.375cm,


                ]
                draw (0,0) grid (8, 8);
                foreach x in 1,2,...,8
                foreach y in 1,2,...,8
                draw (x-0.5,y-0.5) coordinate (x-y);
                node[rectangle, inner sep=12pt](N-x-y) at (x-0.5,y-0.5);


                %Draw as a line % dasheed is not posible
                beginscope[on background layer]
                draw[Myline] (1-1) |- (2-3) |- (1-5);
                draw[Myline] (4-3) -| (5-5) -| (6-7);
                draw[Myline,blue] (4-1) -| (5-2) -| (6-1) -| (8-2) -| (7-3);
                endscope
                %Manual drawing using node coordinates
                draw[dashed,red,thick,fill=red,fill opacity=0.1](N-1-8.135)
                -| (N-5-6.-45)
                -| (N-4-7.-135)
                -| (N-3-7.135)
                -|(N-4-6.45)
                -| (N-5-8.-135)
                -| cycle;
                draw[dashed,blue,thick,fill=blue,fill opacity=0.1](N-4-3.-135)
                -| (N-6-7.45)
                -| (N-6-5.135)
                -| (N-5-3.135)
                -| cycle;
                endtikzpicture
                enddocument





                share|improve this answer
























                  up vote
                  6
                  down vote



                  accepted










                  An option using some fake shape, using a line inside other with less thicknes that not allow to draw like dashed style, whatever is posible with manual drawing helped using node names for desired shapes and without style problems.



                  RESULT:
                  enter image description here



                  MWE:



                  documentclassstandalone
                  usepackagetikz
                  usetikzlibrarybackgrounds,fit
                  begindocument
                  begintikzpicture[
                  Myline/.style=
                  draw,
                  red,
                  line width=0.8cm,
                  shorten >=-0.4cm,
                  shorten <=-0.4cm,
                  postaction=
                  -,draw,white, line width=0.75cm, shorten >=-0.375cm,
                  shorten <=-0.375cm,


                  ]
                  draw (0,0) grid (8, 8);
                  foreach x in 1,2,...,8
                  foreach y in 1,2,...,8
                  draw (x-0.5,y-0.5) coordinate (x-y);
                  node[rectangle, inner sep=12pt](N-x-y) at (x-0.5,y-0.5);


                  %Draw as a line % dasheed is not posible
                  beginscope[on background layer]
                  draw[Myline] (1-1) |- (2-3) |- (1-5);
                  draw[Myline] (4-3) -| (5-5) -| (6-7);
                  draw[Myline,blue] (4-1) -| (5-2) -| (6-1) -| (8-2) -| (7-3);
                  endscope
                  %Manual drawing using node coordinates
                  draw[dashed,red,thick,fill=red,fill opacity=0.1](N-1-8.135)
                  -| (N-5-6.-45)
                  -| (N-4-7.-135)
                  -| (N-3-7.135)
                  -|(N-4-6.45)
                  -| (N-5-8.-135)
                  -| cycle;
                  draw[dashed,blue,thick,fill=blue,fill opacity=0.1](N-4-3.-135)
                  -| (N-6-7.45)
                  -| (N-6-5.135)
                  -| (N-5-3.135)
                  -| cycle;
                  endtikzpicture
                  enddocument





                  share|improve this answer






















                    up vote
                    6
                    down vote



                    accepted







                    up vote
                    6
                    down vote



                    accepted






                    An option using some fake shape, using a line inside other with less thicknes that not allow to draw like dashed style, whatever is posible with manual drawing helped using node names for desired shapes and without style problems.



                    RESULT:
                    enter image description here



                    MWE:



                    documentclassstandalone
                    usepackagetikz
                    usetikzlibrarybackgrounds,fit
                    begindocument
                    begintikzpicture[
                    Myline/.style=
                    draw,
                    red,
                    line width=0.8cm,
                    shorten >=-0.4cm,
                    shorten <=-0.4cm,
                    postaction=
                    -,draw,white, line width=0.75cm, shorten >=-0.375cm,
                    shorten <=-0.375cm,


                    ]
                    draw (0,0) grid (8, 8);
                    foreach x in 1,2,...,8
                    foreach y in 1,2,...,8
                    draw (x-0.5,y-0.5) coordinate (x-y);
                    node[rectangle, inner sep=12pt](N-x-y) at (x-0.5,y-0.5);


                    %Draw as a line % dasheed is not posible
                    beginscope[on background layer]
                    draw[Myline] (1-1) |- (2-3) |- (1-5);
                    draw[Myline] (4-3) -| (5-5) -| (6-7);
                    draw[Myline,blue] (4-1) -| (5-2) -| (6-1) -| (8-2) -| (7-3);
                    endscope
                    %Manual drawing using node coordinates
                    draw[dashed,red,thick,fill=red,fill opacity=0.1](N-1-8.135)
                    -| (N-5-6.-45)
                    -| (N-4-7.-135)
                    -| (N-3-7.135)
                    -|(N-4-6.45)
                    -| (N-5-8.-135)
                    -| cycle;
                    draw[dashed,blue,thick,fill=blue,fill opacity=0.1](N-4-3.-135)
                    -| (N-6-7.45)
                    -| (N-6-5.135)
                    -| (N-5-3.135)
                    -| cycle;
                    endtikzpicture
                    enddocument





                    share|improve this answer












                    An option using some fake shape, using a line inside other with less thicknes that not allow to draw like dashed style, whatever is posible with manual drawing helped using node names for desired shapes and without style problems.



                    RESULT:
                    enter image description here



                    MWE:



                    documentclassstandalone
                    usepackagetikz
                    usetikzlibrarybackgrounds,fit
                    begindocument
                    begintikzpicture[
                    Myline/.style=
                    draw,
                    red,
                    line width=0.8cm,
                    shorten >=-0.4cm,
                    shorten <=-0.4cm,
                    postaction=
                    -,draw,white, line width=0.75cm, shorten >=-0.375cm,
                    shorten <=-0.375cm,


                    ]
                    draw (0,0) grid (8, 8);
                    foreach x in 1,2,...,8
                    foreach y in 1,2,...,8
                    draw (x-0.5,y-0.5) coordinate (x-y);
                    node[rectangle, inner sep=12pt](N-x-y) at (x-0.5,y-0.5);


                    %Draw as a line % dasheed is not posible
                    beginscope[on background layer]
                    draw[Myline] (1-1) |- (2-3) |- (1-5);
                    draw[Myline] (4-3) -| (5-5) -| (6-7);
                    draw[Myline,blue] (4-1) -| (5-2) -| (6-1) -| (8-2) -| (7-3);
                    endscope
                    %Manual drawing using node coordinates
                    draw[dashed,red,thick,fill=red,fill opacity=0.1](N-1-8.135)
                    -| (N-5-6.-45)
                    -| (N-4-7.-135)
                    -| (N-3-7.135)
                    -|(N-4-6.45)
                    -| (N-5-8.-135)
                    -| cycle;
                    draw[dashed,blue,thick,fill=blue,fill opacity=0.1](N-4-3.-135)
                    -| (N-6-7.45)
                    -| (N-6-5.135)
                    -| (N-5-3.135)
                    -| cycle;
                    endtikzpicture
                    enddocument






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Aug 9 at 21:20









                    J Leon V.

                    5,122424




                    5,122424




















                        up vote
                        7
                        down vote













                        This is really just a fun proposal, which may or may not be useful. It seems to me that you want to draw a boundary around certain points of the grid. We could represent the grid by a matrix and say that the boundary should be drawn around the entries 1, all other entries are 0. Here is a very first version. It can and will be improved if that way turns out to make at least a tiny bit of sense. At the moment, the dimensions are hard coded and the shape cannot extend to the boundary of the grid, two things that can easily be fixed.



                        documentclass[border=3.14mm]standalone
                        usepackagetikz
                        usepackagepgfplotstable
                        usepackagefilecontents

                        beginfilecontents*grid.csv
                        0,0,0,0,0,0,0,0
                        0,0,0,0,0,1,0,0
                        0,0,0,0,0,1,0,0
                        0,0,0,0,1,1,0,0
                        0,0,0,0,1,1,0,0
                        0,0,0,1,1,1,0,0
                        0,0,0,0,0,0,0,0
                        0,0,0,0,0,0,0,0
                        endfilecontents*
                        newcommand*ReadOutElement[4]%
                        pgfplotstablegetelem#2[index]#3of#1%
                        let#4pgfplotsretval

                        begindocument
                        pgfplotstableread[header=false,col sep=comma]grid.csvdatatable
                        tikzsetmystyle/.style=red,dashed
                        begintikzpicture
                        foreach Y [evaluate=Y as PrevY using int(Y-1),
                        evaluate=Y as NextY using int(Y+1)] in 1,...,6
                        foreach X [evaluate=X as PrevX using int(X-1),
                        evaluate=X as NextX using int(X+1)] in 1,...,6

                        ReadOutElementdatatableYXCurrent
                        ReadOutElementdatatablePrevYXAbove
                        ReadOutElementdatatableNextYXBelow
                        ReadOutElementdatatableYPrevXLeft
                        ReadOutElementdatatableYNextXRight
                        ifnumCurrent=1
                        typeoutY,X,Above
                        ifnumLeft=0
                        draw[mystyle] (1+X-0.95,8-Y-0.95) -- (1+X-0.95,8-Y-0.05);
                        fi
                        ifnumRight=0
                        draw[mystyle] (1+X-0.05,8-Y-0.95) -- (1+X-0.05,8-Y-0.05);
                        fi
                        ifnumAbove=0
                        draw[mystyle] (1+X-0.95,8-Y-0.05) -- (1+X-0.05,8-Y-0.05);
                        fi
                        ifnumBelow=0
                        draw[mystyle] (1+X-0.95,8-Y-0.95) -- (1+X-0.05,8-Y-0.95);
                        fi
                        fi


                        draw (0,0) grid (8,8);
                        endtikzpicture
                        enddocument


                        enter image description here






                        share|improve this answer
























                          up vote
                          7
                          down vote













                          This is really just a fun proposal, which may or may not be useful. It seems to me that you want to draw a boundary around certain points of the grid. We could represent the grid by a matrix and say that the boundary should be drawn around the entries 1, all other entries are 0. Here is a very first version. It can and will be improved if that way turns out to make at least a tiny bit of sense. At the moment, the dimensions are hard coded and the shape cannot extend to the boundary of the grid, two things that can easily be fixed.



                          documentclass[border=3.14mm]standalone
                          usepackagetikz
                          usepackagepgfplotstable
                          usepackagefilecontents

                          beginfilecontents*grid.csv
                          0,0,0,0,0,0,0,0
                          0,0,0,0,0,1,0,0
                          0,0,0,0,0,1,0,0
                          0,0,0,0,1,1,0,0
                          0,0,0,0,1,1,0,0
                          0,0,0,1,1,1,0,0
                          0,0,0,0,0,0,0,0
                          0,0,0,0,0,0,0,0
                          endfilecontents*
                          newcommand*ReadOutElement[4]%
                          pgfplotstablegetelem#2[index]#3of#1%
                          let#4pgfplotsretval

                          begindocument
                          pgfplotstableread[header=false,col sep=comma]grid.csvdatatable
                          tikzsetmystyle/.style=red,dashed
                          begintikzpicture
                          foreach Y [evaluate=Y as PrevY using int(Y-1),
                          evaluate=Y as NextY using int(Y+1)] in 1,...,6
                          foreach X [evaluate=X as PrevX using int(X-1),
                          evaluate=X as NextX using int(X+1)] in 1,...,6

                          ReadOutElementdatatableYXCurrent
                          ReadOutElementdatatablePrevYXAbove
                          ReadOutElementdatatableNextYXBelow
                          ReadOutElementdatatableYPrevXLeft
                          ReadOutElementdatatableYNextXRight
                          ifnumCurrent=1
                          typeoutY,X,Above
                          ifnumLeft=0
                          draw[mystyle] (1+X-0.95,8-Y-0.95) -- (1+X-0.95,8-Y-0.05);
                          fi
                          ifnumRight=0
                          draw[mystyle] (1+X-0.05,8-Y-0.95) -- (1+X-0.05,8-Y-0.05);
                          fi
                          ifnumAbove=0
                          draw[mystyle] (1+X-0.95,8-Y-0.05) -- (1+X-0.05,8-Y-0.05);
                          fi
                          ifnumBelow=0
                          draw[mystyle] (1+X-0.95,8-Y-0.95) -- (1+X-0.05,8-Y-0.95);
                          fi
                          fi


                          draw (0,0) grid (8,8);
                          endtikzpicture
                          enddocument


                          enter image description here






                          share|improve this answer






















                            up vote
                            7
                            down vote










                            up vote
                            7
                            down vote









                            This is really just a fun proposal, which may or may not be useful. It seems to me that you want to draw a boundary around certain points of the grid. We could represent the grid by a matrix and say that the boundary should be drawn around the entries 1, all other entries are 0. Here is a very first version. It can and will be improved if that way turns out to make at least a tiny bit of sense. At the moment, the dimensions are hard coded and the shape cannot extend to the boundary of the grid, two things that can easily be fixed.



                            documentclass[border=3.14mm]standalone
                            usepackagetikz
                            usepackagepgfplotstable
                            usepackagefilecontents

                            beginfilecontents*grid.csv
                            0,0,0,0,0,0,0,0
                            0,0,0,0,0,1,0,0
                            0,0,0,0,0,1,0,0
                            0,0,0,0,1,1,0,0
                            0,0,0,0,1,1,0,0
                            0,0,0,1,1,1,0,0
                            0,0,0,0,0,0,0,0
                            0,0,0,0,0,0,0,0
                            endfilecontents*
                            newcommand*ReadOutElement[4]%
                            pgfplotstablegetelem#2[index]#3of#1%
                            let#4pgfplotsretval

                            begindocument
                            pgfplotstableread[header=false,col sep=comma]grid.csvdatatable
                            tikzsetmystyle/.style=red,dashed
                            begintikzpicture
                            foreach Y [evaluate=Y as PrevY using int(Y-1),
                            evaluate=Y as NextY using int(Y+1)] in 1,...,6
                            foreach X [evaluate=X as PrevX using int(X-1),
                            evaluate=X as NextX using int(X+1)] in 1,...,6

                            ReadOutElementdatatableYXCurrent
                            ReadOutElementdatatablePrevYXAbove
                            ReadOutElementdatatableNextYXBelow
                            ReadOutElementdatatableYPrevXLeft
                            ReadOutElementdatatableYNextXRight
                            ifnumCurrent=1
                            typeoutY,X,Above
                            ifnumLeft=0
                            draw[mystyle] (1+X-0.95,8-Y-0.95) -- (1+X-0.95,8-Y-0.05);
                            fi
                            ifnumRight=0
                            draw[mystyle] (1+X-0.05,8-Y-0.95) -- (1+X-0.05,8-Y-0.05);
                            fi
                            ifnumAbove=0
                            draw[mystyle] (1+X-0.95,8-Y-0.05) -- (1+X-0.05,8-Y-0.05);
                            fi
                            ifnumBelow=0
                            draw[mystyle] (1+X-0.95,8-Y-0.95) -- (1+X-0.05,8-Y-0.95);
                            fi
                            fi


                            draw (0,0) grid (8,8);
                            endtikzpicture
                            enddocument


                            enter image description here






                            share|improve this answer












                            This is really just a fun proposal, which may or may not be useful. It seems to me that you want to draw a boundary around certain points of the grid. We could represent the grid by a matrix and say that the boundary should be drawn around the entries 1, all other entries are 0. Here is a very first version. It can and will be improved if that way turns out to make at least a tiny bit of sense. At the moment, the dimensions are hard coded and the shape cannot extend to the boundary of the grid, two things that can easily be fixed.



                            documentclass[border=3.14mm]standalone
                            usepackagetikz
                            usepackagepgfplotstable
                            usepackagefilecontents

                            beginfilecontents*grid.csv
                            0,0,0,0,0,0,0,0
                            0,0,0,0,0,1,0,0
                            0,0,0,0,0,1,0,0
                            0,0,0,0,1,1,0,0
                            0,0,0,0,1,1,0,0
                            0,0,0,1,1,1,0,0
                            0,0,0,0,0,0,0,0
                            0,0,0,0,0,0,0,0
                            endfilecontents*
                            newcommand*ReadOutElement[4]%
                            pgfplotstablegetelem#2[index]#3of#1%
                            let#4pgfplotsretval

                            begindocument
                            pgfplotstableread[header=false,col sep=comma]grid.csvdatatable
                            tikzsetmystyle/.style=red,dashed
                            begintikzpicture
                            foreach Y [evaluate=Y as PrevY using int(Y-1),
                            evaluate=Y as NextY using int(Y+1)] in 1,...,6
                            foreach X [evaluate=X as PrevX using int(X-1),
                            evaluate=X as NextX using int(X+1)] in 1,...,6

                            ReadOutElementdatatableYXCurrent
                            ReadOutElementdatatablePrevYXAbove
                            ReadOutElementdatatableNextYXBelow
                            ReadOutElementdatatableYPrevXLeft
                            ReadOutElementdatatableYNextXRight
                            ifnumCurrent=1
                            typeoutY,X,Above
                            ifnumLeft=0
                            draw[mystyle] (1+X-0.95,8-Y-0.95) -- (1+X-0.95,8-Y-0.05);
                            fi
                            ifnumRight=0
                            draw[mystyle] (1+X-0.05,8-Y-0.95) -- (1+X-0.05,8-Y-0.05);
                            fi
                            ifnumAbove=0
                            draw[mystyle] (1+X-0.95,8-Y-0.05) -- (1+X-0.05,8-Y-0.05);
                            fi
                            ifnumBelow=0
                            draw[mystyle] (1+X-0.95,8-Y-0.95) -- (1+X-0.05,8-Y-0.95);
                            fi
                            fi


                            draw (0,0) grid (8,8);
                            endtikzpicture
                            enddocument


                            enter image description here







                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Aug 9 at 20:27









                            marmot

                            51.4k350112




                            51.4k350112




















                                up vote
                                2
                                down vote













                                I don't know if this answer will fit the problem, but it shows another way for referencig cells and filling areas.



                                logicpuzzle is package which can be used to prepare what it's name means, logic puzzles loke sudokus, kakuros, ... But it also provides an easy way of drawing grids and some commands to frame and fill areas in these grids.



                                Environment beginlogicpuzzle[rows=8,columns=8]...endlogicpuzzle will draw a 8x8 grid.



                                Commands for drawing/filling areas inside the grid are frameareacolortikzpath,
                                fillareacolortikzpath, colorareacolortikzpath. In this commands the tikzpath is path where coordinates identifiy the lower left corner of each cell and where (1,1) is the lower left cell. Linear (--) or perpendicular (-|, |-) paths are allowed.



                                Another syntax is provided by xtikzpath which can be used inside tikzpath argument in previous commands.



                                fillareared!20xtikzpath616/1,8/1,4/1,2/1


                                fills cell (6,1). First ,two arguments represent the bottom left corner of starting position. Following pairs like 6/1 mean move right (6=right) 1 cell. The first number of the pair represents a direction according its position in numeric keyboard, while the second is the distance to move.



                                A complete code with some examples looks like:



                                documentclassarticle 
                                usepackagelogicpuzzle
                                begindocument
                                beginlogicpuzzle[rows=8,columns=8]
                                frameareared(7,8)-
                                fillareabrown!40(3,6)-
                                colorareablue!50(1,9)--(3,9)--(3,8)--(5,8)--(5,6)--(2,6)--(2,8)--(1,8)--cycle
                                fillareared!20xtikzpath616/2,8/2,6/1,8/3,4/1,2/2,4/1,2/2,4/1,2/1
                                endlogicpuzzle
                                enddocument


                                enter image description here






                                share|improve this answer
























                                  up vote
                                  2
                                  down vote













                                  I don't know if this answer will fit the problem, but it shows another way for referencig cells and filling areas.



                                  logicpuzzle is package which can be used to prepare what it's name means, logic puzzles loke sudokus, kakuros, ... But it also provides an easy way of drawing grids and some commands to frame and fill areas in these grids.



                                  Environment beginlogicpuzzle[rows=8,columns=8]...endlogicpuzzle will draw a 8x8 grid.



                                  Commands for drawing/filling areas inside the grid are frameareacolortikzpath,
                                  fillareacolortikzpath, colorareacolortikzpath. In this commands the tikzpath is path where coordinates identifiy the lower left corner of each cell and where (1,1) is the lower left cell. Linear (--) or perpendicular (-|, |-) paths are allowed.



                                  Another syntax is provided by xtikzpath which can be used inside tikzpath argument in previous commands.



                                  fillareared!20xtikzpath616/1,8/1,4/1,2/1


                                  fills cell (6,1). First ,two arguments represent the bottom left corner of starting position. Following pairs like 6/1 mean move right (6=right) 1 cell. The first number of the pair represents a direction according its position in numeric keyboard, while the second is the distance to move.



                                  A complete code with some examples looks like:



                                  documentclassarticle 
                                  usepackagelogicpuzzle
                                  begindocument
                                  beginlogicpuzzle[rows=8,columns=8]
                                  frameareared(7,8)-
                                  fillareabrown!40(3,6)-
                                  colorareablue!50(1,9)--(3,9)--(3,8)--(5,8)--(5,6)--(2,6)--(2,8)--(1,8)--cycle
                                  fillareared!20xtikzpath616/2,8/2,6/1,8/3,4/1,2/2,4/1,2/2,4/1,2/1
                                  endlogicpuzzle
                                  enddocument


                                  enter image description here






                                  share|improve this answer






















                                    up vote
                                    2
                                    down vote










                                    up vote
                                    2
                                    down vote









                                    I don't know if this answer will fit the problem, but it shows another way for referencig cells and filling areas.



                                    logicpuzzle is package which can be used to prepare what it's name means, logic puzzles loke sudokus, kakuros, ... But it also provides an easy way of drawing grids and some commands to frame and fill areas in these grids.



                                    Environment beginlogicpuzzle[rows=8,columns=8]...endlogicpuzzle will draw a 8x8 grid.



                                    Commands for drawing/filling areas inside the grid are frameareacolortikzpath,
                                    fillareacolortikzpath, colorareacolortikzpath. In this commands the tikzpath is path where coordinates identifiy the lower left corner of each cell and where (1,1) is the lower left cell. Linear (--) or perpendicular (-|, |-) paths are allowed.



                                    Another syntax is provided by xtikzpath which can be used inside tikzpath argument in previous commands.



                                    fillareared!20xtikzpath616/1,8/1,4/1,2/1


                                    fills cell (6,1). First ,two arguments represent the bottom left corner of starting position. Following pairs like 6/1 mean move right (6=right) 1 cell. The first number of the pair represents a direction according its position in numeric keyboard, while the second is the distance to move.



                                    A complete code with some examples looks like:



                                    documentclassarticle 
                                    usepackagelogicpuzzle
                                    begindocument
                                    beginlogicpuzzle[rows=8,columns=8]
                                    frameareared(7,8)-
                                    fillareabrown!40(3,6)-
                                    colorareablue!50(1,9)--(3,9)--(3,8)--(5,8)--(5,6)--(2,6)--(2,8)--(1,8)--cycle
                                    fillareared!20xtikzpath616/2,8/2,6/1,8/3,4/1,2/2,4/1,2/2,4/1,2/1
                                    endlogicpuzzle
                                    enddocument


                                    enter image description here






                                    share|improve this answer












                                    I don't know if this answer will fit the problem, but it shows another way for referencig cells and filling areas.



                                    logicpuzzle is package which can be used to prepare what it's name means, logic puzzles loke sudokus, kakuros, ... But it also provides an easy way of drawing grids and some commands to frame and fill areas in these grids.



                                    Environment beginlogicpuzzle[rows=8,columns=8]...endlogicpuzzle will draw a 8x8 grid.



                                    Commands for drawing/filling areas inside the grid are frameareacolortikzpath,
                                    fillareacolortikzpath, colorareacolortikzpath. In this commands the tikzpath is path where coordinates identifiy the lower left corner of each cell and where (1,1) is the lower left cell. Linear (--) or perpendicular (-|, |-) paths are allowed.



                                    Another syntax is provided by xtikzpath which can be used inside tikzpath argument in previous commands.



                                    fillareared!20xtikzpath616/1,8/1,4/1,2/1


                                    fills cell (6,1). First ,two arguments represent the bottom left corner of starting position. Following pairs like 6/1 mean move right (6=right) 1 cell. The first number of the pair represents a direction according its position in numeric keyboard, while the second is the distance to move.



                                    A complete code with some examples looks like:



                                    documentclassarticle 
                                    usepackagelogicpuzzle
                                    begindocument
                                    beginlogicpuzzle[rows=8,columns=8]
                                    frameareared(7,8)-
                                    fillareabrown!40(3,6)-
                                    colorareablue!50(1,9)--(3,9)--(3,8)--(5,8)--(5,6)--(2,6)--(2,8)--(1,8)--cycle
                                    fillareared!20xtikzpath616/2,8/2,6/1,8/3,4/1,2/2,4/1,2/2,4/1,2/1
                                    endlogicpuzzle
                                    enddocument


                                    enter image description here







                                    share|improve this answer












                                    share|improve this answer



                                    share|improve this answer










                                    answered Aug 10 at 12:43









                                    Ignasi

                                    86.3k4153286




                                    86.3k4153286






















                                         

                                        draft saved


                                        draft discarded


























                                         


                                        draft saved


                                        draft discarded














                                        StackExchange.ready(
                                        function ()
                                        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2ftex.stackexchange.com%2fquestions%2f445375%2ftikz-drawing-a-line-inside-of-certain-shape2%23new-answer', 'question_page');

                                        );

                                        Post as a guest













































































                                        這個網誌中的熱門文章

                                        How to combine Bézier curves to a surface?

                                        Carbon dioxide

                                        Why am i infinitely getting the same tweet with the Twitter Search API?