如何在 TikZ 中在路径的控制点处绘制切线?

如何在 TikZ 中在路径的控制点处绘制切线?

我正在使用

\draw plot[smooth, tension=.7] coordinates ...

命令。现在我想在指定的坐标处绘制切线。我已经尝试过杰克的回答,但我不知道如何定义某些坐标处的切点,而不是像路径位置那样0.4

我当前的代码如下:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{decorations.markings}

\begin{document}
\begin{tikzpicture}[scale=0.35, transform shape,
    tangent/.style={
        decoration={
            markings,% switch on markings
            mark=
                at position #1
                with
                {
                    \coordinate (tangent point-\pgfkeysvalueof{/pgf/decoration/mark info/sequence number}) at (0pt,0pt);
                    \coordinate (tangent unit vector-\pgfkeysvalueof{/pgf/decoration/mark info/sequence number}) at (1,0pt);
                    \coordinate (tangent orthogonal unit vector-\pgfkeysvalueof{/pgf/decoration/mark info/sequence number}) at (0pt,1);
                }
        },
        postaction=decorate
    },
    use tangent/.style={
        shift=(tangent point-#1),
        x=(tangent unit vector-#1),
        y=(tangent orthogonal unit vector-#1)
    },
    use tangent/.default=1
]

\begin{scope}[every node/.style={fill,draw,blue!50,thick,circle}]
  \draw[thick, tangent=0.9] plot[smooth, tension=.7] coordinates {(-8,5) (-5.5,7) (-3,5.5)};
  \node (m1) at (-8,5)   {};
  \node (m2) at (-5.5,7) {};
  \node (m3) at (-3,5.5) {};
  \draw [blue, use tangent] (-0.5,0) -- (0.5,0);
\end{scope}

\begin{scope}[every node/.style={fill,draw,red!50,thick,circle}, rotate=8, shift={(-0.5,3.5)}]
  \draw[thick] plot[smooth, tension=.7] coordinates {(-8,5) (-5.5,7) (-3,5.5)};
  \node at (-8,5)   {} edge (m1);
  \node at (-5.5,7) {} edge (m2);
  \node at (-3,5.5) {} edge (m2);
\end{scope}

\end{tikzpicture}
\end{document}

这导致:

上述代码的输出

我想在蓝色节点处绘制切线,然后用从红色节点开始并在切线处正交结束的线替换三条直接连接线:

期望输出

提前致谢!

答案1

事实证明它并不像我想象的那么容易使用,但这是一种可能性。

您首先使用 key 命名曲线curve prefix,然后使用my mark放置标记的自定义标记类型绘制它们。优点是您不必放置额外的节点,但缺点是您必须提供mark indices={1,...,n}实际编号标记。此外,此过程不包括端点,但您仍然可以使用 Jake 的样式,因为pos=0pos=1始终是您希望绘制切线的点。

节点名称始终为(tpt-<curve prefix>-<mark number>),您必须使用get tangle=<mark number> of <curve prefix>。然后使用它来旋转图形以绘制自定义切线。

只需将代码放在\makeatletter ... \makeatother序言中的某个位置即可。

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{plotmarks}

\makeatletter
\tikzset{curve prefix/.code={
    \xdef\pgf@mark@prefix{#1}
    }
}
\tikzset{get tangle/.code args={#1 of #2}{% Looks at the previous and next marks and fakes a tangent
\pgfmathparse{int(#1-1)}
\pgfpointanchor{tpt-#2-\pgfmathresult}{center}
\pgf@xa=\pgf@x\pgf@ya=\pgf@y
\pgfmathparse{int(#1+1)}
\pgfpointanchor{tpt-#2-\pgfmathresult}{center}
\pgf@xb=\pgf@x\pgf@yb=\pgf@y
\pgfpointdiff{\pgfpoint{\pgf@xa}{\pgf@ya}}{\pgfpoint{\pgf@xb}{\pgf@yb}}
\pgfmathparse{atan2(\pgf@x,\pgf@y)}
\xdef\tangle{\pgfmathresult}
    }
}

\pgfdeclareplotmark{my mark}% Places coordinates on the marks to be used above
{\pgfsetfillcolor{\pgf@mark@color}\pgfpathcircle{\pgfpoint{0cm}{0cm}}{1ex}\pgfusepathqfill
\pgfcoordinate{tpt-\pgf@mark@prefix-\the\pgf@plot@mark@count}{\pgfpointorigin}
}
\makeatother

\begin{document}
\begin{tikzpicture}
%First Curve
\draw[thick,curve prefix=a] plot[mark color=blue!50,mark=my mark,mark indices={1,...,5},smooth,tension=0.7] 
coordinates {(-8,5) (-5.5,2) (-3,5.5) (-1,-2) (1,3)};

% Second curve
\begin{scope}[every node/.style={fill,draw,red!50,thick,circle}, rotate=8, shift={(-0.5,3.5)}]
\draw[thick,curve prefix=b] plot[mark color=red,mark=my mark,mark indices={1,...,4},smooth,tension=0.7] 
coordinates {(-8,5) (-5.5,7) (-3,5.5) (0,3)};
\end{scope}


% First tangent drawing 
\draw[get tangle=2 of a,dashed,ultra thick,purple,rotate=\tangle] (tpt-a-2) -| (tpt-b-2) (tpt-a-2) -| (tpt-b-4);

% Second tangent drawing 
\draw[get tangle=4 of a,dashdotted,ultra thick,orange,rotate=\tangle] (tpt-a-4) -| (tpt-b-3) (tpt-a-4) -| (tpt-b-1);

\end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容