TikZ:如何在每个网格中点获取装饰?

TikZ:如何在每个网格中点获取装饰?

在每个盒子的每个中心都放置装饰的正确设置是什么?

在此处输入图片描述

\documentclass[margin=5mm, tikz]{standalone}
\usepackage{tikz}
\usetikzlibrary{decorations.markings}
\begin{document}

\begin{tikzpicture}[>=latex, scale=0.5]
\draw (0,0) grid (8,8); 

\tikzset{mymarks/.style={
->, 
decoration={markings,
mark=
between positions 0 and 1 step 0.7071 cm
with {\arrow{>}}},
postaction={decorate}
}
}

\draw[mymarks,red] (0.5,0.5) -- (7.5,7.5); 
\draw[mymarks,blue] (3.5,0.5) -- (3.5,5.5); 
\end{tikzpicture}
\end{document}

答案1

您正确地选择了1/sqrt(2)对角线路径的因子。为什么您认为这是垂直路径的正确因子?下面允许您选择因子。但是,它不会自动拨号。事实上,任意路径的自动解决方案都将是不平凡的,因为不能保证这些路径穿过单元格的中心。

\documentclass[margin=5mm, tikz]{standalone}
\usepackage{tikz}
\usetikzlibrary{decorations.markings}
\begin{document}

\begin{tikzpicture}[>=latex, scale=0.5]
\draw (0,0) grid (8,8); 

\tikzset{mymarks/.style={
->, 
decoration={markings,
mark=
between positions 0 and 0.99 step #1
with {\draw[-latex](0,0)--(0.1,0);}},
postaction={decorate}
}
}

\draw[mymarks=0.7071cm,red] (0.5,0.5) -- (7.5,7.5); 
\draw[mymarks=0.5cm,blue] (3.5,0.5) -- (3.5,5.5); 
\end{tikzpicture}
\end{document}

在此处输入图片描述

这是你的棋盘设置:你可以在特定方向添加箭头,其中的数字具有与数字键盘,即1表示向左下,2表示向下 ... 和9表示向右上。您还可以使用*来指示给定方向上的几步。以下示例对此进行了说明。

\documentclass[tikz,border=3.14mm]{standalone}
\tikzset{1/.style={insert path={edge[->] ++ (-1.1,-1.1) (-1,-1)}},
2/.style={insert path={edge[->] ++ (0,-1.1) ++(0,-1)}},
3/.style={insert path={edge[->] ++ (1.1,-1.1) ++(1,-1)}},
4/.style={insert path={edge[->] ++ (-1.1,0) ++(-1,0)}},
6/.style={insert path={edge[->] ++ (1.1,0) ++(1,0)}},
7/.style={insert path={edge[->] ++ (-1.1,1.1)++ (-1,1)}},
8/.style={insert path={edge[->] ++ (0,1.1) ++(0,1)}},
9/.style={insert path={edge[->] ++ (1.1,1.1)++ (1,1)}},
*/.style 2 args={insert path={foreach \X in {1,...,#1} {[#2]}}}}
\begin{document}
\begin{tikzpicture}[>=latex, scale=0.5]
\draw (0,0) grid (8,8); 
 \draw[red] (0.5,0.5) [*={7}{9}];
 \draw[blue]  (3.5,0.5) [*={5}{8}];
 \draw[green!70!black]  (6.5,0.5) [8,4,7,8,8,7,4,8];
\end{tikzpicture}
\end{document}

在此处输入图片描述

答案2

我使用命令来比较 x 和 y 坐标:

\pgfkeys{/tikz/savevalue/.code 2 args={\global\edef#1{#2}}}

% \move[options]{figure}{start}{end}
\newcommand\move[4][]{%
\path let \p1 = (#3) in coordinate (start) at (\x1,\y1) node[savevalue={\xStart}{\x1}, savevalue={\yStart}{\y1}]{};
\path let \p2 = (#4) in coordinate (end) at (\x2,\y2) node[savevalue={\xEnd}{\x2}, savevalue={\yEnd}{\y2}]{};
\pgfmathsetmacro\xTest{\xStart == \xEnd ? 1 : 0}
\pgfmathsetmacro\yTest{\yStart == \yEnd ? 1 : 0}
\pgfmathsetmacro\Test{\xTest==1 || \yTest== 1 ? "g" : "d"}
\draw[mymarks=\csname \Test \endcsname, #1] (start) -- (end) 
%node[align=left, anchor=west]{\xStart, \yStart;\\ \xEnd, \yEnd;\\ \xTest, \yTest, \Test} % Test
; 
\draw[fill=white] (start) circle(0.45) node{$#2$}; % Figur
}

\move[red]{D}{c-4}{g-8} 

(但这仅适用于直线和对角线)。

在此处输入图片描述

\documentclass[margin=5pt, tikz]{standalone}
\usepackage{tikz}
\usetikzlibrary{decorations.markings}
\usetikzlibrary{calc}
\usepackage{amsmath, amssymb}

\begin{document}
\pgfkeys{/tikz/savevalue/.code 2 args={\global\edef#1{#2}}}

\begin{tikzpicture}[>=latex, scale=0.5,
font=\small]
% KoSy
%\draw[] (0,0) grid (8,8);
\foreach[count=\S from 0, count=\s] \t in {a,b,...,h} {
\path[] (\S,0) -- (\s,0) node[midway,anchor=north] {\strut \t};
\path[] (0,\S) -- (0,\s) node[midway,anchor=east] {\s};
\foreach[count=\Y from 0] \y in {1,...,8} {%
% coordinates
\coordinate[] (\t-\y) at (\S+0.5,\y-0.5);  
% filling
\pgfmathparse{mod(\S+\Y,2) ? "lightgray!50" : "white"}
\colorlet{Farbe}{\pgfmathresult}
\path[fill=Farbe, draw=black] (\S,\Y) rectangle ++(1,1);    }%
}


% Moves
\pgfmathsetmacro\D{sqrt(2)/2}
\pgfmathsetlengthmacro\d{\D cm}
\pgfmathsetlengthmacro\g{0.5cm}
\tikzset{mymarks/.style={->, 
decoration={markings,
mark=
between positions 0 and 0.99 step #1
with {\draw[-latex](0,0)--(0.1,0);}},
postaction={decorate}
}}

% \move[options]{figure}{start}{end}
\newcommand\move[4][]{%
\path let \p1 = (#3) in coordinate (start) at (\x1,\y1) node[savevalue={\xStart}{\x1}, savevalue={\yStart}{\y1}]{};
\path let \p2 = (#4) in coordinate (end) at (\x2,\y2) node[savevalue={\xEnd}{\x2}, savevalue={\yEnd}{\y2}]{};
\pgfmathsetmacro\xTest{\xStart == \xEnd ? 1 : 0}
\pgfmathsetmacro\yTest{\yStart == \yEnd ? 1 : 0}
\pgfmathsetmacro\Test{\xTest==1 || \yTest== 1 ? "g" : "d"}
\draw[mymarks=\csname \Test \endcsname, #1] (start) -- (end) 
%node[align=left, anchor=west]{\xStart, \yStart;\\ \xEnd, \yEnd;\\ \xTest, \yTest, \Test} % Test
; 
\draw[fill=white] (start) circle(0.45) node{$#2$}; % Figur
}

% \move[red]{D}{c-4}{g-8} % Test
% \move{D}{c-4}{h-4} % Test
% \move{D}{c-4}{c-8} % Test

\foreach \Ends in {a-4,c-8,h-4,c-1,g-8,f-1,a-2,a-6}{ 
\move[red]{Q}{c-4}{\Ends}  }
\end{tikzpicture}
\end{document}

相关内容