自定义节点边框

自定义节点边框

我找到了一个不错的函数,可以在两点之间绘制一个漂亮的椭圆。但是,我希望能够使用这个椭圆作为新节点的边框,这样如果我朝这个椭圆画一个箭头,它就会停在边框处。我尝试过装饰,但它没有解决我的问题……

平均能量损失

\documentclass{report}

\usepackage{tikz}
\usetikzlibrary{arrows,backgrounds,positioning}
\usetikzlibrary{calc,math,fit,shapes}
\usetikzlibrary{decorations.pathmorphing}
\pgfdeclarelayer{bg}    % declare background layer
\pgfdeclarelayer{bgmain}    % declare background layer
\pgfsetlayers{bg,bgmain,main}  % set the order of the layers (main is the standard layer)

\usepackage{xcolor}

\newcommand\ellipsebyfoci[4]{% options, focus pt1, focus pt2, cste
  \path[#1] let \p1=(#2), \p2=(#3), \p3=($(\p1)!.5!(\p2)$)
  in \pgfextra{
    \pgfmathsetmacro{\angle}{atan2(\y2-\y1,\x2-\x1)}
    \pgfmathsetmacro{\focal}{veclen(\x2-\x1,\y2-\y1)/2/1cm}
    \pgfmathsetmacro{\lentotcm}{\focal*2*#4}
    \pgfmathsetmacro{\axeone}{(\lentotcm - 2 * \focal)/2+\focal}
    \pgfmathsetmacro{\axetwo}{sqrt((\lentotcm/2)*(\lentotcm/2)-\focal*\focal}
  }
  (\p3) ellipse[x radius=\axeone cm,y radius=\axetwo cm, rotate=\angle];
}

\begin{document}

\begin{tikzpicture}
  \node(a) at (-1,1) {};
  \node(b) at (1,-1) {};
  \begin{pgfonlayer}{bg}
    \ellipsebyfoci{draw,fill=green!50}{a}{b}{1.5}
  \end{pgfonlayer}
  %% I want to point to the ellipse, not the points
  \node[label=east:Should point to the ellipse](c) at (3,-1) {};
  \draw[-latex] (c) -- (0,0);
\end{tikzpicture}


\end{document}

我得到的是:在此处输入图片描述

我想要的是:在此处输入图片描述

谢谢 !

-- EDIT 01 -- 我找到了一种解决方案,基本上使用了fit包和椭圆包。它生成一个节点,但它并不完全遵循上述函数的椭圆形状:

\begin{tikzpicture}
  \node(a) at (-1,1) {a};
  \node(b) at (1,-1) {b};
  \node[rotate fit=45,fit=(a)(b),ellipse,fill=red!,opacity=.5](ell){};
  \node(x) at (3,2) {};
  \draw[->] (x) -- (ell);
  \node(y) at (-4,2) {};
  \draw[->] (y) -- (ell);
\end{tikzpicture}

在此处输入图片描述

以下是其一般功能:

\newcommand\fitEllipse[3]{% options, focus pt1, focus pt2
  \pgfmathanglebetweenpoints{\pgfpointanchor{#2}{center}}
  {\pgfpointanchor{#3}{center}}
 \edef\angle{\pgfmathresult} % save result in \angle 
  \node[rotate fit=\angle, fit=(#2)(#3),ellipse,#1]{};
}

答案1

基于交叉点的解决方案。

\documentclass{report}

\usepackage{tikz}
\usetikzlibrary{arrows,backgrounds,positioning}
\usetikzlibrary{calc,math,fit,shapes,intersections}%<- added intersections
\usetikzlibrary{decorations.pathmorphing}
\pgfdeclarelayer{bg}    % declare background layer
\pgfdeclarelayer{bgmain}    % declare background layer
\pgfsetlayers{bg,bgmain,main}  % set the order of the layers (main is the standard layer)

\usepackage{xcolor}

\newcommand\ellipsebyfoci[4]{% options, focus pt1, focus pt2, cste
  \path[#1] let \p1=(#2), \p2=(#3), \p3=($(\p1)!.5!(\p2)$)
  in \pgfextra{
    \pgfmathsetmacro{\angle}{atan2(\y2-\y1,\x2-\x1)}
    \pgfmathsetmacro{\focal}{veclen(\x2-\x1,\y2-\y1)/2/1cm}
    \pgfmathsetmacro{\lentotcm}{\focal*2*#4}
    \pgfmathsetmacro{\axeone}{(\lentotcm - 2 * \focal)/2+\focal}
    \pgfmathsetmacro{\axetwo}{sqrt((\lentotcm/2)*(\lentotcm/2)-\focal*\focal}
  }
  (\p3) ellipse[x radius=\axeone cm,y radius=\axetwo cm, rotate=\angle];
}

\begin{document}

\begin{tikzpicture}
  \node(a) at (-1,1) {};
  \node(b) at (1,-1) {};
  \begin{pgfonlayer}{bg}
    \ellipsebyfoci{draw,fill=green!50,name path=ellipse}{a}{b}{1.5}
  \end{pgfonlayer}
  %% I want to point to the ellipse, not the points
  \node[label=east:points to the ellipse](c) at (3,-1) {};
  \path[name path=connection] (c) -- ($(a)!0.5!(b)$);
  \draw [-latex, name intersections={of=ellipse and connection}] (c)--
    (intersection-1);;
\end{tikzpicture}

在此处输入图片描述

编辑:这是你的椭圆节点。

\documentclass{report}

\usepackage{tikz}
\usetikzlibrary{arrows,backgrounds,positioning}
\usetikzlibrary{calc,math,fit,shapes,intersections}%<- added intersections
\usetikzlibrary{decorations.pathmorphing}
\pgfdeclarelayer{bg}    % declare background layer
\pgfdeclarelayer{bgmain}    % declare background layer
\pgfsetlayers{bg,bgmain,main}  % set the order of the layers (main is the standard layer)

\usepackage{xcolor}
\makeatletter % from https://tex.stackexchange.com/a/412901/121799
\newcommand{\Distance}[3]{% % from https://tex.stackexchange.com/q/56353/121799
\tikz@scan@one@point\pgfutil@firstofone($#1-#2$)\relax  
\pgfmathsetmacro{#3}{veclen(\the\pgf@x,\the\pgf@y)/28.45274}
}\makeatother 

% from https://tex.stackexchange.com/a/75084/121799
\newcommand\ellipsebyfoci[4]{% options, focus pt1, focus pt2, cste
  \path[#1] let \p1=(#2), \p2=(#3), \p3=($(\p1)!.5!(\p2)$)
  in \pgfextra{
    \pgfmathsetmacro{\angle}{atan2(\y2-\y1,\x2-\x1)}
    \pgfmathsetmacro{\focal}{veclen(\x2-\x1,\y2-\y1)/2/1cm}
    \pgfmathsetmacro{\lentotcm}{\focal*2*#4}
    \pgfmathsetmacro{\axeone}{(\lentotcm - 2 * \focal)/2+\focal}
    \pgfmathsetmacro{\axetwo}{sqrt((\lentotcm/2)*(\lentotcm/2)-\focal*\focal}
  }
  (\p3) ellipse[x radius=\axeone cm,y radius=\axetwo cm, rotate=\angle]{};
}

\newcommand\ellipticnode[5][]{% options, focus pt1, focus pt2, cste
 \pgfmathanglebetweenpoints{\pgfpointanchor{#2}{center}}
                            {\pgfpointanchor{#3}{center}}
 \edef\angle{\pgfmathresult} % save result in \angle 
 \Distance{(#2)}{(#3)}{\focal}
 \pgfmathsetmacro{\lentotcm}{\focal*#4}
 \pgfmathsetmacro{\axeone}{2*((\lentotcm - \focal)/2+\focal/2)}
 \pgfmathsetmacro{\axetwo}{2*(sqrt((\lentotcm/2)*(\lentotcm/2)-\focal*\focal/4)}
 \node at ($(#2)!.5!(#3)$) [shape=ellipse,minimum width=\axeone cm,minimum
 height=\axetwo cm, rotate=\angle,#1] (#5)
  {};
}

\begin{document}

\begin{tikzpicture}
  \node(a) at (-1,1) {};
  \node(b) at (1,-1) {};
  \begin{pgfonlayer}{bg}
    \ellipticnode[draw,fill=green!50]{a}{b}{1.5}{d}
  \end{pgfonlayer}
  \node[label=east:points to the ellipse](c) at (3,-1) {};
  \draw[->] (c)--(d);   
\end{tikzpicture}

在此处输入图片描述

相关内容