在 tikz 中创建椭圆矩形节点

在 tikz 中创建椭圆矩形节点

我想创建一个新的节点形状。节点的左半部分呈矩形,右半部分呈椭圆形。我应该能够在节点中插入文本,并且节点的尺寸应该自动调整。

我认为,一种做法是绘制三个节点,一个是带有幻像文本的矩形,一个是带有幻像文本的椭圆形,还有一个没有形状但显示文本的节点。前两个节点将被剪裁成两半。但是,使用这种方法,我无法保证两个半形的边界会接触。

答案1

\pgfdeclareshape以前从没用过……

我从中汲取元素 这个问题这个答案

\documentclass[border=10pt,tikz,multi]{standalone}
\usetikzlibrary{positioning}

\makeatletter%
\pgfdeclareshape{rectell}{%
    \inheritsavedanchors[from=rectangle]
    \inheritanchorborder[from=rectangle]
    \inheritbackgroundpath[from=rectangle]
    \inheritanchor[from=rectangle]{center}
    \inheritanchor[from=rectangle]{north}
    \inheritanchor[from=rectangle]{north east}
    \inheritanchor[from=rectangle]{north west}
    \inheritanchor[from=rectangle]{south}
    \inheritanchor[from=rectangle]{south east}
    \inheritanchor[from=rectangle]{south west}
    \inheritanchor[from=rectangle]{east}
    \inheritanchor[from=rectangle]{west}
    \inheritanchor[from=rectangle]{text}
    \backgroundpath{
        % store lower right in xa/ya and upper right in xb/yb
        \southwest \pgf@xa=\pgf@x \pgf@ya=\pgf@y
        \northeast \pgf@xb=\pgf@x \pgf@yb=\pgf@y
        % construct main path
        \pgfpathmoveto{\pgfpoint{(\pgf@xa+\pgf@xb)/2}{\pgf@ya}}
        \pgfpathlineto{\pgfpoint{\pgf@xa}{\pgf@ya}}
        \pgfpathlineto{\pgfpoint{\pgf@xa}{\pgf@yb}}
        \pgfpathlineto{\pgfpoint{(\pgf@xa+\pgf@xb)/2}{\pgf@yb}}
        \pgfpatharcto{(\pgf@xa+\pgf@xb)/8}{(\pgf@ya+\pgf@yb)/4}{0}{0}{0}{\pgfpoint{(\pgf@xa+\pgf@xb)/2}{\pgf@ya}}
    }
    \foregroundpath{
        \pgfusepath{stroke}
    }
}
\makeatother

\begin{document}
    \begin{tikzpicture}
%   \draw [help lines] (-2,-4) grid (10,2);
    \node (a) [rectell, draw] at (0, 0) {$\bullet\bullet\bullet$};
    \node (b) [right=5pt of a.east, rectell, anchor=north west, draw=magenta,very thick, fill=gray] {text};
    \node (c) [right=5pt of b.east, rectell, anchor=south west, draw=blue] {$\bullet$};
    \node (d) [right=5pt of c.south east, rectell, anchor=south east, draw, draw=red, scale=-.5] {*};
    \end{tikzpicture}
\end{document}

结果是

在此处输入图片描述

答案2

一个可能的解决方案:

\documentclass[tikz,border=3mm]{standalone}
\usetikzlibrary{calc}

\tikzset{
    pics/mypic/.style args={#1/#2/#3}{
      code = {
       \draw (0,0)coordinate(A) arc(90:-90:#1cm and #2cm)coordinate(B);
 \draw (A)--++(180:#1)--++(-90:#1)--(B);
 \node at  ($(A)!0.5!(B)$) {#3};
    }}}
    
\begin{document}  
\begin{tikzpicture}
\draw pic {mypic=2/1/dada}; 
\end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容