看看下面的代码,当我想从右到左或从上到下绘制网格时,即使使用xstep=-1
或ystep=-1
(正如 Jake 在中的回答中所建议的那样) ,也不会绘制垂直线或水平线网格缺少水平线)。
我错过了什么?我正在使用 TikZ 的 CVS 版本。
\documentclass[border=2mm]{standalone}
\usepackage{tikz}
\newcommand{\arrow}[2]{\draw[->,blue,thick,shorten <=5mm, shorten >=5mm] #1 -- #2 ;}
\begin{document}
\begin{tikzpicture}
\draw[help lines] (-2,-2) grid (2,2);
\draw[red,fill] (0,0) circle (1mm) node[below right] {(0,0)};
\node[anchor=south,align=center] at (0,2){(-2,-2) to (2,2)};
\arrow{(-2,-2)}{(2,2)}
\end{tikzpicture}
\begin{tikzpicture}
\draw[help lines] (-2,2) grid[ystep=-1] (2,-2);
\arrow{(-2,2)}{(2,-2)}
\draw[red,fill] (0,0) circle (1mm) node[above right] {(0,0)};
\node[anchor=south,align=center] at (0,2){(-2,2) to (2,-2)};
\end{tikzpicture}
\begin{tikzpicture}
\draw[help lines] (2,-2) grid[xstep=-1] (-2,2);
\arrow{(2,-2)}{(-2,2)}
\draw[red,fill] (0,0) circle (1mm) node[above right] {(0,0)};
\node[anchor=south,align=center] at (0,2){(2,-2) to (-2,2)};
\end{tikzpicture}
\begin{tikzpicture}
\draw[help lines] (2,2) grid[xstep=-1,ystep=-1] (-2,-2);
\arrow{(2,2)}{(-2,-2)}
\draw[red,fill] (0,0) circle (1mm) node[below right] {(0,0)};
\node[anchor=south,align=center] at (0,2){(2,2) to (-2,-2)};
\end{tikzpicture}
\end{document}
答案1
编辑:此问题现已在 TikZ/PGF 的当前开发版本 (CVS) 中得到修复。修复方法不是下面提出的,而是来自 Till Tantau 的评论:
在 CVS 中已修复。但是,负增量(仍然)不允许。相反,pgfpathgrid 命令的两个参数现在被视为矩形的两个角,而不是明确的左下角和右上角。这以人们期望的方式解决了问题。
如果我没有弄错的话,我已经修补了在文件\pgfpathgrid
中找到的命令pgfcorepathconstruct.code.tex
。但如果它在任何其他我可能跳过的情况下失败,请告诉我。
这样,您就不需要使用负步长,因为它会相应增加。
简而言之,我检查了起点是否大于终点,然后乘以几个不等式-1
作为\c@pgf@counta
条件变量。
\documentclass{article}
\usepackage{tikz}
\makeatletter
\def\pgfpathgrid{\pgfutil@ifnextchar[{\pgf@pathgrid}{\pgf@pathgrid[]}}
\def\pgf@pathgrid[#1]#2#3{%
\pgfset{#1}%
\pgfmathsetlength\pgf@xc{\pgfkeysvalueof{/pgf/stepx}}%
\pgfmathsetlength\pgf@yc{\pgfkeysvalueof{/pgf/stepy}}%
\pgf@process{#3}%
\pgf@xb=\pgf@x%
\pgf@yb=\pgf@y%
\pgf@process{#2}%
\pgf@xa=\pgf@x\relax%
\pgf@ya=\pgf@y\relax%
{%
% compute bounding box
% first corner
\pgf@x=\pgf@xb%
\pgf@y=\pgf@yb%
\pgf@pos@transform{\pgf@x}{\pgf@y}%
\pgf@protocolsizes{\pgf@x}{\pgf@y}%
% second corner
\pgf@x=\pgf@xb%
\pgf@y=\pgf@ya%
\pgf@pos@transform{\pgf@x}{\pgf@y}%
\pgf@protocolsizes{\pgf@x}{\pgf@y}%
% third corner
\pgf@x=\pgf@xa%
\pgf@y=\pgf@yb%
\pgf@pos@transform{\pgf@x}{\pgf@y}%
\pgf@protocolsizes{\pgf@x}{\pgf@y}%
% fourth corner
\pgf@x=\pgf@xa%
\pgf@y=\pgf@ya%
\pgf@pos@transform{\pgf@x}{\pgf@y}%
\pgf@protocolsizes{\pgf@x}{\pgf@y}%
}%
\c@pgf@counta=\pgf@y\relax% Truncate the start y coordinate to integer
\c@pgf@countb=\pgf@yc\relax% Truncate the step size to integer
\divide\c@pgf@counta by\c@pgf@countb\relax% Truncate the ratio
\pgf@y=\c@pgf@counta\pgf@yc\relax% % Find the closest integer-multiple of step size to the start
\ifdim\pgf@ya>\pgf@yb% If the start point is larger than finish
\c@pgf@counta=-1\relax
\else % If everything is fine
\c@pgf@counta=1\relax
\fi
\ifdim\the\c@pgf@counta\pgf@y<\the\c@pgf@counta\pgf@ya% If for some reason it goes too far
\advance\pgf@y by\the\c@pgf@counta\pgf@yc% take back one step size
\fi%
\loop% horizontal lines
{%
\pgf@xa=\pgf@x%
\pgf@ya=\pgf@y%
\pgf@pos@transform{\pgf@xa}{\pgf@ya}
\pgfsyssoftpath@moveto{\the\pgf@xa}{\the\pgf@ya}%
\pgf@xa=\pgf@xb%
\pgf@ya=\pgf@y%
\pgf@pos@transform{\pgf@xa}{\pgf@ya}
\pgfsyssoftpath@lineto{\the\pgf@xa}{\the\pgf@ya}%
}%
\advance\pgf@y by\the\c@pgf@counta\pgf@yc% Increment in the - or + direction
\ifdim\the\c@pgf@counta\pgf@y<\the\c@pgf@counta\pgf@yb% Also compare with the correct sign.
\repeat%
\advance\pgf@y by 0.01\dimexpr0pt-(1pt)*\c@pgf@counta\relax%
\ifdim\the\c@pgf@counta\pgf@y<\the\c@pgf@counta\pgf@yb
{%
\pgf@xa=\pgf@x%
\pgf@ya=\pgf@y%
\pgf@pos@transform{\pgf@xa}{\pgf@ya}
\pgfsyssoftpath@moveto{\the\pgf@xa}{\the\pgf@ya}%
\pgf@xa=\pgf@xb%
\pgf@ya=\pgf@y%
\pgf@pos@transform{\pgf@xa}{\pgf@ya}
\pgfsyssoftpath@lineto{\the\pgf@xa}{\the\pgf@ya}%
}%
\fi%
\c@pgf@counta=\pgf@x\relax%
\c@pgf@countb=\pgf@xc\relax%
\divide\c@pgf@counta by\c@pgf@countb\relax%
\pgf@x=\c@pgf@counta\pgf@xc\relax%
\ifdim\pgf@xa>\pgf@xb% If the start point is larger than finish
\c@pgf@counta=-1\relax
\else % If everything is fine
\c@pgf@counta=1\relax
\fi
\ifdim\the\c@pgf@counta\pgf@x<\the\c@pgf@counta\pgf@xa%
\advance\pgf@x by\the\c@pgf@counta\pgf@xc%
\fi%
\loop% vertical lines
{%
\pgf@xc=\pgf@x%
\pgf@yc=\pgf@ya%
\pgf@pos@transform{\pgf@xc}{\pgf@yc}
\pgfsyssoftpath@moveto{\the\pgf@xc}{\the\pgf@yc}%
\pgf@xc=\pgf@x%
\pgf@yc=\pgf@yb%
\pgf@pos@transform{\pgf@xc}{\pgf@yc}
\pgfsyssoftpath@lineto{\the\pgf@xc}{\the\pgf@yc}%
}%
\advance\pgf@x by\the\c@pgf@counta\pgf@xc% Increment in the - or + direction
\ifdim\the\c@pgf@counta\pgf@x<\the\c@pgf@counta\pgf@xb% Also compare with the correct sign.
\repeat%
\advance\pgf@x by 0.01\dimexpr0pt-(1pt)*\c@pgf@counta\relax%
\ifdim\the\c@pgf@counta\pgf@x<\the\c@pgf@counta\pgf@xb%
{%
\pgf@xc=\pgf@x%
\pgf@yc=\pgf@ya%
\pgf@pos@transform{\pgf@xc}{\pgf@yc}
\pgfsyssoftpath@moveto{\the\pgf@xc}{\the\pgf@yc}%
\pgf@xc=\pgf@x%
\pgf@yc=\pgf@yb%
\pgf@pos@transform{\pgf@xc}{\pgf@yc}
\pgfsyssoftpath@lineto{\the\pgf@xc}{\the\pgf@yc}%
}%
\fi%
}
\makeatother
\newcommand{\arrow}[2]{\draw[->,blue,thick,shorten <=5mm, shorten >=5mm] #1 -- #2 ;}
\begin{document}
\begin{tikzpicture}
\draw[help lines] (-2,-2) grid (2,2);
\draw[red,fill] (0,0) circle (1mm) node[below right] {(0,0)};
\node[anchor=south,align=center] at (0,2){(-2,-2) to (2,2)};
\arrow{(-2,-2)}{(2,2)}
\end{tikzpicture}
\begin{tikzpicture}
\draw[help lines] (-2,2) grid[ystep=0.33] (2,-2);
\arrow{(-2,2)}{(2,-2)}
\draw[red,fill] (0,0) circle (1mm) node[above right] {(0,0)};
\node[anchor=south,align=center] at (0,2){(-2,2) to (2,-2)};
\end{tikzpicture}
\begin{tikzpicture}
\draw[help lines] (2,-2) grid[xstep=0.66] (-2,2);
\arrow{(2,-2)}{(-2,2)}
\draw[red,fill] (0,0) circle (1mm) node[above right] {(0,0)};
\node[anchor=south,align=center] at (0,2){(2,-2) to (-2,2)};
\end{tikzpicture}
\begin{tikzpicture}
\draw[help lines] (2,2) grid[xstep=0.5,ystep=0.5] (-2,-2);
\arrow{(2,2)}{(-2,-2)}
\draw[red,fill] (0,0) circle (1mm) node[below right] {(0,0)};
\node[anchor=south,align=center] at (0,2){(2,2) to (-2,-2)};
\end{tikzpicture}
\end{document}