使用 pgf 绘制带箭头的圆弧以创建新形状

使用 pgf 绘制带箭头的圆弧以创建新形状

我借用了一些代码TikZ 电路:气体放电管的符号?定义我自己的符号。内弧的一端应该有一个箭头,但我没能做到。我的代码有什么问题?这是我的尝试:

\documentclass{article}

\usepackage{tikz}

\begin{document}

\makeatletter
\pgfdeclareshape{circulator}
{
    \inheritsavedanchors[from=circle]
    \inheritanchor[from=circle]{center}
    \inheritanchor[from=circle]{north}
    \inheritanchor[from=circle]{south}
    \inheritanchor[from=circle]{east}
    \inheritanchor[from=circle]{west}
    \inheritanchor[from=circle]{north east}
    \inheritanchor[from=circle]{north west}
    \inheritanchor[from=circle]{south east}
    \inheritanchor[from=circle]{south west}
    \inheritanchor[from=circle]{input}
    \inheritanchor[from=circle]{output}
    \inheritanchorborder[from=circle]

    \backgroundpath{
        \pgf@process{\radius}
        \pgfutil@tempdima=\radius

        \pgfpathcircle{\centerpoint}{\pgfutil@tempdima}

        \pgfsetarrowsstart{latex}
        \pgfpathmoveto{\pgfpoint{.7\pgfutil@tempdima}{0pt}}
        \pgfpatharc{0}{120}{.7\pgfutil@tempdima}
        \pgfusepath{stroke}

    }
}
\makeatother

\begin{tikzpicture}
  \node [circulator] at (0,0) {};
\end{tikzpicture}

\end{document}

在此处输入图片描述

答案1

这里有两种解决方案。在这两种情况下,您都必须描画两条路径:圆(无箭头)然后是圆弧(有箭头)。

第一种解决方案

在第一个解决方案中,我添加了一条额外的线以避免箭头位置不佳。

在此处输入图片描述

\documentclass{standalone}

\usepackage{tikz}
\usetikzlibrary{arrows.meta}

\makeatletter
\pgfdeclareshape{circulator}
{
    \inheritsavedanchors[from=circle]
    \inheritanchor[from=circle]{center}
    \inheritanchor[from=circle]{north}
    \inheritanchor[from=circle]{south}
    \inheritanchor[from=circle]{east}
    \inheritanchor[from=circle]{west}
    \inheritanchor[from=circle]{north east}
    \inheritanchor[from=circle]{north west}
    \inheritanchor[from=circle]{south east}
    \inheritanchor[from=circle]{south west}
    \inheritanchor[from=circle]{input}
    \inheritanchor[from=circle]{output}
    \inheritanchorborder[from=circle]

    \backgroundpath{
        \pgf@process{\radius}
        \pgfutil@tempdima=\radius

        \pgfpathcircle{\centerpoint}{\pgfutil@tempdima}
        \pgfusepath{stroke}

        \pgfsetarrowsstart{Latex[length=2pt]}
        \pgfpathmoveto{\pgfpoint{.7\pgfutil@tempdima}{-2pt}}
        \pgfpathlineto{\pgfpoint{.7\pgfutil@tempdima}{0pt}}
        \pgfpatharc{0}{120}{.7\pgfutil@tempdima}
        \pgfusepath{stroke}

    }
}
\makeatother

\begin{document}
\begin{tikzpicture}
  \node [circulator] at (0,0) {};
\end{tikzpicture}
\end{document}

二维解决方案

这是另一种无需多余线条的解决方案(使用bendingTikZ 库来调整箭头的位置)。

在此处输入图片描述

\documentclass{standalone}

\usepackage{tikz}
\usetikzlibrary{arrows.meta,bending}

\makeatletter
\pgfdeclareshape{circulator}
{
    \inheritsavedanchors[from=circle]
    \inheritanchor[from=circle]{center}
    \inheritanchor[from=circle]{north}
    \inheritanchor[from=circle]{south}
    \inheritanchor[from=circle]{east}
    \inheritanchor[from=circle]{west}
    \inheritanchor[from=circle]{north east}
    \inheritanchor[from=circle]{north west}
    \inheritanchor[from=circle]{south east}
    \inheritanchor[from=circle]{south west}
    \inheritanchor[from=circle]{input}
    \inheritanchor[from=circle]{output}
    \inheritanchorborder[from=circle]

    \backgroundpath{
        \pgf@process{\radius}
        \pgfutil@tempdima=\radius

        \pgfpathcircle{\centerpoint}{\pgfutil@tempdima}
        \pgfusepath{stroke}

        \pgfsetarrowsstart{Latex[length=2pt]}
        \pgfpathmoveto{\pgfpoint{.7\pgfutil@tempdima}{0pt}}
        \pgfpatharc{0}{120}{.7\pgfutil@tempdima}
        \pgfusepath{stroke}

    }
}
\makeatother

\begin{document}
\begin{tikzpicture}
  \node [circulator] at (0,0) {};
\end{tikzpicture}
\end{document}

相关内容