TikZ-从圆的边缘开始画线的最佳实践?

TikZ-从圆的边缘开始画线的最佳实践?

我想知道如何实现节点(在本例中为圆)和以该节点为起点的线之间的最佳无缝交叉。我希望该线仅接触圆的边缘。到目前为止,我发现了两种方法来实现一些简单的结果:

  • shorten <=:经过一些 T & E 运行后,它的工作方式令人满意。

  • \begin{pgfonlayer}{background}:将封闭的内容放在背景中。效果不太好,因为它将相应的内容(在本例中为 2 行)放在其他已经存在的内容(在本例中为“网格”)下方。

在此处输入图片描述

梅威瑟:

\documentclass[
11pt
]{scrartcl}

\usepackage{
tikz,
relsize,
amsmath
}

\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{lmodern}


%% from here on forth TikZ-stuff

\usetikzlibrary{
calc,trees,shadows,positioning,arrows,chains,shapes.geometric,
decorations.pathreplacing,decorations.pathmorphing,shapes,
matrix,shapes.symbols,patterns,intersections,fit
}

\pgfdeclarelayer{background layer}
\pgfdeclarelayer{foreground layer}
\pgfsetlayers{background layer,main,foreground layer}

\tikzset{
>=latex
}

\begin{document}

\begin{center}
\begin{tikzpicture}[font=\sffamily\small]
%
\draw[style=help lines,step=0.5cm] (0,0) grid (8,6);
%
\draw[->,thick] (-0.1,0) -- (8.5,0) node[anchor=west]{x}; %X-Achse
\draw[->,thick] (0,-0.1) -- (0,6.5) node[anchor=south]{y}; %Y-Achse
%
\draw
(0,0) coordinate (orig)
(3,3) coordinate (B)
(4.5,4.5) coordinate (C)
;
%
\foreach \pt/\labpos in {B/below right,C/below right}{
  \filldraw (\pt) circle (3pt) node[\labpos=3pt,fill=white]{\pt};
};
%
\path[name path=Segment] (B) -- (C);
%highlight segment
\draw
($(B) + (-0.75,0.75)$) coordinate (BPoint)
($(B) + (-0.25,0.25)$) coordinate (BPointExtra)
($(C) + (-0.75,0.75)$) coordinate (CPoint)
;
%\begin{pgfonlayer}{background layer}
\draw[black!60,thick,shorten <=3pt] (B) -- (BPoint);
\draw[black!60,thick,shorten <=2pt] (C) -- (CPoint);
%\end{pgfonlayer}
\draw[black!60,thick,<->,shorten >=2pt,shorten <=2pt] (BPointExtra) -- ($(C)!(BPointExtra)!(CPoint)$) node[black!60,above=3pt,midway,rotate=45,fill=white]{Segment};
\end{tikzpicture}
\end{center}

\end{document}

答案1

一个相当简单易行的解决方案是\filldraw在绘制线条后移动命令。由于坐标BC都已定义好,因此更改\filldraw节点的绘制时间不会影响使用(B) -- something或的命令的依赖关系(C) -- something,反之亦然。此外,如果您仔细观察线和圆的交汇处,灰线将在圆与其重叠并切断的地方弯曲;但是,缩短它上面的一条线仍然会有一个平端或圆角(如果您使用)line cap = round。这些端点仍然会在圆的顶部,或者如果您缩短太多,永远不会与圆相交,留下空白。在我看来,这看起来很不自然。

\documentclass[11pt]{scrartcl}
\usepackage{tikz,relsize,amsmath}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{lmodern}
\usetikzlibrary{
  calc,trees,shadows,positioning,arrows,chains,shapes.geometric,
  decorations.pathreplacing,decorations.pathmorphing,shapes,
  matrix,shapes.symbols,patterns,intersections,fit}
\pgfdeclarelayer{background layer}
\pgfdeclarelayer{foreground layer}
\pgfsetlayers{background layer,main,foreground layer}
\tikzset{>=latex}
\begin{document}
\begin{center}
  \begin{tikzpicture}[font=\sffamily\small]
    %                                                                               
    \draw[style=help lines,step=0.5cm] (0,0) grid (8,6);
    %                                                                               
    \draw[->,thick] (-0.1,0) -- (8.5,0) node[anchor=west]{x}; %X-Achse              
    \draw[->,thick] (0,-0.1) -- (0,6.5) node[anchor=south]{y}; %Y-Achse             
    %                                                                               
    \draw
    (0,0) coordinate (orig)
    (3,3) coordinate (B)
    (4.5,4.5) coordinate (C)
    ;
    %                                                                               
    %                                                                               
    \path[name path=Segment] (B) -- (C);
    % highlight segment                                                             
    \draw
    ($(B) + (-0.75,0.75)$) coordinate (BPoint)
    ($(B) + (-0.25,0.25)$) coordinate (BPointExtra)
    ($(C) + (-0.75,0.75)$) coordinate (CPoint)
    ;

    \draw[black!60,thick] (B) -- (BPoint);
    \draw[black!60,thick] (C) -- (CPoint);

    \draw[black!60,thick,<->,shorten >=2pt,shorten <=2pt] (BPointExtra) --
    ($(C)!(BPointExtra)!(CPoint)$)
    node[black!60,above=3pt,midway,rotate=45,fill=white]{Segment};

    %moving the filldraw after the lines being drawn works
    \foreach \pt/\labpos in {B/below right,C/below right}{
      \filldraw (\pt) circle (3pt) node[\labpos=3pt,fill=white]{\pt};
    };
  \end{tikzpicture}
\end{center}
\end{document}

在此处输入图片描述


使用缩短方法将产生与此类似的效果,具体取决于您获得的接近程度。这里的问题是您试图将曲面与直曲面对置:

在此处输入图片描述

使用带有以下项的缩短方法line cap = round

在此处输入图片描述

将线放在黑色圆圈下方(这看起来是正确的):

在此处输入图片描述

相关内容