Tikz 节点样式:半正方形,半椭圆形

Tikz 节点样式:半正方形,半椭圆形

我只是想知道是否有办法更改节点的边框样式,以便一半是矩形,另一半是椭圆形或云形等。下图显示了这一点:

节点(左半部分:矩形,右半部分:椭圆形

有人知道如何实现这一目标吗?

背景:我想使用边框样式来定义节点描述的元素,例如,矩形节点表示元素类型为 X,具有椭圆边框的节点表示元素类型为 Y,同时具有 X 和 Y 类型的元素应为半矩形、半椭圆。我知道如何绘制矩形、椭圆、云等,但如何将它们合并为一种样式?

答案1

我会给你我的意见,以防万一。

1. 使用 shapes.misc 库

    \begin{tikzpicture}
            
        \node[
            draw,
            rounded corners=3pt,
            minimum width=3cm,
            minimum height=2cm,
            rounded rectangle,
            rounded rectangle left arc=none,
            font=\sffamily\Large] {Hello};

    \end{tikzpicture}

花式节点 1

2. 使用图片并手动插入锚点

    \begin{tikzpicture}

        \tikzset%
            {
            pin/.style={Circle[]-,red},
            rectell/.pic={              
                \draw (0,0.5*3) coordinate(-north) -| (-0.5*5,0) coordinate(-west) |- (0,-0.5*3) coordinate(-south) arc(-90:0:0.5*5 cm and 0.5*3 cm) coordinate(-east) arc(0:90:0.5*5 cm and 0.5*3 cm) -- cycle;
                \node (-center) at (0,0) {#1}; 
                }
            }
        
        \draw (0,0) pic(A){rectell={\sffamily\Huge Hello}};
        
        \draw[pin] (A-center.center) --++ (3,3) node[above] {A-center};
        \draw[pin] (A-north) --++ (-1,1) node[above] {A-north};
        \draw[pin] (A-west) --++ (-1,1) node[above] {A-west};
        \draw[pin] (A-south) --++ (-1,-1) node[below] {A-south};
        \draw[pin] (A-east) --++ (1,1) node[above] {A-east};
        
    \end{tikzpicture}

花式节点 2

完整代码(带序言):

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{arrows.meta,shapes.misc}

\begin{document}

    \begin{tikzpicture}
            
        \node[
            draw,
            rounded corners=3pt,
            minimum width=3cm,
            minimum height=2cm,
            rounded rectangle,
            rounded rectangle left arc=none,
            font=\sffamily\Large] {Hello};

    \end{tikzpicture}
    
    
    \bigskip
    
    
    \begin{tikzpicture}

        \tikzset%
            {
            pin/.style={Circle[]-,red},
            rectell/.pic={              
                \draw (0,0.5*3) coordinate(-north) -| (-0.5*5,0) coordinate(-west) |- (0,-0.5*3) coordinate(-south) arc(-90:0:0.5*5 cm and 0.5*3 cm) coordinate(-east) arc(0:90:0.5*5 cm and 0.5*3 cm) -- cycle;
                \node (-center) at (0,0) {#1}; 
                }
            }
        
        \draw (0,0) pic(A){rectell={\sffamily\Huge Hello}};
        
        \draw[pin] (A-center.center) --++ (3,3) node[above] {A-center};
        \draw[pin] (A-north) --++ (-1,1) node[above] {A-north};
        \draw[pin] (A-west) --++ (-1,1) node[above] {A-west};
        \draw[pin] (A-south) --++ (-1,-1) node[below] {A-south};
        \draw[pin] (A-east) --++ (1,1) node[above] {A-east};
        
    \end{tikzpicture}
\end{document}

编辑:使用的参数图片作为节点

如果你想画一幅画节点并选择宽度和高度,您可以在调用时将这些参数作为参数传递给图片。

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

\begin{document}    
    \begin{tikzpicture}

        \tikzset%
            {
            pin/.style={Circle[]-,red},
            %
            pics/rectell/.style args={#1/#2/#3}{code = {                
                \draw (0,0.5*#3) coordinate(-north) -| (-0.5*#2,0) coordinate(-west) |- (0,-0.5*#3) coordinate(-south) arc(-90:0:0.5*#2 cm and 0.5*#3 cm) coordinate(-east) arc(0:90:0.5*#2 cm and 0.5*#3 cm) -- cycle;
                \coordinate (-center) at (0,0) node {#1}; 
                }}
            }
        
        \draw (0,0) pic(A){rectell={\sffamily\Huge Width 7 Height 3}/7/3};
        
        \draw[pin] (A-center) --++ (3,2) node[above] {A-center};
        \draw[pin] (A-north) --++ (-1,1) node[above] {A-north};
        \draw[pin] (A-west) --++ (-1,1) node[above] {A-west};
        \draw[pin] (A-south) --++ (-1,-1) node[below] {A-south};
        \draw[pin] (A-east) --++ (1,1) node[above] {A-east};
        
        \draw (0,-6) pic{rectell={\sffamily\Huge W 4 H 5}/4/5};
    \end{tikzpicture}
\end{document}

图片参数

相关内容