如何在 tikz 中绘制以下特殊表格?

如何在 tikz 中绘制以下特殊表格?

我不知道如何在 tikz 中绘制以下特殊表格?我知道 tikz 的一些基本命令,但我很困惑!任何建议都值得赞赏。

更新:WME:

    \documentclass{article}
    \usepackage{tikz}

    \begin{document}
    \begin{tikzpicture}
    \draw (0,0)--(0,10)--(10,10)--(10,0)--(0,0);
    \draw (0,1)--(10,1);
    \draw (0,2)--(10,2);
    \draw (0,3)--(10,3);
    \draw (0,4)--(10,4);
    \draw (0,5)--(10,5);
    \draw (0,6)--(10,6);
    \draw (0,7)--(10,7);
    \draw (0,8)--(10,8);
    \draw (0,9)--(10,9);
    %---------------------
    \draw (1,0)--(1,10);
    \draw (2,0)--(2,10);
    \draw (3,0)--(3,10);
    \draw (4,0)--(4,10);
    \draw (5,0)--(5,10);
    \draw (6,0)--(6,10);
    \draw (7,0)--(7,10);
    \draw (8,0)--(8,10);
    \draw (9,0)--(9,10);
    %------------------------

    \draw[very thick](0,10)--(10,10)--(10,0)--(0,0)--(0,9)-- (9,9)--(9,1)--        (1,1)--(1,8)--(8,8)--(2,8)--(8,8)--(8,2)--(2,2)--(2,7)--(7,7)--(7,3)--(3,3)--(3,6)   --(6,6)--(6,4)--(4,4)--(4,5)   --(5,5);
    \end{tikzpicture}
    \end{document}

在此处输入图片描述

答案1

我的解决方案使用螺旋码我将其翻译成了 lua,并做了一些修改。

\documentclass{article}

\usepackage{luacode}
\usepackage{tikz}

\begin{document}

\begin{tikzpicture}[scale=1]
  \draw[step=1,very thin,xshift=0.5cm,yshift=0.5cm] (-5,-5) grid (5,5);

  % position numbers
  \begin{luacode}
    function isPrime(n)
      primes = {}
      if n <= 0 then
        return false
      end
      if n <= 2 then
        return true
      end
      if n % 2 == 0 then
        return false
      end
      for i = 3,n/2,2 do
        if n % i == 0 then
          return false
        end
      end
      return true
    end

    NUM = 100
    di = 1
    dj = 0
    i = 0
    j = 0
    segment_passed = 0
    segment_length = 1
    for k=0, (NUM-1) do
      if isPrime(k+1) then
        tex.sprint("\\draw ("..(i-0.5)..","..(j-0.5)..") -- ("..(i+0.5)..","..(j+0.5)..");")
        tex.sprint("\\draw ("..(i-0.5)..","..(j+0.5)..") -- ("..(i+0.5)..","..(j-0.5)..");")
        tex.sprint("\\draw node at ("..i..","..j..") {\\colorbox{white}{"..(k+1).."}};")
      else
        tex.sprint("\\draw node at ("..i..","..j..") {"..(k+1).."};")
      end
      i = i + di
      j = j + dj
      segment_passed = segment_passed + 1

      if segment_passed == segment_length then
        segment_passed = 0
        tmp = di
        di = -dj
        dj = tmp
        if dj == 0 then
          segment_length = segment_length + 1
        end
      end
    end
  \end{luacode}

  % draw spiral
  \begin{luacode}
    NUM = 120
    di = -1
    dj = 0
    i = 0
    j = 0
    segment_passed = 0
    segment_length = 1
    for k=0, (NUM-1) do
      tex.sprint("\\draw[ultra thick,line cap=round] ("..(i+0.5)..","..(j+0.5)..") -- ")
      i = i + di
      j = j + dj
      tex.sprint("("..(i+0.5)..","..(j+0.5)..");")
      segment_passed = segment_passed + 1

      if segment_passed == segment_length then
        segment_passed = 0
        tmp = di
        di = -dj
        dj = tmp
        if dj == 0 then
          segment_length = segment_length + 1
        end
      end
    end
  \end{luacode}

\end{tikzpicture}

\end{document}

在此处输入图片描述

更新 我发现这幅素描可能属于乌兰姆螺旋。因此,某些素数的对角线应该表示这种螺旋内的对角线。

只是为了好玩,我修改了我的示例代码来生成类似于维基百科文章的图形。(我希望它不会偏离主题太多)

\documentclass{article}

\usepackage{luacode}
\usepackage{tikz}

\begin{document}

\begin{tikzpicture}[scale=0.05]
  % position circles
  \begin{luacode}
    function isPrime(n)
      primes = {}
      if n <= 0 then
        return false
      end
      if n <= 2 then
        return true
      end
      if n % 2 == 0 then
        return false
      end
      for i = 3,n/2,2 do
        if n % i == 0 then
          return false
        end
      end
      return true
    end

    NUM = 40000
    di = 1
    dj = 0
    i = 0
    j = 0
    segment_passed = 0
    segment_length = 1
    for k=0, (NUM-1) do
      if isPrime(k+1) then
        tex.sprint("\\filldraw ("..i..","..j..") circle (0.3);")
      else
        -- tex.sprint("\\draw node at ("..i..","..j..") {"..(k+1).."};")
      end
      i = i + di
      j = j + dj
      segment_passed = segment_passed + 1

      if segment_passed == segment_length then
        segment_passed = 0
        tmp = di
        di = -dj
        dj = tmp
        if dj == 0 then
          segment_length = segment_length + 1
        end
      end
    end
  \end{luacode}

\end{tikzpicture}

\end{document}

在此处输入图片描述

答案2

帮助你……

    \documentclass{article}
    \usepackage{tikz}

    \begin{document}
    \begin{tikzpicture}
    \foreach \x in {0,...,10}
      {
      \draw(0,\x)--(10,\x);
      \draw(\x,0)--(\x,10);
      }
    \draw[ultra thick,red] (0,10)--(10,10);
   \foreach \x in {9,7,...,1} 
     {
     \draw[ultra thick,red] (0.5*\x+5.5,0.5*\x+5.5)--(0.5*\x+5.5,4.5-0.5*\x)--(4.5-0.5*\x,4.5-0.5*\x)
       -- (4.5-0.5*\x,4.5+0.5*\x) -- (4.5+0.5*\x,4.5+0.5*\x);
    }
    \end{tikzpicture}
    \end{document}

在此处输入图片描述

答案3

真丢脸,我没认出质数!

这是我第一次尝试 LuaLatex。这是一种有点奇怪的语言,不是吗?

输出

在此处输入图片描述

lua

--- makeMatrix.lua
function zeroTable(n)
  myTable={}
  for i=1,n do
    myTable[i]={}
    for j=1,n do
      myTable[i][j] = "0"
    end 
  end 
  return myTable
end

myListCrossed = { 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97}
isCrossed ={}
for key,value in pairs(myListCrossed) do
  isCrossed[value]=true
end

function nodeContent(a)
  if isCrossed[a] then
    option="|[crossed]| "
  else
    option=""
  end
  return option .. a
end

function table(n)
  myTable=zeroTable(n)
  square=math.floor(n^2)
  back=false
  vertical=false
  dir={}
  dir[1]=1
  dir[2]=0
  a=square-1
  myTable[1][1]=square
  i=1
  j=1
  repeat
    repeat
      i=i+dir[1]
      j=j+dir[2]
      myTable[i][j] = nodeContent(a)
      a=a-1
    --print("i: " .. i ..  "  j: " .. j )
    until (j==1+i and j+i<n+1) or (j==i and j+i>n+1) or (i+j==n+1)
    tmp=-dir[2]
    dir[2]= dir[1]
    dir[1]=tmp
  until a<1
  return myTable
end

function printTable(myTable,n,printFunction)
  doubleBackslash="\\\\"
  for i=1,n do
    str=""
    for j=1,n do
      str= str .. myTable[j][i] .. " & "
    end
    str=string.gsub(str, "& $", doubleBackslash)
    printFunction(str)
  end
end

n=10
myTable=table(n)

--printTable(myTable,n,print) -- debug prints to console 
--printTable(myTable,n,tex.print) -- what we'll call the function as

LaTeX

\RequirePackage{luatex85}
\documentclass[tikz]{standalone}
\usetikzlibrary{matrix}
\directlua{dofile("makeMatrix.lua")}
\begin{document}
\tikzset
{
  crossed/.style=
  {
    fill=blue!30!black,
    fill opacity=.4,
    text opacity=1,
  },
  square matrix/.style=
  {
    matrix of nodes,
    column sep=-\pgflinewidth, row sep=-\pgflinewidth,
    nodes=
    {
      draw=gray,
      minimum height=#1,
      anchor=center,
      text width=#1,
      align=center,
      inner sep=0pt
    },
  },
  square matrix/.default=1cm
}

\begin{tikzpicture}

  \node (myMatrix)
  [
    square matrix,
  ]
  {
    \directlua{printTable(myTable,n,tex.print)}
  };

  \draw [very thick, red]
    (myMatrix-1-1.north west)
      -| ++(10,-10)
      -| ++(-10,9)
    foreach \x in {8,6,...,1}
    {
      -| ++(\x+1,-\x)
      -| ++(-\x,\x-1)
    }
    -- ++(1,0) ;
\end{tikzpicture}
\end{document}

答案4

使用 Tikz 的另一种解决方案是

\documentclass[border={10pt}]{standalone}

\usepackage{tikz}
\usetikzlibrary{shapes.misc}
\begin{document}
    \begin{tikzpicture}
    [%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    Container/.style={rectangle,draw=black,thick, minimum size=1cm},
    XcrossBox/.style={cross out, draw=black, minimum size=1cm,thick},
    LeftStrikeBox/.style={strike out, rotate=90, draw=black, minimum size=1cm,thick},
    RightStrikeBox/.style={strike out, draw=black, minimum size=1cm,thick},
    box/.style={rectangle,draw=white,fill=white, minimum size=.3cm},
    ]
    ]%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    \foreach \x in {0,1,...,10}{
        \foreach \y in {0,1,...,10}
        \node[Container] at (\x,\y){};
    }
    \node[XcrossBox] at (5,5) {};  
    \node[box] at (5,5){5};  
    \node[LeftStrikeBox] at (4,4) {};
    \node[box] at (4,4){8};
    \node[RightStrikeBox] at (2,2) {};
    \node[box] at (2,2){6};
    \end{tikzpicture}

\end{document}

输出为 在此处输入图片描述

我认为填补其余部分很简单。

相关内容