这是我当前的 MWE:
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc}
\tikzset{>=stealth}
\colorlet{veccol}{green!45!black}
\tikzstyle{vector}=[->,very thick,veccol]
\begin{document}
\begin{figure}
\centering
\begin{tikzpicture}
\draw[<->] (0,0)--(5.5,0) node[right]{$x$};
\draw[<->] (0,0)--(0,5.5) node[above]{$y$};
\coordinate (O) at (0,0);
\coordinate (A) at (125:5);
\coordinate (B) at (0:4);
\coordinate (A+B) at ($(A)+(B)$);
\draw[vector,blue] (0,0)--(A) node[midway,left] {$\vec{x}$};
\draw[vector,red] (0,0)--(B) node[midway,above] {$\vec{y}$};
\draw[vector,thin,dashed] (A)--(A+B);
\draw[vector,thin,dashed] (B)--(A+B);
\draw[vector,purple] (O)--(A+B) node[above,rotate=-60] {$\vec{x}+\vec{y}$};
\draw[rotate=180] (O)--(A+B);
\end{tikzpicture}
\end{figure}
\end{document}
生成此 pdf:
问题就在这里。我总是把这段代码写出来。我有 10 多个地方都有它。有没有办法我可以创建一个类似函数的东西,使用极坐标给它输入两个(或更多)向量,然后像这样绘制它?
另外,我如何使用头尾规则而不是平行四边形规则绘制相同的向量?是否也可以创建某种可重复使用的函数(如上面的问题),但将其用于头尾规则而不是平行四边形规则?
答案1
对于平行四边形部分:
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc}
\newcommand{\parallelogramRule}[2]{%
\begin{tikzpicture}[inner sep=1pt,>=stealth]
\draw[<->] (0,0)--(5.5,0) node[right]{$x$};
\draw[<->] (0,0)--(0,5.5) node[above]{$y$};
\tikzstyle{vector label} = [midway,fill=white,sloped]
\tikzstyle{vector}=[->,very thick]
\tikzstyle{construction} = [->,thin,dashed,draw=green!45!black]
\coordinate (O) at (0,0);
\coordinate (A) at (#1);
\coordinate (B) at (#2);
\coordinate (A+B) at ($(A)+(B)$);
\draw[vector,blue] (O)--(A) node[vector label] {$\vec{x}$};
\draw[vector,red] (O)--(B) node[vector label] {$\vec{y}$};
\draw[construction] (A)--(A+B);
\draw[construction] (B)--(A+B);
\draw[vector,purple] (O)--(A+B) node[vector label] {$\vec{x}+\vec{y}$};
\end{tikzpicture}
}
\begin{document}
\begin{figure}
\centering
\parallelogramRule{30:5}{0:4}%
\parallelogramRule{130:2}{90:2}%
\parallelogramRule{130:4}{-10:2}
\end{figure}
\end{document}
实现头尾规则很简单,只需用减法代替加法,并改变结果向量的标签。
编辑
用于添加多个向量
\documentclass{article}
\usepackage{listofitems}
\usepackage{tikz}
\usetikzlibrary{calc}
\newcommand{\multipleVecAdd}[1]{%
\begin{tikzpicture}[inner sep=0pt]
\tikzstyle{vector}=[->,very thick,blue]
\tikzstyle{vector label} = [midway,fill=white,sloped]
\tikzstyle{resultant}=[->,very thick,red]
\coordinate (O) at (0,0);
\draw [->] (O) -- (4,0) node [right] {$x$};
\draw [->] (O) -- (0,4) node [right] {$y$};
\setsepchar{;}
\readlist*{\veclist}{#1}
\coordinate (resultant) at (O);
\foreach \i in {1,2,...,\veclistlen}{%
\coordinate (vec\i) at (\veclist[\i]);
\draw [vector] (O) -- (vec\i) node [vector label] {$\vec{v}_\i$};
\coordinate (resultant) at ($(resultant)+(vec\i)$);
}
\draw [resultant] (O) -- (resultant) node [vector label] {resultant};
\end{tikzpicture}
}
\begin{document}
\begin{figure}
\centering
\multipleVecAdd{1,1;-2,3;4,1;4,-2;-1,-4}
\end{figure}
\end{document}
编辑2
按照 Qrrbrbirlbel 和 SebGlav 的建议,直接使用tikzset
和\pgffor
。
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc}
\newcommand{\multipleVecAdd}[1]{%
\begin{tikzpicture}[inner sep=0pt]
\tikzset{%
vector/.style={->,very thick,blue},
vector label/.style={midway,fill=white,sloped},
resultant/.style={->,very thick,red}
}
\coordinate (O) at (0,0);
\draw [->] (O) -- (4,0) node [right] {$x$};
\draw [->] (O) -- (0,4) node [above] {$y$};
\coordinate (resultant) at (O);
\foreach \vecPos [count=\vecId] in {#1}{%
\draw [vector] (O) -- \vecPos node [vector label] {$\vec{v}_\vecId$};
\draw (resultant) +\vecPos coordinate (resultant);
}
\draw [resultant] (O) -- (resultant) node [vector label] {resultant};
\end{tikzpicture}
}
\begin{document}
\begin{figure}
\centering
\multipleVecAdd{(1,1),(-2,3),(4,1),(4,-2),(-1,-4)}
\end{figure}
\end{document}