我有两张尺寸相同的图像,想在每张图像上添加一个点网格。然后,相应的点应该用一条线连接起来。它应该大致看起来像这样(我只连接了上排和下排的点。)
\documentclass[demo]{standalone}
\usepackage{tikz}
\usetikzlibrary{positioning,decorations.pathmorphing}
\begin{document}
\tikz[
spec/.style={decoration=snake}
]{
\node[anchor=south west,inner sep=0] (F) at (0,0) {\includegraphics[width=2cm]{image.jpg}};
\node[above right=of F,anchor=south west,inner sep=0] (B) at (1,0) {\includegraphics[width=2cm]{image.jpg}};
\begin{scope}[x={(F.south east)},y={(F.north west)}]
%\draw[help lines,xstep=.1,ystep=.1] (0,0) grid (1,1);
\foreach \x in {1,...,9} {
\foreach \y in {1,...,9}{
\node[draw,circle,fill,blue,inner sep=0.5pt] (F\x\y) at (\x/10,\y/10) {};
}
}
\end{scope}
\begin{scope}[x={(B.south east)},y={(B.north west)}]
\foreach \x in {1,...,9} {
\foreach \y in {1,...,9}{
\node[draw,circle,fill,green,inner sep=0.5pt] (B\x\y) at (\x/10,\y/10) {};
\draw[decoration=snake] (B\x\y)--(F\x\y);
}
}
\end{scope}
}
\end{document}
线条看起来不错,但绿点网格不太对劲。我遗漏了什么?
答案1
图层在这里可能很有用。同样使用隐式xyz
坐标系:
\documentclass[tikz,margin=5]{standalone}
\usetikzlibrary{backgrounds,decorations.pathmorphing}
\begin{document}
\begin{tikzpicture}[x=(330:1.414cm),y=(90:2cm),z=(210:2cm),line cap=round]
\foreach \layer/\dotcolor [count=\k] in {background/green, main/blue}{
\begin{pgfonlayer}{\layer}
\path [fill=black, fill opacity=0.875]
(0,0,\k) -- (1,0,\k) -- (1,1,\k) -- (0,1,\k) -- cycle;
\foreach \i in {1,...,4}\foreach \j in {1,...,4}
\path [fill=\dotcolor] (\j*.2, \i*.2, \k) circle [radius=.0125]
coordinate (n-\i-\j-\k);
\end{pgfonlayer}
}
\begin{pgfonlayer}{background}
\foreach \i in {1,...,4}\foreach \j in {1,...,4}
\draw [gray, decoration={snake, amplitude=1}, decorate]
(n-\i-\j-1) -- (n-\i-\j-2);
\end{pgfonlayer}
\end{tikzpicture}
\end{document}
答案2
您正在设置 x 和 y 向量以适应图片。这对第一张图片有效,因为它位于 (0,0)。但对于第二张图片则不然,因为它没有位于原点。绘制向量(参见下面代码中的注释部分)显示了这一点:
这里您的绿点完美地定位在一个不完美的 1x1“矩形”内。
可以通过在设置向量 xB 和 yB 时减去 B 的锚点来纠正这个问题:
x={($(B.south east) - (B.south west)$)},
y={($(B.north west) - (B.south west)$)}
另外,必须将锚点添加到绿点的位置以将它们移到第二张图片中:
($(\x/10,\y/10) + (B.south west)$)
其结果是:
代码:
\documentclass[demo,border=1mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{positioning,decorations.pathmorphing,calc}
\begin{document}
\tikz[
spec/.style={decoration=snake}
]{
\node[anchor=south west,inner sep=0] (F) at (0,0) {\includegraphics[width=2cm]{image.jpg}};
\node[above right=of F,anchor=south west,inner sep=0] (B) at (1,0) {\includegraphics[width=2cm]{image.jpg}};
\begin{scope}[x={(F.south east)},y={(F.north west)}]
%\draw[help lines,xstep=.1,ystep=.1] (0,0) grid (1,1);
\foreach \x in {1,...,9} {
\foreach \y in {1,...,9}{
\node[draw,circle,fill,blue,inner sep=0.5pt] (F\x\y) at (\x/10,\y/10) {};
}
}
% draw x and y vector
%\draw[cyan,->] (0,0) -- (1,0) node[right] {$xF$};
%\draw[cyan,->] (0,0) -- (0,1) node[above] {$yF$};
\end{scope}
\begin{scope}[x={($(B.south east) - (B.south west)$)},y={($(B.north west) - (B.south west)$)}]
%\begin{scope}[x={(B.south east)},y={(B.north west)}]
\foreach \x in {1,...,9} {
\foreach \y in {1,...,9}{
\node[draw,circle,fill,green,inner sep=0.5pt] (B\x\y) at ($(\x/10,\y/10) + (B.south west)$) {};
% \node[draw,circle,fill,green,inner sep=0.5pt] (B\x\y) at (\x/10,\y/10) {};
\draw[red,decoration=snake] (B\x\y)--(F\x\y);
}
}
% draw x and y vector
%\draw[cyan,->] (0,0) -- (1,0) node[right] {$xB$};
%\draw[cyan,->] (0,0) -- (0,1) node[above] {$yB$};
% lines to show the 1x1 rectangle
%\draw[cyan] (1,0) -- (1,1);
%\draw[cyan] (0,1) -- (1,1);
\end{scope}
}
\end{document}