我可以\draw [->] (0,0) -- (5,5);
用 tikz 画箭头。但是也可以创建锯齿状箭头,比如
我需要像普通箭头一样使用它;从一个节点到另一个节点。
答案1
这里有一个手动的解决方案(它是手动的因为您必须在水平线和垂直线之间以及 45 度角或 -45 度角之间进行选择)。
\documentclass[tikz,margin=2mm]{standalone}
\usetikzlibrary{calc}
\tikzset{
hor jagged -45/.style={to path={
let \p{s}=(\tikztostart), \p{e}=(\tikztotarget), \p{m}=($(\p{s})!.5!(\p{e})$),
\p{s1}=($(\p{m})!1.41421!45:(\p{s}-|\p{m})$), \p{e1}=($(\p{m})!1.41421!45:(\p{e}-|\p{m})$)
in (\p{s}) -- (\p{s1}) -- (\p{e1}) -- (\p{e})
},
},
hor jagged 45/.style={to path={
let \p{s}=(\tikztostart), \p{e}=(\tikztotarget), \p{m}=($(\p{s})!.5!(\p{e})$),
\p{s1}=($(\p{m})!1.41421!-45:(\p{s}-|\p{m})$), \p{e1}=($(\p{m})!1.41421!-45:(\p{e}-|\p{m})$)
in (\p{s}) -- (\p{s1}) -- (\p{e1}) -- (\p{e})
},
},
ver jagged -45/.style={to path={
let \p{s}=(\tikztostart), \p{e}=(\tikztotarget), \p{m}=($(\p{s})!.5!(\p{e})$),
\p{s1}=($(\p{m})!1.41421!-45:(\p{s}|-\p{m})$), \p{e1}=($(\p{m})!1.41421!-45:(\p{e}|-\p{m})$)
in (\p{s}) -- (\p{s1}) -- (\p{e1}) -- (\p{e})
},
},
ver jagged 45/.style={to path={
let \p{s}=(\tikztostart), \p{e}=(\tikztotarget), \p{m}=($(\p{s})!.5!(\p{e})$),
\p{s1}=($(\p{m})!1.41421!45:(\p{s}|-\p{m})$), \p{e1}=($(\p{m})!1.41421!45:(\p{e}|-\p{m})$)
in (\p{s}) -- (\p{s1}) -- (\p{e1}) -- (\p{e})
},
},
}
\begin{document}
\begin{tikzpicture}[line join=round,line cap=round]
\coordinate (a) at (0,0);
\coordinate (b) at (4,1);
\draw[line width=5pt,-latex] (a) to[hor jagged 45] (b);
\draw[line width=3pt,red,-latex] (a) to[hor jagged -45] (b);
\draw[line width=2pt,blue,-latex] (a) to[ver jagged 45] (b);
\draw[line width=.5pt,green,-latex] (a) to[ver jagged -45] (b);
\end{tikzpicture}
\end{document}
答案2
您可以使用zigzag
设置的样式to path
,如下所示:
\documentclass[tikz,border=5pt]{standalone}
\usetikzlibrary{calc}
\tikzset{
zigzag/.style={to path={ -- ($(\tikztostart)!.55!-7:(\tikztotarget)$) -- ($(\tikztostart)!.45!7:(\tikztotarget)$) -- (\tikztotarget) \tikztonodes}}
}
\begin{document}
\begin{tikzpicture}
\draw[-stealth,line width=1mm] (-2,2.5) to[zigzag] +(4,0);
\foreach[evaluate={\c=\i/3.3}] \i in {0,30,...,330}
\draw[-latex, red!\c!blue, thick] (0,0) to[zigzag] (\i:2);
\end{tikzpicture}
\end{document}
答案3
假设您总是想要水平或垂直的锯齿形。有办法避免创建一堆新的长度,但目前这应该足够令人困惑了。
\documentclass[border=.1in]{standalone}
\usepackage{tikz}
\usetikzlibrary{calc,arrows}
\newlength{\tempx}
\newlength{\tempy}
\newlength{\absx}
\newlength{\absy}
\newcommand{\zotz}[3][->]% #1=parameters (optional), #2=from x,y, #3= to x,y
{\coordinate (zotzfrom) at (#2);
\coordinate (zotzto) at (#3);
\pgfextractx{\tempx}{\pgfpointdiff{\pgfpointanchor{zotzfrom}{center}}%
{\pgfpointanchor{zotzto}{center}}};
\pgfextracty{\tempy}{\pgfpointdiff{\pgfpointanchor{zotzfrom}{center}}%
{\pgfpointanchor{zotzto}{center}}};
\ifdim\tempx>0pt \absx=\tempx \else\absx=-\tempx \fi
\ifdim\tempy>0pt \absy=\tempy \else\absy=-\tempy \fi
\ifdim\absy>\absx
\draw[#1] (zotzfrom) -- +(0pt,0.6\tempy) -- +(\tempx,.4\tempy) -- (zotzto);
\else
\draw[#1] (zotzfrom) -- +(0.6\tempx,0pt) -- +(.4\tempx,\tempy) -- (zotzto);
\fi}
\begin{document}
\begin{tikzpicture}
\zotz{0,0}{4,1}
\zotz[-latex,red]{0,1}{1,5}
\end{tikzpicture}
\end{document}