简化跨度的 Tikz 图片

简化跨度的 Tikz 图片

我制作了一张图片来展示跨度的工作原理,并且我知道有一种更有效的方法可以做到这一点:

\begin{tikzpicture} [thick,scale=0.5, every node/.style={scale=0.5}]
\draw[step=1cm,gray,very thin] (-1.25,-1.25) grid (1.25,1.25);
\draw[latex-latex] (-1.25,0) -- (1.25,0) node[right] {$x$-axis} ;
\draw[latex-latex] (0,-1.25) -- (0,1.25) node[above] {$y$-axis} ;
\draw[red][ultra thick][->] (0,0) -- (0.4,0.4);
\draw[red][ultra thick][->] (0,0) -- (0.8,0.8);
\draw[red][ultra thick][->] (0,0) -- (1.2,1.2);
\draw[red][ultra thick][->] (0,0) -- (-0.4,-0.4);
\draw[red][ultra thick][->] (0,0) -- (-0.8,-0.8);
\draw[red][ultra thick][->] (0,0) -- (-1.2,-1.2);
\draw[red][ultra thick][->] (0,0) -- (-0.4,0.4);
\draw[red][ultra thick][->] (0,0) -- (-0.8,0.8);
\draw[red][ultra thick][->] (0,0) -- (-1.2,1.2);
\draw[red][ultra thick][->] (0,0) -- (0.4,-0.4);
\draw[red][ultra thick][->] (0,0) -- (0.8,-0.8);
\draw[red][ultra thick][->] (0,0) -- (1.2,-1.2);
\draw[red][ultra thick][->] (0,0) -- (0,0.4);
\draw[red][ultra thick][->] (0,0) -- (0,0.8);
\draw[red][ultra thick][->] (0,0) -- (0,1.2);
\draw[red][ultra thick][->] (0,0) -- (0,-0.4);
\draw[red][ultra thick][->] (0,0) -- (0,-0.8);
\draw[red][ultra thick][->] (0,0) -- (0,-1.2);
\draw[red][ultra thick][->] (0,0) -- (-0.4,0);
\draw[red][ultra thick][->] (0,0) -- (-0.8,0);
\draw[red][ultra thick][->] (0,0) -- (-1.2,0);
\draw[red][ultra thick][->] (0,0) -- (0.4,0);
\draw[red][ultra thick][->] (0,0) -- (0.8,0);
\draw[red][ultra thick][->] (0,0) -- (1.2,0);
\end{tikzpicture}

现在它可以工作了,因为向量很简单,但我想创建一个函数,在给定区域内绘制给定向量或向量集的跨度。

答案1

我扩大了网格以避免箭头重叠。

截屏

\documentclass[border=10mm]{standalone} 
\usepackage{tikz}

\begin{document}

\begin{tikzpicture}[thick,scale=0.5, every node/.style={scale=0.5},]
\draw[step=1cm,gray,very thin] (-2.5,-2.5) grid (2.5,2.5);
\draw[latex-latex] (-2.5,0) -- (2.5,0) node[right] {$x$-axis} ;
\draw[latex-latex] (0,-2.5) -- (0,2.5) node[above] {$y$-axis} ;

\begin{scope}[red,ultra thick]
\foreach \i in {.4,.8,1.2}
    {\foreach \j [evaluate =\j as \jj using \j*\i] in {-1,1}
{\draw [->](0,0)--(\i,\jj);
\draw [->](0,0)--(0,\jj);
\draw[->](0,0)--(-\i,\jj);
\draw[->](0,0)--(\jj,0);
}}
\end{scope}

\end{tikzpicture}
\end{document}

答案2

你可以用一个pic。我添加了一个 pic 定义,用它绘制两个向量的跨度就像说

 \pic[red,ultra thick]{span={vector 1={(1,0)},vector 2={(0,1)}}};

可以按如下方式使用:

\documentclass[tikz,border=3mm]{standalone}
\usetikzlibrary{calc}
\begin{document}
\begin{tikzpicture}[thick,scale=0.5,nodes={scale=0.5},
    pics/span/.style={code={\tikzset{span/.cd,#1}
     \foreach \XX in {-1,0,1}
     {\foreach \YY in {-1,0,1}
     {\pgfmathtruncatemacro{\itest}{ifthenelse(\XX==0 && \YY==0,0,1)}
      \ifnum\itest=1
       \edef\temp{\noexpand\draw[pic actions,->] (0,0) -- 
       ($\XX*\pgfkeysvalueof{/tikz/span/vector 1} 
        +\YY*\pgfkeysvalueof{/tikz/span/vector 2}$);}
       \temp    
      \fi}
     }
    }},span/vector 1/.initial={(1,0)},span/vector 2/.initial={(0,1)},
    pics/axis/.style={code={
     \draw[step=1cm,gray,very thin] (-#1,-#1) grid (#1,#1);
     \draw[latex-latex] (-#1,0) -- (#1,0) node[right] {$x$-axis};
     \draw[latex-latex] (0,-#1) -- (0,#1) node[above] {$y$-axis};
    }}]
 \pic{axis=3.5};
 \foreach \X in {0.8,1.6,2.4}   
 {\pic[red,ultra thick,>=stealth]{span={vector 1={(\X,0)},vector 2={(0,\X)}}};}
\end{tikzpicture}
\end{document}

在此处输入图片描述

如您所见,此 MWE 还具有轴的图片。它还避免添加两个系数均为 0 的线性组合。

相关内容