
下列的使用 TikZ 在圖像上繪圖,我有很多成功的例子蒂克茲。
然而,現在我有一個tikzpicture
環境一些圖形,我想在每個圖形上繪圖。然而,似乎提供的解決方案使用 TikZ 在圖像上繪圖僅適用於以 (0, 0) 為中心的圖片,因為如果我嘗試在第二張圖片上繪製,座標不會按預期運行:
\documentclass[final, 12pt]{standalone}
\usepackage{tikz}
\newcommand{\helplines}[0]{
\draw[help lines,semithick,xstep=.1,ystep=.1] (0,0) grid (1,1);
\foreach \x in {0,1,...,9} { \node [anchor=north] at (\x/10,0) {0.\x}; }
\foreach \y in {0,1,...,9} { \node [anchor=east] at (0,\y/10) {0.\y}; }
}
\begin{document}%
\begin{tikzpicture}%
\node[anchor=south west,inner sep=0] (image1) at (0, 0)
{\rule{3cm}{3cm}};
\begin{scope}[x={(image1.south east)},y={(image1.north west)}]
% draw stuff
%\helplines
\end{scope}
\node[anchor=south west,inner sep=0] (image2) at (image1.south east)
{\rule{3cm}{3cm}};
\begin{scope}[x={(image2.south east)},y={(image2.north west)}]
\helplines
% draw more stuff, but coordinates do not map as desired
\end{scope}
\end{tikzpicture}
\end{document}
結果:
我想要一個變換後的座標系,其原點位於圖片的左下角,點 (1, 1) 位於右上角。這適用於位於 (0, 0) 的圖片,但不適用於緊鄰右側的圖片。為什麼不可以,如何才能達到預期的效果?
答案1
這是因為您正在縮放單位向量,x=... , y=....
但第二個影像中的參數不是正交向量。因為image2
在其他地方,它們不是座標類型(x0,0)
。(0,y0)
相反,它們是具有非零條目的向量。這就是為什麼你的座標是對角線而不是水平/垂直的。
x
要執行相同的操作,您需要將相應的或組件清除y
。例子
\documentclass[final, 12pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{calc}
\newcommand{\helplines}[0]{
\draw[help lines,semithick,xstep=.1,ystep=.1] (0,0) grid (1,1);
\foreach \x in {0,1,...,9} { \node [anchor=north] at (\x/10,0) {\tiny .\x}; }
\foreach \y in {0,1,...,9} { \node [anchor=east] at (0,\y/10) {\tiny .\y}; }
}
\begin{document}%
\begin{tikzpicture}%
\node[anchor=south west,inner sep=0] (image1) at (0, 0)
{\color{red}\rule{3cm}{3cm}};
\begin{scope}[x={(image1.south east)},y={(image1.north west)}]
% draw stuff
\helplines
\end{scope}
\node[anchor=south west,inner sep=0] (image2) at (image1.south east)
{\color{blue}\rule{3cm}{3cm}};
\begin{scope}[
x={($(image2.north east)-(image2.north west)$)},
y={($(image2.north west)-(image2.south west)$)},
shift={(image2.south west)}]
\helplines
\end{scope}
\end{tikzpicture}
\end{document}
在這裡,我使用該calc
庫來獲取圖像的寬度和高度。但是您可以透過將圖像也包含在範圍內來避免所有這些問題,以便所有內容都在本地定義相同。然後你改變這些範圍。例子:
\documentclass[tikz]{standalone}
\newcommand{\helplines}[0]{
\draw[help lines,semithick,xstep=.1,ystep=.1] (0,0) grid (1,1);
\foreach \x in {0,1,...,9} { \node [anchor=north] at (\x/10,0) {\tiny .\x}; }
\foreach \y in {0,1,...,9} { \node [anchor=east] at (0,\y/10) {\tiny .\y}; }
}
\begin{document}%
\begin{tikzpicture}%
\node[anchor=south west,inner sep=0] (image1) at (0, 0)
{\color{red}\rule{3cm}{3cm}};
\begin{scope}[x={(image1.south east)},y={(image1.north west)}]
% draw stuff
\helplines
\end{scope}
\begin{scope}[shift={(image1.south east)}] % Shift the scope
\node[anchor=south west,inner sep=0] (image2) at (0,0) %still at the origin of this scope
{\color{blue}\rule{3cm}{3cm}};
\begin{scope}[x={(image2.south east)},y={(image2.north west)}]
\helplines
\end{scope}
\end{scope}
\end{tikzpicture}
\end{document}
這給出了相同的輸出。