8

I am using the following code to add the tangent vector at various points along a curve. I would like to normalize each vector, thus resulting in tangent vectors of equal unit length. How might I achieve this?

\documentclass[11pt]{article}   
\usepackage{tikz}       
 \usetikzlibrary{arrows}      
\begin{document}

 \begin{tikzpicture}[allow upside down,x=1.5cm,y=1.4cm]
    \draw[red,thick] (0,0)
    .. controls +(right:6cm) and +(left:4cm) .. (2,6)
    \foreach \p in {0,10,...,100} {
      node[sloped,inner sep=0cm,above,pos=\p*0.01,
      anchor=south west,
      minimum height=(10+\p)*0.03cm,minimum width=(10+\p)*0.03cm]
      (N \p){}
    }
    .. controls +(right:2cm) and +(right:3cm) .. (6,2)
    \foreach \p in {0,10,...,100} {
      node[sloped,inner sep=0cm,above,pos=\p*0.01,
      anchor=south west,
      minimum height=(110-\p)*0.03cm,minimum width=(110-\p)*0.03cm]
      (N2 \p){}
    }
    ;
    \foreach \p in {0,10,...,100} {
      \draw[-latex,color=black] (N \p.south west) -- (N \p.south east);
    }
    \foreach \p in {0,10,...,100} {
      \draw[-latex,color=black] (N2 \p.south west) -- (N2 \p.south east);
    }
  \end{tikzpicture}
 \end{document}
8

With the calc tikz-library, the syntax

($A!1cm!B$)

means a point 1cm from A in the direction B. Using this we can easily modify your tangent vector expressions to

\foreach \p in {0,10,...,100} {
  \draw[-latex,color=black] (N \p.south west)
    -- ($(N \p.south west)!1cm!(N \p.south east)$);
  }
  \foreach \p in {0,10,...,100} {
  \draw[-latex,color=black] (N2 \p.south west)
    -- ($(N2 \p.south west)!1cm!(N2 \p.south east)$);
  }

which produces

Sample output

\documentclass[11pt]{article}   
\usepackage{tikz}       
\usetikzlibrary{arrows,calc}      

\begin{document}

\begin{tikzpicture}[allow upside down,x=1.5cm,y=1.4cm]
  \draw[red,thick] (0,0)
  .. controls +(right:6cm) and +(left:4cm) .. (2,6)
  \foreach \p in {0,10,...,100} {
    node[sloped,inner sep=0cm,above,pos=\p*0.01,
    anchor=south west,
    minimum height=(10+\p)*0.03cm,minimum width=(10+\p)*0.03cm]
    (N \p){}
  }
  .. controls +(right:2cm) and +(right:3cm) .. (6,2)
  \foreach \p in {0,10,...,100} {
    node[sloped,inner sep=0cm,above,pos=\p*0.01,
    anchor=south west,
    minimum height=(110-\p)*0.03cm,minimum width=(110-\p)*0.03cm]
    (N2 \p){}
  }
  ;
  \foreach \p in {0,10,...,100} {
  \draw[-latex,color=black] (N \p.south west)
    -- ($(N \p.south west)!1cm!(N \p.south east)$);
  }
  \foreach \p in {0,10,...,100} {
  \draw[-latex,color=black] (N2 \p.south west)
    -- ($(N2 \p.south west)!1cm!(N2 \p.south east)$);
  }
\end{tikzpicture}


\end{document}
5

For comparison, here's a Metapost version, where defining the path, drawing it, and marking the tangents are done separately.

enter image description here

prologues := 3;
outputtemplate := "%j%c.eps";

beginfig(1);
u = 2cm;
% define the curve
path curve; 
curve = ( origin .. controls (6,0) and (-2,6)
        .. (2,6) .. controls (4,6) and (9,2) 
        .. (6,2) ) scaled u;

% draw it in pink
draw curve withpen pencircle scaled 1.44 withcolor .67[red,white];

% mark it with tangents at regular intervals
pair p, q;
A := arclength curve;
s := A/20;
for a = 0 step s until A+eps:
  t := arctime a of curve;
  p := point t of curve;
  q := p + 1/2u * unitvector(direction t of curve);
  drawarrow p -- q;
endfor

endfig;
end.
4

Here is another method to get tangents via the decorations.markings library:

enter image description here

\documentclass[tikz]{standalone}   
\usetikzlibrary{arrows,calc,decorations.markings}      
\begin{document}
\begin{tikzpicture}[x=1.5cm,y=1.4cm]
  \tikzset{
    draw tangents/.style={decorate,decoration={
        markings,mark=between positions 0 and 1 step .04 with
        {\draw[-latex,black,thin] (0,0) -- (1cm,0);},
      }
    }
  }
  \draw[red,thick,postaction=draw tangents]
  (0,0) .. controls +(right:6cm) and +(left:4cm) .. (2,6)
  .. controls +(right:2cm) and +(right:3cm) .. (6,2);
\end{tikzpicture}
\end{document}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.