Drawing tangent lines from points on 2 curves to x and y axes

tikz-pgf

I used the following code to draw tangent lines from points a and o (on 2 curves starting from a certain point) to x and y axes.

The node at point strt is used to draw other curves and lines; so I need the 2 curves to start from it.

Is there a code that automates this drawing, without trying drawing lines from different points on the x and y axes till they pass through the designated points on the curves.

\documentclass{beamer}
\beamertemplatenavigationsymbolsempty
\usepackage{verbatim}
\usepackage{tikz}
\begin{document}
\begin{frame}[t]
\frametitle{tangent lines to x and y axes}
\begin{tikzpicture}[scale=1., transform shape]
\draw [thick] (0,0) -- (7,0);
\draw [thick] (0,0) -- (0,6);

\node at (2.5,2.) (strt){};
\draw [very thick, blue, looseness=.8] (strt.center) to [out=140,in=-80] node [pos=.3] (x){x} +(120.:3.cm) (strt.center) to [out=-40,in=170] node [pos=.25] (o){o} +(-20.:3.cm);
\draw [thick, red] (3.8,0) -- (0,5.2);
\draw [thick, red] (6.2,0) -- (0,3.2);
\end{tikzpicture}
\end{frame}
\end{document}

enter image description here

Best Answer

enter image description here

The answer is updated in accordance with Hany's remark, "I want the tangent at point 1 to end at y axis with a node containing some text".

There are two decorations tanget at and tangent vector at. The main one is the former and is used for the points 1 and 2. The later, used for 3, is simpler and indicates the sens in which TikZ goes along the curve.

Some comments concerning tanget at

  1. It has three arguments: #1 is a sub-unitary float which determines the point on the curve; #2 and #3 are multiplicative constants.
  2. It draws a tangent (segment) line at #1. Think about #1 as being a value of the time coordinate describing the curve with constant speed.
  3. The length of the tangent line is determined by the two multiplicative constants: #2 represents the length (in length units, cm by default) of the tangent semi-line in the negative direction. The same for #3, but in the positive one.
  4. As a side effect, tangent at sets the following names to points involved in the construction:
  • (point-k) the point on the curve defined by #1
  • (tpoint-k) the point on the tangent line such that (tpoint-k) - (point-k) is the unit tangent vector in the positive direction (see also tangent vector at)
  • (A-k) and (B-k) the extremities of the tangent line.

The integer k stands for the index of the point in the sequence of invocations of tangent at. In the above drawing k is 1 or 2.

Remark You can play with the arguemnts #2 and #3 to have the tangent line you want at the point #1. Then use (A-k) and/or (B-k) to insert the text you need. It might be easier to draw the point (A-k) before looking for the perfect value of #2.

The code

\documentclass[11pt, border=.4cm]{standalone}
\usepackage{tikz}
\usetikzlibrary{calc, math}
\usetikzlibrary{decorations.markings}

\begin{document}

\tikzset{
  tangent vector at/.style={
    decoration={
      markings,  % switch on markings
      mark = at position #1 with {
        \filldraw[red] (0, 0) circle (1.5pt) ++(0, 1.5ex)
        node[scale=.8]
        {\pgfkeysvalueof{/pgf/decoration/mark info/sequence number}};
        \draw[red, ->] (0, 0) -- (1, 0);
      }
    }, postaction=decorate
  },
  tangent at/.style args={#1;#2|#3}{% 
    decoration={
      markings,  % switch on markings
      mark = at position #1 with {
        \path (0, 0) coordinate
        (point-\pgfkeysvalueof{/pgf/decoration/mark info/sequence number});
        \path (1, 0) coordinate (tpoint-%
        \pgfkeysvalueof{/pgf/decoration/mark info/sequence number});
        \filldraw[blue] (0, 0) circle (1.5pt) ++(0, 1.5ex)
        node[scale=.8]
        {\pgfkeysvalueof{/pgf/decoration/mark info/sequence number}};
        \draw[blue] (-#2, 0)
        coordinate (A-\pgfkeysvalueof{/pgf/decoration/mark info/sequence number})
        -- (#3, 0)
        coordinate (B-\pgfkeysvalueof{/pgf/decoration/mark info/sequence number});
      }
    }, postaction=decorate
  }  
}

\begin{tikzpicture}[every node/.style={scale=.8}]
  \begin{scope}[gray, very thin, ->]
    \draw (-1, 0) -- (5.5, 0) node[right] {$x$};
    \draw (0, -1) -- (0, 3.5) node[above] {$y$};
  \end{scope}
  
  \draw[tangent at={.4; 1.92|2}, tangent at={.77; 1|1.1},
  tangent vector at=.02]
  (-1, 1) .. controls +(2, 2.3) and +(3, 3) .. ++(4.5, -1.5);
  \filldraw (A-1) circle (1.5pt) node[above left] {Some text};

  % a curve through the first point
  \draw[red] (point-1) to[out=210, in=90] ++(0, -2);
\end{tikzpicture}  

\end{document}