数组\xcoordinates
和\ycoordinates
定义网格上的点。我只是试图用短线段按顺序连接所有点。
\documentclass[12pt]{article}
\usepackage{tikz}
\usepackage[active,tightpage]{preview}%
\PreviewEnvironment{tikzpicture}%
\setlength\PreviewBorder{0.1cm}%
\usetikzlibrary{positioning}%
\usetikzlibrary{shapes}%
\usetikzlibrary{arrows}%
\usetikzlibrary{calc}%
\begin{document}
\def\thexslant{-1}
\def\theyslant{0.5}
\newlength\markeroffset
\setlength\markeroffset{0.20cm}
% particle diameter
\def\markerdiameter{0.2cm}
\begin{tikzpicture}[scale=.9,every node/.style={minimum size=1cm},on grid]
\begin{scope}[
yshift=0cm,every node/.append style={
yslant=\theyslant,xslant=\thexslant},yslant=\theyslant,xslant=\thexslant]
\fill[white,fill opacity=.9] (0,0) rectangle (5,5);
\draw[black,very thick] (0,0) rectangle (5,5);
\draw[step=10mm, black] (0,0) grid (5,5);
\foreach \i / \j in {1/1, 1/2, 1/3, 1/4, 1/5, 2/5, 3/5, 3/4, 3/3, 3/2, 3/1, 4/1, 5/1, 5/2, 5/3, 5/4, 5/5}{%
\fill[black] (\i - 0.5, \j - 0.5) circle (\markerdiameter/2);%
\node at (\i - 0.5, \j - 0.5) [blue,xshift=\markeroffset, yshift=\markeroffset,font=\tiny] () {$\i,\j$};
}%
% draw a red line indicating the path of the survey
\def\xcoordinates{{1,1,1,1,1,2,3,3,3,3,3,4,5,5,5,5,5}}
\def\ycoordinates{{1,2,3,4,5,5,5,4,3,2,1,1,1,2,3,4,5}}
% ****** uncomment this to reveal problem
%\foreach \i in {1,...,15}{
%\coordinate (A) at (\pgfmathparse{\xcoordinates[\i]} \pgfmathresult, \pgfmathparse{\ycoordinates[\i]} \pgfmathresult)-(0.5,0.5);
%\coordinate (B) at (\pgfmathparse{\xcoordinates[\i+1]} \pgfmathresult, \pgfmathparse{\ycoordinates[\i+1]} \pgfmathresult)-(0.5,0.5);
%\draw[gray] (B) -- (3,3);
%}
\end{scope}
\end{tikzpicture}
\end{document}
我不确定是否可以做到这一点\xcoordinates[\i+1]
。当我编译此代码时,出现错误:
! Incomplete \iffalse; all text was ignored after line 83.
具有更多功能的相同 mwe 如下所示:
\documentclass[12pt]{article}
\usepackage{tikz}
\usepackage[active,tightpage]{preview}%
\PreviewEnvironment{tikzpicture}%
\setlength\PreviewBorder{0.1cm}%
\usetikzlibrary{positioning}%
\usetikzlibrary{shapes}%
\usetikzlibrary{arrows}%
\usetikzlibrary{calc}%
\begin{document}
\def\thexslant{-1}
\def\theyslant{0.5}
\newlength\markeroffset
\setlength\markeroffset{0.20cm}
% particle diameter
\def\markerdiameter{0.2cm}
\begin{tikzpicture}[scale=.9,every node/.style={minimum size=1cm},on grid]
\begin{scope}[
yshift=0cm,every node/.append style={
yslant=\theyslant,xslant=\thexslant},yslant=\theyslant,xslant=\thexslant]
\fill[white,fill opacity=.9] (0,0) rectangle (5,5);
\draw[black,very thick] (0,0) rectangle (5,5);
\draw[step=10mm, black] (0,0) grid (5,5);
\foreach \i / \j in {1/1, 1/2, 1/3, 1/4, 1/5, 2/5, 3/5, 3/4, 3/3, 3/2, 3/1, 4/1, 5/1, 5/2, 5/3, 5/4, 5/5}{%
\fill[black] (\i - 0.5, \j - 0.5) circle (\markerdiameter/2);%
\node at (\i - 0.5, \j - 0.5) [blue,xshift=\markeroffset, yshift=\markeroffset,font=\tiny] () {$\i,\j$};
}%
% draw a red line indicating the path of the survey
\def\xcoordinates{{1,1,1,1,1,2,3,3,3,3,3,4,5,5,5,5,5}}
\def\ycoordinates{{1,2,3,4,5,5,5,4,3,2,1,1,1,2,3,4,5}}
% suggestion by marmot
\draw[red, very thick] plot[variable=\x,samples at={0,...,16}] ({\xcoordinates[\x]-0.5},{\ycoordinates[\x]-0.5});
% from the first point to every other point
\coordinate (A) at (0.5,0.5);
\foreach \position in {(1,2), (1,3), (1,4), (1,5), (2,5), (3,5), (3,4), (3,3), (3,2), (3,1), (4,1), (5,1), (5,2), (5,3), (5,4), (5,5)}{
\draw[gray] \position+(-0.5,-0.5) -- (A);
}%
% and here is how we draw all the possible connections
\foreach \xa / \ya [count=\i] in {1/1, 1/2, 1/3, 1/4, 1/5, 2/5, 3/5, 3/4, 3/3, 3/2, 3/1, 4/1, 5/1, 5/2, 5/3, 5/4, 5/5}
\foreach \xb / \yb [count=\j] in {1/1, 1/2, 1/3, 1/4, 1/5, 2/5, 3/5, 3/4, 3/3, 3/2, 3/1, 4/1, 5/1, 5/2, 5/3, 5/4, 5/5}
{
\ifnum \i=\j
\else
\draw[gray] (\xa-0.5,\ya-0.5) -- (\xb-0.5,\yb-0.5);
\fi
}%
\end{scope}
\end{tikzpicture}
\end{document}
这确实很丑陋(快速完成,边学边做),因为我用三种不同的方式定义测量点/坐标。它们应该只定义一次。
任何有关结构的建议都值得赞赏。
答案1
你不需要任何东西\pgfmathparse
来实现这一点。(而且你不能以你想要的方式减去坐标。)
\documentclass[tikz,border=3.14mm]{standalone}
\begin{document}
\begin{tikzpicture}
\def\xcoordinates{{1,1,1,1,1,2,3,3,3,3,3,4,5,5,5,5,5}}
\def\ycoordinates{{1,2,3,4,5,5,5,4,3,2,1,1,1,2,3,4,5}}
\foreach \i [evaluate=\i as \j using {int(\i+1)}] in {1,...,15} {
\draw[gray] ({\xcoordinates[\i]-0.5},{\ycoordinates[\i]-0.5}) -- ({\xcoordinates[\j]-0.5},{\ycoordinates[\j]-0.5});
}
\end{tikzpicture}
\end{document}
如果您确实坚持在很大程度上保留原始语法,则可以使用calc
。然后我想说的是,如果您创建命名坐标,使它们唯一可能会有所帮助。
\documentclass[tikz,border=3.14mm]{standalone}
\usetikzlibrary{calc}
\begin{document}
\begin{tikzpicture}
\def\xcoordinates{{1,1,1,1,1,2,3,3,3,3,3,4,5,5,5,5,5}}
\def\ycoordinates{{1,2,3,4,5,5,5,4,3,2,1,1,1,2,3,4,5}}
\foreach \i in {1,...,15} {
\coordinate (A-\i) at ($({\xcoordinates[\i]},{\ycoordinates[\i]} )-(0.5,0.5)$);
\coordinate (B-\i) at ($({\xcoordinates[\i+1]} ,{\ycoordinates[\i+1]})-(0.5,0.5)$);
\draw[gray] (A-\i) -- (B-\i);
}
\end{tikzpicture}
\end{document}
当然,您可以更简单地获得(几乎)相同的结果(仍然使用您的列表)。
\documentclass[tikz,border=3.14mm]{standalone}
\begin{document}
\begin{tikzpicture}
\def\xcoordinates{{1,1,1,1,1,2,3,3,3,3,3,4,5,5,5,5,5}}
\def\ycoordinates{{1,2,3,4,5,5,5,4,3,2,1,1,1,2,3,4,5}}
\draw[gray] plot[variable=\x,samples at={1,...,16}] ({\xcoordinates[\x]-0.5},{\ycoordinates[\x]-0.5});
\end{tikzpicture}
\end{document}