如何在 tikzpicture 中绘制一个 45 度角的箭头?

如何在 tikzpicture 中绘制一个 45 度角的箭头?

|-在 tikz 中,我可以使用或制作一个连接两个节点的箭头,这些节点有直线和一个 90 度角-|。但是有没有一种简单的方法可以制作一条直线的水平/垂直线段,然后制作一条 45 度(或径向向内或其他角度)的线段,就像图中的红线一样?

\documentclass[12pt]{extarticle}
\usepackage[paperwidth=7.4cm, paperheight=5.55cm, top=0cm, bottom=0cm, left=0cm, right=0cm]{geometry}
\usepackage{tikz}
\usetikzlibrary{shapes,arrows,positioning,calc}
\tikzstyle{circle1} = [draw, circle, minimum size = 1.5mm]

\begin{document}
\begin{tikzpicture}[auto, node distance = 1.2cm]
        \node (first) [circle1] {first};
        \node (second) [circle1, right of = first, node distance = 3.5 cm, below=2.5 cm] {second};

        \draw[->, thick] (first.0) -- node [above] {$--$} (second.135);
        \draw[->, thick] (first.0) |- node [below] {$|-$} (second.160);
\end{tikzpicture}
 \end{document}

示例代码的输出,其中所需添加的内容以红色显示

答案1

\documentclass[12pt]{extarticle}
\usepackage[paperwidth=8.6cm, paperheight=5.55cm, top=0cm, bottom=0cm, left=0cm, right=0cm]{geometry}
\usepackage{tikz}
\usetikzlibrary{shapes,arrows,positioning,calc}
\tikzset{circle1/.style={draw, circle, minimum size = 1.5mm}}
\tikzset{pics/perp/.style={code={\draw[-] (0,0) -- (1,0) (0,-0.5) -- (0,0.5);}},
pics/line/.style={code={\draw[-] (0,0) -- (1,0);}}}

\begin{document}
\begin{tikzpicture}[auto]
        \node (first) [circle1] {first};
        \node (second) [circle1, below right=1.5cm and 5.5cm of first] {second};
        \draw[->, thick] (first.0) -- pic[yshift=3mm,scale=0.3]{line} (second.135);
        \draw[->, thick] (first.0) |- pic[yshift=-3mm,scale=0.3]{perp} (second.160);
        \draw[->, thick,red] let \p1=(first.east), \p2=(second.120) in
        (first.east) -- ++({\x2-\x1-\y1+\y2},0) -- (second.120);
\end{tikzpicture}
\end{document}

在此处输入图片描述

经过 EL_DON 的建议进行概括:这是一个具有边缘样式的版本。

\documentclass[12pt]{extarticle}
\usepackage[paperwidth=8.6cm, paperheight=5.55cm, top=0cm, bottom=0cm, left=0cm, right=0cm]{geometry}
\usepackage{tikz}
\usetikzlibrary{shapes,arrows,positioning,calc}
\tikzset{circle1/.style={draw, circle, minimum size = 1.5mm}}
\tikzset{pics/perp/.style={code={\draw[-] (0,0) -- (1,0) (0,-0.5) -- (0,0.5);}},
pics/line/.style={code={\draw[-] (0,0) -- (1,0);}}}


\begin{document}

\begin{tikzpicture}[auto,connect with angle/.style=
{to path={let \p1=(\tikztostart), 
\p2=(\tikztotarget) in -- ++({\x2-\x1-(\y1-\y2)*tan(#1-90)},0) -- (\tikztotarget)}}]
        \node (first) [circle1] {first};
        \node (second) [circle1, below right=1.5cm and 5.5cm of first] {second};
        \draw[->, thick] (first.0) -- pic[yshift=3mm,scale=0.3]{line} (second.135);
        \draw[->, thick] (first.0) |- pic[yshift=-3mm,scale=0.3]{perp} (second.160);
        \draw[red,-latex] (first.east) edge[connect with angle=135] (second.120);
        \draw[blue,-latex] (first.east) edge[connect with angle=105] (second.120);
\end{tikzpicture}
\end{document}

在此处输入图片描述

这里,按照 EL_DON 的惯例,角度是两个伸展之间的角度。其他惯例可以通过tan(#1-90)适当调整来解释。

答案2

使用intersections库:

\documentclass[tikz, margin=4mm]{standalone}
\usetikzlibrary{intersections,
                positioning,
                shapes}
\tikzstyle{circle1} = [draw, circle, minimum size = 1.5mm]

\begin{document}
    \begin{tikzpicture}[auto,
node distance = 11mm and 22mm]
\node (first) [circle1] {first};
\node (second) [circle1, below right=of first] {second};
%
\draw[->, thick] (first) -- node [above] {$--$} (second);
\draw[->, thick] (first) |- node [below] {$|-$} (second);
%
\path[name path=A] (first) -- (first -| second);
\path[name path=B] (second) -- ++ (135:33mm);
\draw [name intersections={of=A and B, by=a},very thick, blue]
    (first) -- (a) -- (second);
\end{tikzpicture}
 \end{document}

在此处输入图片描述

相关内容