Tikz 形状作为节点

Tikz 形状作为节点

我创建了一些图片,计划在坐标平面的不同位置使用它。但我以 TeX 宏的形式制作了它\cone{...}

我怎样才能以 的形式创建这样的图片tikz \node[cone]

\documentclass[tikz, border=1cm]{standalone}
\usetikzlibrary{calc}
\begin{document}
    \begin{tikzpicture}
            \def\cone(#1){
            \draw (#1) +(135:0.5) -- +(-45:0.5);
            \draw (#1) +(45:0.5) -- +(-135:0.5);
            \fill (#1) circle (0.03);
            \draw let \p1=(#1) in (\x1,{\y1+.35cm}) ellipse (0.352 and 0.05);
            \draw let \p1=(#1) in (\x1,{\y1-0.35cm}) ellipse (0.352 and 0.05);
            }

            \coordinate (A) at (0,0);
            \coordinate (B) at (1,1);

            \cone(A)
            \cone(B)

    \end{tikzpicture}
\end{document}

答案1

在此处输入图片描述

上面的图像是用\pic

\documentclass[tikz, border=1cm]{standalone}

\begin{document}
    \begin{tikzpicture}[
cone/.pic = {\draw (-0.5,-0.5) -- ++ (1, 1);
             \draw (-0.5, 0.5) -- ++ (1,-1);
             \fill (0,0) circle (0.03);
             \draw (0, 0.5) ellipse (0.5 and 0.05);
             \draw (0,-0.5) ellipse (0.5 and 0.05);
            }
                    ]
\pic at (0,0) {cone};
\pic at (2,1) {cone};
    \end{tikzpicture}
\end{document}

由于“锥体”的使用环境未知,因此我没有定义任何锥体的锚点。

附录:

如果您想在锥体之间画线,那么您需要定义锥体的锚点:

\documentclass[tikz, border=1cm]{standalone}

\begin{document}
    \begin{tikzpicture}[
cone/.pic = {\draw (-0.5, 0.5) coordinate (-nw) -- ++ (1,-1) coordinate (-se);
             \draw (-0.5,-0.5) coordinate (-sw) -- ++ (1, 1) coordinate (-ne);
             \fill (0,0)  coordinate (-center) circle (0.03);
             \draw (0, 0.5) ellipse (0.5 and 0.05);
             \draw (0,-0.5) ellipse (0.5 and 0.05);
             \coordinate (-top)     at (0, 0.55);
             \coordinate (-north)   at (0, 0.5);
             \coordinate (-south)   at (0,-0.5);
             \coordinate (-bottom)  at (0,-0.55);
            }
                    ]
\pic (A) at (0,0) {cone};
\pic (B) at (2,1) {cone};
\draw[red] (A-center) -- (B-bottom);
\draw[blue] (A-north) -- + (0,2) -| (B-north);
\draw[green] (A-ne) |- (B-nw);
\pic[rotate=90] (B) at (4,0) {cone};
    \end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容